@@ -1164,9 +1164,16 @@ date_set_zone.POSIXt <- function(x, zone) {
11641164# ' `NA`s, or completely fails to parse, then no time zone will be able to be
11651165# ' determined. In that case, the result will use `"UTC"`.
11661166# '
1167- # ' If manually parsing sub-second components, be aware that they will be
1168- # ' automatically rounded to the nearest second when converting them to POSIXct.
1169- # ' See the examples for a way to control this.
1167+ # ' If you have strings with sub-second components, then these date-time parsers
1168+ # ' are not appropriate for you. Remember that clock treats POSIXct as a second
1169+ # ' precision type, so parsing a string with fractional seconds directly into a
1170+ # ' POSIXct is ambiguous and undefined. Instead, fully parse the string,
1171+ # ' including its fractional seconds, into a clock type that can handle it, such
1172+ # ' as a naive-time with [naive_time_parse()], then round to seconds with
1173+ # ' whatever rounding convention is appropriate for your use case, such as
1174+ # ' [time_point_floor()], and finally convert that to POSIXct with
1175+ # ' [as_date_time()]. This gives you complete control over how the fractional
1176+ # ' seconds are handled when converting to POSIXct.
11701177# '
11711178# ' @inheritParams zoned-parsing
11721179# ' @inheritParams as-zoned-time-naive-time
@@ -1223,28 +1230,21 @@ date_set_zone.POSIXt <- function(x, zone) {
12231230# ' date_time_parse_abbrev(abbrev_times, "America/New_York")
12241231# '
12251232# ' # ---------------------------------------------------------------------------
1226- # ' # Rounding of sub -second components
1233+ # ' # Sub -second components
12271234# '
1228- # ' # Generally, if you have a string with sub-second components, they will
1229- # ' # be ignored when parsing into a date-time
1230- # ' x <- c("2019-01-01 00:00:01.1", "2019-01-01 00:00:01.7")
1235+ # ' # If you have a string with sub-second components, but only require up to
1236+ # ' # seconds, first parse them into a clock type that can handle sub-seconds to
1237+ # ' # fully capture that information, then round using whatever convention is
1238+ # ' # required for your use case before converting to a date-time.
1239+ # ' x <- c("2019-01-01 00:00:01.1", "2019-01-01 00:00:01.78")
12311240# '
1232- # ' date_time_parse(x, "America/New_York")
1233- # '
1234- # ' # If you manually try and parse those sub-second components with `%4S` to
1235- # ' # read the 2 seconds, 1 decimal point, and 1 fractional component, the
1236- # ' # fractional component will be rounded to the nearest second
1237- # ' date_time_parse(x, "America/New_York", format = "%Y-%m-%d %H:%M:%4S")
1238- # '
1239- # ' # If you don't like this, parse the full string as a naive-time,
1240- # ' # then round manually and convert to a POSIXct
1241- # ' nt <- naive_time_parse(x, format = "%Y-%m-%d %H:%M:%S", precision = "millisecond")
1242- # ' nt
1241+ # ' x <- naive_time_parse(x, precision = "millisecond")
1242+ # ' x
12431243# '
1244- # ' nt <- time_point_floor(nt , "second")
1245- # ' nt
1244+ # ' time_point_floor(x , "second")
1245+ # ' time_point_round(x, "second")
12461246# '
1247- # ' as.POSIXct(nt , "America/New_York")
1247+ # ' as_date_time(time_point_round(x, "second") , "America/New_York")
12481248NULL
12491249
12501250# ' @rdname date-time-parse
0 commit comments