@@ -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") )
10361032date_parse <- function (x , ... , format = NULL , locale = clock_locale()) {
10371033 x <- naive_time_parse(x , ... , format = format , precision = " day" , locale = locale )
10381034 as.Date(x )
0 commit comments