| time_shift | R Documentation |
Shifts time points by a duration, like x + by, with control over how time
points that don't exist in the target granule are resolved. Shifting by a
duration with an irregular relationship to the time points' chronon (e.g.
days within months) can produce time points that don't exist, such as
shifting 31 January by one month. In a time zone, shifting by days or
longer keeps the local clock time, which may have been skipped when daylight
saving starts (e.g. 02:30 on the day clocks go forward from 02:00 to 03:00).
The + and - operators use the default strategy, set by the
mixtime.on_invalid option ("clamp" unless set).
time_shift(x, by, on_invalid = getOption("mixtime.on_invalid", "clamp"))
x |
A time point vector. |
by |
A time |
on_invalid |
How to resolve time points that don't exist in the target
granule. Defaults to the
|
A time point vector of the same type as x.
x <- datetime("2021-01-30 09:30:00")
time_shift(x, months(1L))
time_shift(x, months(1L), on_invalid = "previous")
time_shift(x, months(1L), on_invalid = "next")
time_shift(x, months(1L), on_invalid = "overflow")
time_shift(x, months(1L), on_invalid = "reject")
try(time_shift(x, months(1L), on_invalid = "error"))
# Shifting backwards
time_shift(date("2021-03-31"), months(-1L), on_invalid = "overflow")
# 02:30 doesn't exist on 2025-10-05 in Melbourne (clocks go forward at 02:00)
x <- datetime(as.POSIXct("2025-10-04 02:30", tz = "Australia/Melbourne"))
time_shift(x, days(1L))
time_shift(x, days(1L), on_invalid = "previous")
time_shift(x, days(1L), on_invalid = "next")
time_shift(x, days(1L), on_invalid = "overflow")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.