One disadvantage of supplying an array of holiday dates to the toCalendar method is that the array can only hold dates for a specific period. If you want to display a calendar outside of that period then you need to add the holidays for that year to the array or you will not be able to highlight the holidays.
Adding that one extra line to the toCalendar method to allow a function to be supplied instead of an array mostly resolves this problem. It doesn't completely resolve it because a function to calculate when the holidays should be in a given year will be dependent on those rules actually being followed in which holidays actually are declared for that year. Still we should be able to get reasonably close allowing for the fact that our function will not be able to identify any special public holidays added for just the one year and will also not be able to omit any holidays that for some reason doesn't occur in a specific year.
Most holidays follow a specific set of rules so that we should be able to apply those rules in order to be able to determine what specific date that holiday was on in a specified year. Before we start though we need to remember that public holidays are specific to a particular location and so in producing your list of holiday dates you need to have a location in mind (this applies even if you are simply creating a static array of dates). You will therefore need to write your own custom function that generates an array of public holidays for your selected location.
For the purpose of this example we'll consider the public holidays that generally apply in the state of New South Wales in Australia.
Holidays here either occur on a specific date or on a particular Monday in a specific month. With some of the holidays where the date itself falls on a weekend the following Monday is also a holiday.
Adding the specific dates to the array is straightforward - we simply take the supplied year and concatenate the month and day to give us our ccyy-mm-dd string. For those holidays that occur on a specific Monday we create a nextMonday function that takes a specified day of the month and if that isn't aMonday will return the day of the month following that date that is the next Monday. By supplying a start day of 1 we can retrieve the first Monday, 8 gives us the second Monday, 15 gives us the third Monday and 22 gives us the fourth Monday of the Month via this function. To test if a date is on a weekend we need simply test if the day of the week is 0 or 6. We can then set the following day to also be a holiday (or where that is still weekend we can set the day after to be a holiday).
So 1st January is the new year's day holiday and if that is on a weekend then Monday the 2nd or 3rd will also be a holiday. 26th January is the Australia Day holiday but in recent years an additional holiday has not been provided if it falls on a weekend. The Queen's birthday holiday is held in NSW on the second Monday in June. So we apply the nextMonday and isWeekend functions as appropriate to obtain all these dates.
That just leaves Easter as the one group of holiday dates that are not catered for by the two functions we already have. So we simply add one extra function that will determine when Easter Day is in the specific year and which returns the month and day as a two element array. Here we have three holiday dates since Good Friday is also a public holiday and so is the Monday following (to make up for Easter Day always being on the weekend).
So taking all that into account and setting up appropriate rules for generating all the holiday dates for the year and saving them to the array we end up with the following code that is specific to public holidays in NSW, Australia.
Well there is one extra line on the end of that code that isn't a part of the holidays function itself. The one problem with using a function to generate our array of holiday dates is that we would be regenerating the same dates over and over if we swap the calendar between months in the same year. To avoid this we memoize the function. What this does is to store the results from each call to the function and if the function is called again with the same parameters it simply returns the stored result from the prior call with those parameters rather than recalculating the same values. This only works where the same call always produces the same result - which is the case here. So by momoizing our function for creating an array of holiday dates for a specified year we end up with the function only ever being run once for any specified year no matter how many times that calendars from that year are displayed. All of the subsequent calendars will simply retrieve the array that was built when the function was called the first time the holidays for that year were requested.
This article written by Stephen Chapman, Felgall Pty Ltd.