A generic function for rounding date and time values

round_time(x, n, tz)

Arguments

x

A vector of class POSIX or Date.

n

Unit to round to. Defaults to mins. Numeric values treated as seconds. Otherwise this should be one of "mins", "hours", "days", "weeks", "months", "years" (plural optional).

tz

Time zone to be used, defaults to "UTC" (Twitter default)

Value

If POSIXct then POSIX. If date then Date.

Examples

## class posixct round_time(Sys.time(), "12 hours")
#> [1] "2018-08-29 12:00:00 UTC"
## class date unique(round_time(seq(Sys.Date(), Sys.Date() + 100, "1 day"), "weeks"))
#> [1] "2018-08-23" "2018-08-30" "2018-09-06" "2018-09-13" "2018-09-20" #> [6] "2018-09-27" "2018-10-04" "2018-10-11" "2018-10-18" "2018-10-25" #> [11] "2018-11-01" "2018-11-08" "2018-11-15" "2018-11-22" "2018-11-29" #> [16] "2018-12-06"