A family of helpers to round date/time objects to a specified time granule such as second, minute, hour, or day. These functions preserve the input time class, as rounded by the attributes of the unit.

round_time(x, unit, ...)

Arguments

x

A date/time object to be rounded. Accepted types include Date, POSIXct, POSIXlt and other objects that inherit from POSIXt. The returned object will be of the same class as the input.

...

Additional arguments passed to specific implementations.

units

A time unit (or object coercible to a time unit, e.g. "day").

Value

An object of the same class as x with its time components adjusted to the requested unit.

See also

Examples

# Round POSIXct to the nearest minute (preserving tz)
t <- as.POSIXct("2020-01-01 12:34:56", tz = "UTC")
round_time(t, units = "minute")
#> Error in round_time.default(t, units = "minute"): argument "unit" is missing, with no default

# Floor to the nearest hour
floor_time(t, units = "hour")
#> Error in floor_time.default(t, units = "hour"): argument "unit" is missing, with no default

# Ceiling a Date (treated as midnight-of-day rounding)
d <- as.Date("2020-01-01")
ceiling_time(d, units = "day")
#> Error in ceiling_time.default(d, units = "day"): argument "unit" is missing, with no default