naive_time_parse | R Documentation |
naive_time_parse()
is a parser into a naive-time.
naive_time_parse()
is useful when you have date-time strings like
"2020-01-01T01:04:30"
. If there is no attached UTC offset or time zone
name, then parsing this string as a naive-time is your best option. If
you know that this string should be interpreted in a specific time zone,
parse as a naive-time, then use as_zoned_time()
.
The default options assume that x
should be parsed at second precision,
using a format
string of "%Y-%m-%dT%H:%M:%S"
. This matches the default
result from calling format()
on a naive-time.
naive_time_parse()
ignores both the %z
and %Z
commands.
If your date-time strings contain a full time zone name and a UTC offset, use
zoned_time_parse_complete()
. If they contain a time zone abbreviation, use
zoned_time_parse_abbrev()
.
If your date-time strings contain a UTC offset, but not a full time zone
name, use sys_time_parse()
.
naive_time_parse(
x,
...,
format = NULL,
precision = "second",
locale = clock_locale()
)
x |
A character vector to parse. |
... |
These dots are for future extensions and must be empty. |
format |
A format string. A combination of the following commands, or A vector of multiple format strings can be supplied. They will be tried in the order they are provided. Year
Month
Day
Day of the week
ISO 8601 week-based year
Week of the year
Day of the year
Date
Time of day
Time zone
Miscellaneous
|
precision |
A precision for the resulting time point. One of:
Setting the |
locale |
A locale object created from |
A naive-time.
It is highly recommended to parse all of the information in the date-time
string into a type at least as precise as the string. For example, if your
string has fractional seconds, but you only require seconds, specify a
sub-second precision
, then round to seconds manually using whatever
convention is appropriate for your use case. Parsing such a string directly
into a second precision result is ambiguous and undefined, and is unlikely to
work as you might expect.
naive_time_parse("2020-01-01T05:06:07")
# Day precision
naive_time_parse("2020-01-01", precision = "day")
# Nanosecond precision, but using a day based format
naive_time_parse("2020-01-01", format = "%Y-%m-%d", precision = "nanosecond")
# Remember that the `%z` and `%Z` commands are ignored entirely!
naive_time_parse(
"2020-01-01 -4000 America/New_York",
format = "%Y-%m-%d %z %Z"
)
# ---------------------------------------------------------------------------
# Fractional seconds and POSIXct
# If you have a string with fractional seconds and want to convert it to
# a POSIXct, remember that clock treats POSIXct as a second precision type.
# Ideally, you'd use a clock type that can support fractional seconds, but
# if you really want to parse it into a POSIXct, the correct way to do so
# is to parse the full fractional time point with the correct `precision`,
# then round to seconds using whatever convention you require, and finally
# convert that to POSIXct.
x <- c("2020-01-01T00:00:00.123", "2020-01-01T00:00:00.555")
# First, parse string with full precision
x <- naive_time_parse(x, precision = "millisecond")
x
# Then round to second with a floor, ceiling, or round to nearest
time_point_floor(x, "second")
time_point_round(x, "second")
# Finally, convert to POSIXct
as_date_time(time_point_round(x, "second"), zone = "UTC")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.