Checks whether a vector can be coerced to a lubridate::Period without losing information, returning it silently if so. Otherwise an informative error message is signaled.
Usage
to_dur(
x,
...,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)
to_duration(
x,
...,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class '`NULL`'
to_dur(x, ..., allow_null = TRUE, x_arg = caller_arg(x), call = caller_env())
# S3 method for class 'Period'
to_dur(x, ...)
# S3 method for class 'character'
to_dur(
x,
...,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'factor'
to_dur(
x,
...,
x_arg = caller_arg(x),
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'difftime'
to_dur(x, ..., call = caller_env())
# S3 method for class 'numeric'
to_dur(x, ..., call = caller_env())
# S3 method for class 'integer'
to_dur(x, ..., call = caller_env())Arguments
- x
The object to stabilize.
- ...
Arguments passed to methods.
- x_arg
(
character(1)) The name of the object being stabilized to use in error messages. The automatic value will work in most cases, or pass it through from higher-level functions to make error messages clearer in unexported functions.- call
(environment)The execution environment to mention as the source of error messages.- x_class
(
character(1)) The class name of the object being stabilized to use in error messages. Use this if you remove a special class from the object before checking its coercion, but want the error message to match the original class.- allow_null
(
logical(1)) Is NULL an acceptable value?
Value
The input as a lubridate::Period vector.
Details
Period is chosen as the target class because it preserves the calendar
components (years, months, days, hours, minutes, seconds) of an RFC 3339 / ISO 8601 duration string separately,
rather than collapsing them into a single number of seconds. That fidelity
comes at a cost: years and months have no fixed length, so comparisons
(min_value, max_value, allowed_values in stabilize_dur()) use
lubridate's approximate, nominal lengths (a 365.25-day year, a 30.4375-day
month) and may not reflect the actual elapsed time implied by a specific
calendar date.
Character vectors must use the RFC 3339 duration format: "P"
followed by an optional number of years (Y), months (M), and days (D),
then an optional "T"-prefixed block of hours (H), minutes (M), and
seconds (S) (for example "P3Y6M4DT12H30M5S", "P23DT23H", or "PT1M");
or a stand-alone week form ("P4W"). The date/time form and the week form
cannot be mixed, fractional components are not permitted, and "P" or "PT"
alone (with no components) are not valid durations. base::difftime values
(including hms::hms()) are converted directly, preserving their unit.
Numeric and integer values are treated as a (fractional) number of seconds
and broken into day/hour/minute/second components, with no year or month
component, since a number of seconds cannot unambiguously imply a calendar
length.
to_dur() requires the lubridate package.
See also
Other duration functions:
specify_dur(),
stabilize_dur(),
stabilize_dur_scalar(),
to_dur_scalar()
Other stabilization functions:
assert_present(),
stabilize_any_of(),
stabilize_arg(),
stabilize_chr(),
stabilize_chr_scalar(),
stabilize_date(),
stabilize_date_scalar(),
stabilize_dbl(),
stabilize_dbl_scalar(),
stabilize_df(),
stabilize_dttm(),
stabilize_dttm_scalar(),
stabilize_dur(),
stabilize_dur_scalar(),
stabilize_fct(),
stabilize_fct_scalar(),
stabilize_int(),
stabilize_int_scalar(),
stabilize_lgl(),
stabilize_lgl_scalar(),
stabilize_lst(),
stabilize_time(),
stabilize_time_scalar(),
to_chr(),
to_chr_scalar(),
to_date(),
to_date_scalar(),
to_dbl(),
to_dbl_scalar(),
to_dttm(),
to_dttm_scalar(),
to_dur_scalar(),
to_fct(),
to_fct_scalar(),
to_int(),
to_int_scalar(),
to_lgl(),
to_lgl_scalar(),
to_time(),
to_time_scalar()
Examples
to_dur(lubridate::period(year = 1, month = 2, day = 3))
#> [1] "1y 2m 3d 0H 0M 0S"
to_dur("P3Y6M4DT12H30M5S")
#> [1] "3y 6m 4d 12H 30M 5S"
to_dur("PT1M")
#> [1] "1M 0S"
to_dur("P4W")
#> [1] "28d 0H 0M 0S"
to_dur(c("P1D", NA))
#> [1] "1d 0H 0M 0S" NA
to_dur(3661)
#> [1] "1H 1M 1S"
to_dur(as.difftime(90, units = "mins"))
#> [1] "1H 30M 0S"
to_dur(NULL)
#> NULL
try(to_dur("P"))
#> Error in eval(expr, envir) :
#> `"P"` <character> must be coercible to <duration>
#> ✖ Can't convert some values due to invalid or ambiguous duration format.
#> • Locations: 1
#> • Values: "P"
try(to_dur(c("P1D", "not-a-duration")))
#> Error in eval(expr, envir) :
#> `c("P1D", "not-a-duration")` <character> must be coercible to <duration>
#> ✖ Can't convert some values due to invalid or ambiguous duration format.
#> • Locations: 2
#> • Values: "not-a-duration"