Returns for (each of) the given timestamp(s) the timestamp(s) itself if it represents a multiple of the given time step or the nearest smaller or nearest greater timestamp that represents a multiple of the time step.

roundTime(tstamp, tstep, direction = -1)

Arguments

tstamp

(vector of) timestamp(s) of class "POSIXlt" or "POSIXct"

tstep

time step in seconds of which timestamps in tstamp shall represent multiples

direction

one of -1, 0, 1. If -1, the nearest timestamp (either smaller or greater) complying with the timestamp is returned. If 0, always the nearest greater timestamp and if 1, always the nearest smaller timestamp is returned.

Value

(Vector of) timestamp(s) corresponding to timestamp(s) given in tstamp being “rounded” to the nearest --- greater or smaller (direction == -1), always smaller (direction == 1) or always greater (direction == 0) --- timestamp representing a multiple of the given time step tstep.

Examples

# Generate a timestamp to be "rounded" t0 <- hsToPosix("2011-12-24 18:22:05") # Round to nearest (default) full minute roundTime(t0, 60) ## = 2011-12-24 18:22:00 UTC
#> [1] "2011-12-24 18:22:00 UTC"
# Round to nearest full greater minute roundTime(t0, 60, 0) ## = 2011-12-24 18:23:00 UTC
#> [1] "2011-12-24 18:23:00 UTC"
# Round to nearest multiple of 15 minutes (-1 could be omitted) roundTime(t0, 15*60, -1) ## 2011-12-24 18:15:00 UTC
#> [1] "2011-12-24 18:15:00 UTC"
# Round to nearest smaller multiple of four hours roundTime(t0, 4*60*60, 1) ## 2011-12-24 16:00:00 UTC
#> [1] "2011-12-24 16:00:00 UTC"