Given a schedule of events, create a new schedule where the events are adjusted according to some rule.
roll_by()
adjusts the events of a schedule by some incremental period.
roll_forward()
and roll_backward()
adjust the events of a schedule
to the nth previous/next occurrence of some other scheduled event.
roll_by(x, n, unit, .p = NULL) roll_forward(x, to_schedule, n = 1, .p = NULL) roll_backward(x, to_schedule, n = 1, .p = NULL)
x | The schedule of events to adjust. |
---|---|
n | The increment of the event adjustment.
|
unit | A character shortcut for a period object. Eg. "year", "years",
"month", "months", "week", "weeks" etc. Can be any value accepted by
|
.p | Optionally, a schedule to use for limiting the adjustment
performed on
|
to_schedule | A schedule to which events can be rolled. |
A schedule object.
library(lubridate, warn.conflicts = FALSE) library(magrittr, warn.conflicts = FALSE) # Imagine you get paid on the 25th of the month on_payday <- on_mday(25) schedule_days(on_payday, during = 2000)#> [1] "2000-01-25" "2000-02-25" "2000-03-25" "2000-04-25" "2000-05-25" #> [6] "2000-06-25" "2000-07-25" "2000-08-25" "2000-09-25" "2000-10-25" #> [11] "2000-11-25" "2000-12-25"# Except if your payday falls on a weekend, in which case it moves to the # next weekday on_payday %>% roll_forward(to = on_weekday(), .p = on_weekend()) %>% schedule_days(during = 2000)#> [1] "2000-01-25" "2000-02-25" "2000-03-27" "2000-04-25" "2000-05-25" #> [6] "2000-06-26" "2000-07-25" "2000-08-25" "2000-09-25" "2000-10-25" #> [11] "2000-11-27" "2000-12-25"# For some people payday may adjust to the previous weekday if it falls on # a weekend on_payday %>% roll_backward(to = on_weekday(), .p = on_weekend()) %>% schedule_days(during = 2000)#> [1] "2000-01-25" "2000-02-25" "2000-03-24" "2000-04-25" "2000-05-25" #> [6] "2000-06-23" "2000-07-25" "2000-08-25" "2000-09-25" "2000-10-25" #> [11] "2000-11-24" "2000-12-25"# Imagine the garbage truck normally comes every Monday, but if # Monday is a state holiday, then it comes on Tuesday instead. on_labor_day <- on_first(on_wday("Mon"), within_given = "month") %>% only_occur(in_month("Sep")) on_christmas_day <- only_occur(in_month("Dec"), on_mday(25)) on_non_working_day <- on_weekend() %>% also_occur(on_labor_day) %>% also_occur(on_christmas_day) on_my_business_day <- dont_occur(on_non_working_day) on_normal_trash_day <- on_wday("Mon") on_trash_day <- on_normal_trash_day %>% roll_forward(to_schedule = on_my_business_day, .p = on_non_working_day) # A Monday in September happen(on_normal_trash_day, ymd("2019-09-09"))#> [1] TRUE#> [1] TRUE#> [1] TRUE#> [1] FALSE#> [1] FALSE#> [1] TRUE# Say that a trash inspector always comes the day after trash day, whatever # day that happens to be. on_inspection_day <- on_trash_day %>% roll_by(1, "day") # Inspector comes on a Tuesday in September happen(on_inspection_day, ymd("2019-09-10"))#> [1] TRUE#> [1] FALSE# Inspector doesn't come on the Tuesday after Labor Day Monday happen(on_inspection_day, ymd("2019-09-03"))#> [1] FALSE#> [1] TRUE