| time_parse | R Documentation |
time_parse() is the inverse of format(): given text and the same
{lin(...)}/{cyc(...)}/{dur(...)} template format uses, it
reconstructs the time points (via time_compose()) or durations that would
have produced that text.
time_parse(
x,
chronon = NULL,
cycle = NULL,
format = NULL,
regex = FALSE,
na = c("", "NA"),
calendar = NULL,
locale = NULL,
discrete = TRUE
)
x |
A character vector to parse. |
chronon |
Target time granule for the result, and (with |
cycle |
Target cycle granule, pairing with |
format |
A glue-style format string of |
regex |
If |
na |
Strings to treat as missing ( |
calendar |
Calendar used to resolve granule names in |
locale |
Default locale for named ( |
discrete |
Whether the result is discrete (integer chronon counts)
or continuous (fractional). See |
A format with a {lin(...)} token parses to linear time; one of only
{cyc(...)} tokens parses to cyclical time, e.g. time_parse("Feb", format = "{cyc(month, year, label = TRUE)}") recovers the same kind of
value as month_of_year(). A format with a {dur(...)} token (which
must be its only token) parses to a duration(), e.g. time_parse("3 days", format = "{dur(day)}").
Granule-specific extraction and decoding labels for each {lin(...)}/
{cyc(...)} token is done by linear_labels_parse()/
cyclical_labels_parse(). A {dur(...)} token matches a number, optionally
followed by the granule's unit name (e.g. "3 days", "1 day", "3D" or
"3").
A mixtime time vector, the same length as x. A duration if
format is a {dur(...)} token, linear if it includes a {lin(...)}
token (or cycle is NULL), cyclical otherwise.
format() for the inverse direction, time_compose() for
composing a time point from already-decoded components,
label_scheme() for declaring how a granule's labels parse,
chronon_parse_linear()/chronon_parse_cyclical() for the candidate
formats derived from chronon/cycle, and vignette("time-format-strings")
for the format string syntax.
time_parse("2024-02-15", format = "{lin(year)}-{cyc(month, year)}-{cyc(day, month)}")
time_parse(
"15 Feb 2024",
format = "{cyc(day, month)} {cyc(month, year, label = TRUE)} {lin(year)}"
)
# One bad value becomes NA (with a warning) instead of aborting the batch
time_parse(
c("2024-02-15", "not a date"),
format = "{lin(year)}-{cyc(month, year)}-{cyc(day, month)}"
)
# No {lin(...)} token: parses to cyclical time
time_parse("Feb", format = "{cyc(month, year, label = TRUE)}")
# Several formats: whichever parses the most values is used for the whole
# vector; here none of the "Y-M-D" format's values match, so the "D/M/Y"
# format (which matches both) is used instead
time_parse(
c("15/02/2024", "20/03/2024"),
format = c(
"{lin(year)}-{cyc(month, year)}-{cyc(day, month)}",
"{cyc(day, month)}/{cyc(month, year)}/{lin(year)}"
)
)
# regex = TRUE: match "/" or "-" as the separator,
# and tolerate a trailing comment after the date.
time_parse(
c("2024-02-15", "2024/02/15 (approx)"),
format = "{lin(year)}[/-]{cyc(month, year)}[/-]{cyc(day, month)}( .*)?",
regex = TRUE
)
# Durations, with an optional unit name after the magnitude
time_parse(c("3 days", "1 day", "2D", "5"), format = "{dur(day)}")
time_parse("1.5 hours", format = "{dur(hour)}", discrete = FALSE)
# Default format strings from the target chronon, and results with `tz`.
time_parse("2024-02-15 09:00:00", chronon = cal_gregorian$second(1L, tz = "America/Los_Angeles"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.