Checks whether a vector can be converted to a lubridate::Period without losing information, returning it silently if so. Otherwise an informative error message is signaled.
Usage
to_dur(
x,
...,
treat_number_as = c("seconds", "milliseconds", "days", "error"),
x_arg = caller_arg(x),
to_arg = NULL,
call = caller_env(),
x_class = object_type(x)
)
to_duration(
x,
...,
treat_number_as = c("seconds", "milliseconds", "days", "error"),
x_arg = caller_arg(x),
to_arg = NULL,
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class '`NULL`'
to_dur(
x,
...,
allow_null = TRUE,
x_arg = caller_arg(x),
to_arg = NULL,
call = caller_env()
)
# S3 method for class 'Period'
to_dur(x, ...)
# S3 method for class 'character'
to_dur(
x,
...,
x_arg = caller_arg(x),
to_arg = NULL,
call = caller_env(),
x_class = object_type(x)
)
# S3 method for class 'factor'
to_dur(
x,
...,
x_arg = caller_arg(x),
to_arg = NULL,
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,
...,
treat_number_as = c("seconds", "milliseconds", "days", "error"),
x_arg = caller_arg(x),
to_arg = NULL,
call = caller_env(),
x_class = object_type(x)
)Arguments
- x
The object to stabilize.
- ...
Arguments passed to methods.
- treat_number_as
(
character(1)) How to interpret numeric input:"seconds"or"milliseconds"as a length of time,"days"as a number of days, or"error"to reject numbers. Ignored for non-numeric input.- 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.- to_arg
(
character(1)orNULL) The name of the object thatxis being converted to match, for use in error messages. WhenNULL, error messages describe onlyx. When supplied, conversion failure messages name bothx_argandto_arg.- 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 conversion, 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, or an error
condition with classes <stbl-error>, <stbl-condition>, <rlang_error>,
<error>, <condition>, and a specific class by failure mode:
<stbl-error-coerce-duration>when numbers are rejected bytreat_number_as = "error".<stbl-error-incompatible_values-duration>when some values cannot be safely converted to a duration.<stbl-error-bad_null>forNULLvalues whenallow_null = FALSE.
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 interpreted according to treat_number_as, by
default as a (fractional) number of seconds. Seconds and milliseconds are
broken into day/hour/minute/second components, with no year or month
component, since a number of seconds cannot unambiguously imply a calendar
length. Days go into the day component, with any fractional part broken into
hours, minutes, and seconds (so 0.5 days is 12 hours). treat_number_as is
ignored for non-numeric input.
to_dur() requires the lubridate package.
See also
Other duration functions:
specify_dur(),
stabilize_dur(),
stabilize_dur_scalar(),
to(),
to_dur_scalar()
Other stabilization functions:
assert_contains(),
assert_not(),
assert_present(),
stabilize_all_of(),
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_one_of(),
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(86400000, treat_number_as = "milliseconds")
#> [1] "1d 0H 0M 0S"
to_dur(1.5, treat_number_as = "days")
#> [1] "1d 12H 0M 0S"
try(to_dur(3661, treat_number_as = "error"))
#> Error in eval(expr, envir) :
#> Can't convert `3661` <double> to <duration>.
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 convertible 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 convertible to
#> <duration>
#> ✖ Can't convert some values due to invalid or ambiguous duration format.
#> • Locations: 2
#> • Values: "not-a-duration"