Skip to contents

A generic function for rounding date and time values

Usage

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] "2024-04-15 12:00:00 UTC"

## class date
unique(round_time(seq(Sys.Date(), Sys.Date() + 100, "1 day"), "weeks"))
#>  [1] "2024-04-11" "2024-04-18" "2024-04-25" "2024-05-02" "2024-05-09"
#>  [6] "2024-05-16" "2024-05-23" "2024-05-30" "2024-06-06" "2024-06-13"
#> [11] "2024-06-20" "2024-06-27" "2024-07-04" "2024-07-11" "2024-07-18"