almanac provides tools for working with recurrence rules, the fundamental building blocks used to identify calendar “events”, such as weekends or holidays.
Install the released version of almanac from CRAN with:
Install the development version from GitHub with:
# install.packages("remotes") remotes::install_github("DavisVaughan/almanac")
Constructing recurrence rules looks like this:
# Thanksgiving = "The fourth Thursday in November" on_thanksgiving <- yearly() %>% recur_on_ymonth("November") %>% recur_on_wday("Thursday", nth = 4) on_thanksgiving #> <rrule[yearly / 1900-01-01 / 2100-01-01]> #> - ymonth: Nov #> - wday: Thu
After constructing a recurrence rule, it can be used to generate dates that are in the “event set”. For example, you can search for all Thanksgivings between 2000-2006.
alma_search("2000-01-01", "2006-12-31", on_thanksgiving) #>  "2000-11-23" "2001-11-22" "2002-11-28" "2003-11-27" "2004-11-25" #>  "2005-11-24" "2006-11-23"
Determine if a particular date is a part of the event set with
You can also shift an existing sequence of dates, “stepping over” dates that are part of the event set.
There is an additional “stepper” object you can create for more intuitive stepping. Combine it with
%s+% to perform the same step done by
alma_step(). Create a stepper function with
stepper(), and then use it by supplying the number of days to step.
step_over_thanksgiving <- stepper(on_thanksgiving) wednesday_before_thanksgiving %s+% step_over_thanksgiving(2) #>  "2000-11-25"
The above example just scratches the surface of what almanac can do. Practically speaking, you’ll probably have multiple holidays and events that you’d like to combine into one big recurrence object. This is known as a recurrence bundle.
This example creates recurrence rules for weekends and Christmas, and bundles them together along with the Thanksgiving rule in such a way that we get the union of the underlying event sets.
on_weekends <- weekly() %>% recur_on_weekends() on_christmas <- yearly() %>% recur_on_mday(25) %>% recur_on_ymonth("Dec") bundle <- runion() %>% add_rschedule(on_weekends) %>% add_rschedule(on_christmas) %>% add_rschedule(on_thanksgiving) bundle #> <runion[3 rschedules / 0 rdates / 0 exdates]>
We can create a stepper that steps over all of the events in the bundle. If these two holidays were the only ones that your company celebrated, the stepper could be viewed as a way to step forward by a “business day”.
For example, Christmas was on a Monday in 2006. If you wanted to step 1 business day forward from the Friday before Christmas, you’d probably like it to step over the weekend and the Christmas Monday to Tuesday. The
bundle lets you do exactly that!
View the vignettes on the website to learn more about how to use almanac.
almanac has developed as a composite of ideas from multiple different libraries.
The date shifting / adjusting functions are modeled after similar functions in QuantLib.
The author of gs, James Laird-Smith, has been a great collaborator as we have bounced ideas off of each other. gs attempts to solve a similar problem, but with a slightly different implementation.