# list the last 10 days including today
today() + (-9:0)
# how many days have passed since the beginning of the year?
today() - floor_t(today(), "y")
# same but the result is not tdiff
day_of_year(today()) - 1
# single time interval
x <- "2024-06-01 08:00" %--% "2024-06-01 16:00"
# shift by 0, 1, ..., 5 days
x %+d% 0:5
# are we in or after 2026?
today() >= 2026
# are we after 2025?
today() > 2025
# corner cases - ends of months and shifts by months
as.date("2024-01-31") %+m% 0:5
# same
as.date("2024-01-31") + mnths(0:5)
# corner cases - 53rd week of year
as.week(202053) %-y% 0:5
# corner cases - DST changes and shifts by days
if ("Europe/Warsaw" %in% OlsonNames()) {
# 2020-10-25 had 25h with 02:00 repeated
print(as.date_time("2020-10-24 02:00", tz = "Europe/Warsaw") %+h% 23:26)
print(as.date_time("2020-10-24 02:00", tz = "Europe/Warsaw") %+d% 1)
print(as.date_time("2020-10-26 02:00", tz = "Europe/Warsaw") %-h% 26:23)
print(as.date_time("2020-10-26 02:00", tz = "Europe/Warsaw") %-d% 1)
# 2021-03-28 had 23h with 02:00 missing
print(as.date_time("2021-03-27 02:00", tz = "Europe/Warsaw") %+h% 22:25)
print(as.date_time("2021-03-27 02:00", tz = "Europe/Warsaw") %+d% 1)
print(as.date_time("2021-03-29 02:00", tz = "Europe/Warsaw") %-h% 25:22)
print(as.date_time("2021-03-29 02:00", tz = "Europe/Warsaw") %-d% 1)
}
Run the code above in your browser using DataLab