on_weekends <- weekly() %>%
recur_on_weekends()
# New Year's Day, observed on the nearest weekday if it falls on a weekend
on_new_years <- hol_new_years_day() %>%
hol_observe(on_weekends, adj_nearest)
# Christmas, observed on the nearest weekday if it falls on a weekend
on_christmas <- hol_christmas() %>%
hol_observe(on_weekends, adj_nearest)
cal <- rcalendar(on_new_years, on_christmas)
cal
# In 2010, Christmas fell on a Saturday and was adjusted backwards
cal_events(cal, year = 2010)
# In 2011, New Year's fell on a Saturday and was adjusted backwards.
# Note that the returned date is in 2010, even though we requested holidays
# for 2011, because most people would consider the actual New Year's date of
# 2011-01-01 part of the 2011 set of holidays, even though it was observed in
# 2010.
cal_events(cal, year = 2011)
# If you want to filter by the observed date, set `observed = TRUE`, which
# will move the New Year's Day that was observed in 2010 to the 2010 result
cal_events(cal, year = 2010, observed = TRUE)
cal_events(cal, year = 2011, observed = TRUE)
Run the code above in your browser using DataLab