sys-parsing | R Documentation |
There are two parsers into a sys-time, sys_time_parse()
and
sys_time_parse_RFC_3339()
.
sys_time_parse()
is useful when you have date-time strings like
"2020-01-01T01:04:30"
that you know should be interpreted as UTC, or like
"2020-01-01T01:04:30-04:00"
with a UTC offset but no zone name. If you find
yourself in the latter situation, then parsing this string as a sys-time
using the %Ez
command to capture the offset is probably your best option.
If you know that this string should be interpreted in a specific time zone,
parse as a sys-time to get the UTC equivalent, 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 sys-time.
sys_time_parse()
is nearly equivalent to naive_time_parse()
, except for
the fact that the %z
command is actually used. Using %z
assumes that the
rest of the date-time string should be interpreted as a naive-time, which is
then shifted by the UTC offset found in %z
. The returned time can then be
validly interpreted as UTC.
sys_time_parse()
ignores the %Z
command.
sys_time_parse_RFC_3339()
is a wrapper around sys_time_parse()
that is
intended to parse the extremely common date-time format outlined by
RFC 3339. This document
outlines a profile of the ISO 8601 format that is even more restrictive.
In particular, this function is intended to parse the following three formats:
2019-01-01T00:00:00Z 2019-01-01T00:00:00+0430 2019-01-01T00:00:00+04:30
This function defaults to parsing the first of these formats by using
a format string of "%Y-%m-%dT%H:%M:%SZ"
.
If your date-time strings use offsets from UTC rather than "Z"
, then set
offset
to one of the following:
"%z"
if the offset is of the form "+0430"
.
"%Ez"
if the offset is of the form "+04:30"
.
The RFC 3339 standard allows for replacing the "T"
with a "t"
or a space
(" "
). Set separator
to adjust this as needed.
For this function, the precision
must be at least "second"
.
sys_time_parse(
x,
...,
format = NULL,
precision = "second",
locale = clock_locale()
)
sys_time_parse_RFC_3339(
x,
...,
separator = "T",
offset = "Z",
precision = "second"
)
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 |
separator |
The separator between the date and time components of the string. One of:
|
offset |
The format of the offset from UTC contained in the string. One of:
|
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 don't contain an offset from UTC and you aren't
sure if they should be treated as UTC or not, you might consider using
naive_time_parse()
, since the resulting naive-time doesn't come with an
assumption of a UTC time zone.
A sys-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.
sys_time_parse("2020-01-01T05:06:07")
# Day precision
sys_time_parse("2020-01-01", precision = "day")
# Nanosecond precision, but using a day based format
sys_time_parse("2020-01-01", format = "%Y-%m-%d", precision = "nanosecond")
# Multiple format strings are allowed for heterogeneous times
sys_time_parse(
c("2019-01-01", "2019/1/1"),
format = c("%Y/%m/%d", "%Y-%m-%d"),
precision = "day"
)
# The `%z` command shifts the date-time by subtracting the UTC offset so
# that the returned sys-time can be interpreted as UTC
sys_time_parse(
"2020-01-01 02:00:00 -0400",
format = "%Y-%m-%d %H:%M:%S %z"
)
# Remember that the `%Z` command is ignored entirely!
sys_time_parse("2020-01-01 America/New_York", format = "%Y-%m-%d %Z")
# ---------------------------------------------------------------------------
# RFC 3339
# Typical UTC format
x <- "2019-01-01T00:01:02Z"
sys_time_parse_RFC_3339(x)
# With a UTC offset containing a `:`
x <- "2019-01-01T00:01:02+02:30"
sys_time_parse_RFC_3339(x, offset = "%Ez")
# With a space between the date and time and no `:` in the offset
x <- "2019-01-01 00:01:02+0230"
sys_time_parse_RFC_3339(x, separator = " ", offset = "%z")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.