Skip to content

Commit 490206b

Browse files
committed
Adjust date specific documentation and tests
1 parent f640d3c commit 490206b

File tree

3 files changed

+43
-41
lines changed

3 files changed

+43
-41
lines changed

R/date.R

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -983,9 +983,11 @@ date_set_zone.Date <- function(x, zone) {
983983
#' _`date_parse()` ignores both the `%z` and `%Z` commands,_ as clock treats
984984
#' Date as a _naive_ type, with a yet-to-be-specified time zone.
985985
#'
986-
#' If parsing a string with sub-daily components, such as hours, minutes or
987-
#' seconds, note that the conversion to Date will round those components to
988-
#' the nearest day. See the examples for a way to control this.
986+
#' Parsing strings with sub-daily components, such as hours, minutes, or
987+
#' seconds, should be done with [date_time_parse()]. If you only need the date
988+
#' components, round the result to day precision, and then use [as_date()].
989+
#' Attempting to directly parse a sub-daily string into a Date is ambiguous and
990+
#' undefined, and is unlikely to work as you might expect.
989991
#'
990992
#' @inheritParams zoned-parsing
991993
#'
@@ -1012,27 +1014,21 @@ date_set_zone.Date <- function(x, zone) {
10121014
#' date_parse("2020-W01-2", format = "%G-W%V-%u")
10131015
#'
10141016
#' # ---------------------------------------------------------------------------
1015-
#' # Rounding of sub-daily components
1017+
#' # Sub-daily components
10161018
#'
1017-
#' # Note that rounding a string with time components will round them to the
1018-
#' # nearest day if you try and parse them
1019+
#' # If you have a string with sub-daily components, but only require the date,
1020+
#' # first parse them as date-times to fully parse the sub-daily components,
1021+
#' # then round using whatever convention is required for your use case before
1022+
#' # converting to date.
10191023
#' x <- c("2019-01-01 11", "2019-01-01 12")
10201024
#'
1021-
#' # Hour 12 rounds up to the next day
1022-
#' date_parse(x, format = "%Y-%m-%d %H")
1023-
#'
1024-
#' # If you don't like this, one option is to just not parse the time component
1025-
#' date_parse(x, format = "%Y-%m-%d")
1026-
#'
1027-
#' # A more general option is to parse the full string as a naive-time,
1028-
#' # then round manually
1029-
#' nt <- naive_time_parse(x, format = "%Y-%m-%d %H", precision = "hour")
1030-
#' nt
1025+
#' x <- date_time_parse(x, zone = "UTC", format = "%Y-%m-%d %H")
1026+
#' x
10311027
#'
1032-
#' nt <- time_point_floor(nt, "day")
1033-
#' nt
1028+
#' date_floor(x, "day")
1029+
#' date_round(x, "day")
10341030
#'
1035-
#' as.Date(nt)
1031+
#' as_date(date_round(x, "day"))
10361032
date_parse <- function(x, ..., format = NULL, locale = clock_locale()) {
10371033
x <- naive_time_parse(x, ..., format = format, precision = "day", locale = locale)
10381034
as.Date(x)

man/date_parse.Rd

Lines changed: 15 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-date.R

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,16 +290,16 @@ test_that("formatting Dates with `%z` or `%Z` returns NA with a warning", {
290290

291291
test_that("`%z` and `%Z` commands are ignored", {
292292
expect_identical(
293-
date_parse("2019-12-31 11:59:59-0500", format = "%Y-%m-%d %H:%M:%S%z"),
293+
date_parse("2019-12-31 -0500", format = "%Y-%m-%d %z"),
294294
as.Date("2019-12-31")
295295
)
296296
expect_identical(
297-
date_parse("2019-12-31 11:59:59[America/New_York]", format = "%Y-%m-%d %H:%M:%S[%Z]"),
297+
date_parse("2019-12-31 America/New_York", format = "%Y-%m-%d %Z"),
298298
as.Date("2019-12-31")
299299
)
300300
})
301301

302-
test_that("parsing into a date if you requested to parse time components rounds the time (#207)", {
302+
test_that("parsing into a date if you requested to parse time components rounds the time (#207) (#230) (undocumented)", {
303303
expect_identical(
304304
date_parse("2019-12-31 11:59:59", format = "%Y-%m-%d %H:%M:%S"),
305305
as.Date("2019-12-31")
@@ -310,6 +310,16 @@ test_that("parsing into a date if you requested to parse time components rounds
310310
)
311311
})
312312

313+
test_that("parsing fails when undocumented rounding behavior would result in invalid 60 second component (#230) (undocumented)", {
314+
expect_identical(
315+
expect_warning(
316+
date_parse("2019-01-01 01:01:59.550", format = "%Y-%m-%d %H:%M:%6S"),
317+
class = "clock_warning_parse_failures"
318+
),
319+
new_date(NA_real_)
320+
)
321+
})
322+
313323
test_that("failure to parse throws a warning", {
314324
expect_warning(date_parse("foo"), class = "clock_warning_parse_failures")
315325
expect_snapshot(date_parse("foo"))

0 commit comments

Comments
 (0)