datetime_precision() returns the "precision" of a datetime vector's datetimes.
precision_to_int() converts the precision to an integer.
Usage
datetime_precision(x, ...)
# S3 method for class 'datetimeoffset'
datetime_precision(x, range = FALSE, unspecified = FALSE, ...)
# S3 method for class 'clock_calendar'
datetime_precision(x, ...)
# S3 method for class 'clock_time_point'
datetime_precision(x, ...)
# S3 method for class 'clock_zoned_time'
datetime_precision(x, ...)
# S3 method for class 'nanotime'
datetime_precision(x, ...)
precision_to_int(precision)Arguments
- x
A datetime vector. Either
datetimeoffset(), a "clock" "calendar", or a "clock" "time".- ...
Used by some S3 methods.
- range
If
TRUEreturn just the minimum and maximum "precision".- unspecified
If
TRUEuse the smallest non-missing component's as the precision even if there is a missing value for a larger component.- precision
A datetime precision (as returned by
datetime_precision()).
Value
datetime_precision() returns a character vector of precisions.
Depending on the object either "missing", "year", "quarter", "month", "week",
"day", "hour", "minute", "second", "millisecond", "microsecond", or "nanosecond".
precision_to_int() returns an integer vector.
Examples
dts <- as_datetimeoffset(c("2020", "2020-04-10", "2020-04-10T10:10"))
datetime_precision(dts)
#> [1] "year" "day" "minute"
datetime_precision(dts, range = TRUE)
#> [1] "year" "minute"
dt <- datetimeoffset(2020, NA_integer_, 10)
datetime_precision(dt)
#> [1] "year"
datetime_precision(dt, unspecified = TRUE)
#> [1] "day"
precision_to_int("year") < precision_to_int("day")
#> [1] TRUE
library("clock")
datetime_precision(year_month_day(1918, 11, 11))
#> [1] "day"
datetime_precision(sys_time_now())
#> [1] "nanosecond"
datetime_precision(zoned_time_now(Sys.timezone()))
#> [1] "nanosecond"