Getting the Date when a Holiday occurs over the Years

Navigation:  Appendices > Macro Language > Built-in Functions > Holidays > Using Holidays Functions >

Getting the Date when a Holiday occurs over the Years

Previous pageReturn to chapter overviewNext page

Sometimes you need to use macro holidays functions to list the occurrence of a list of holidays over 2 or 3 years (often at the back of diaries).


You are then faced with the problem that a list of holidays may occur in a given order one year and in another the next year (because of holidays which move such as Easter). So you cannot list all the holidays of year one, then all the holidays of year 2, etc… Once you encounter a holiday ID, you must grab on to it and search for its occurrences over the range of year.


The code below shows , once you have a holiday ID, how to look for all occurrences of that holiday rule over a given number of years, even if this holiday occurs more than once in a year. Before reaching this block of code we assume that you have reached a particular holiday occurrence, possibly using FindHolidayOnDate. If that function returns true you know you have reached a holiday in the table, and the first two lines of code simply read the specs for this holiday. The main element we are interested in here is the holiday Id which we get with the call :


nCurHolId = GetHolIdFromTable()


We start a loop to cover the desired range of years using the following WHILE loop, although we could also have used a FOR loop.


while nYTemp <= nY3


We then initialize the table and look for an occurrence of our holiday in year nYTemp.



nCurHolDate = GetDateForHolidayId(nCurHolId, nYTemp)


Note how the next line makes our search totally general, we allow for the possibility that there are 0 occurrences (eg. Feb.29 holiday in 1999), or many occurrences (eg. Eid al Fitr in 2000) :


while nCurHolDate <> 0


The IF-ELSE-ENDIF loop that follows has more to do with pretty-printing, than with holidays tables. It is basically used to group multiple occurrences of a holiday in the same year.


// table is now positioned on record, get the info ASAP

sCurHol = GetHolNameFromTable()

nCurHolId = GetHolIdFromTable()

// now look for same holiday ID in years Y1, Y2 and Y3

nYTemp = nY1

sRESULT = ''

while nYTemp <= nY3

 Reset_GetDateForHolidayId() //init table

 nCurHolDate = GetDateForHolidayId(nCurHolId, nYTemp)

 sOneYear = ''

 while nCurHolDate <> 0 // there could be more than one

         // separate multiple dates for the SAME year with commas

         if sOneYear == ''

                 // usual case : holiday occurs once a year

                 sOneYear = EvalToken(nCurHolDate, sDateToken)


                 // same holiday occurs twice in same year        

                 sOneYear = sOneYear + ', ' + EvalToken(nCurHolDate,                                                                                                                                    ->> sDateToken))


         nCurHolDate = GetDateForHolidayId(nCurHolId, nYTemp)


 // make sure to increment nYTemp to avoid spinning your wheels

 nYTemp = nYTemp + 1

endwhile // loop Y1 .. Y3

// finally add the holiday name to the left

// and a soft return at the end of the line

sThreeYears = sCurHol + '&t' + sThreeYears

sRESULT = sRESULT + sThreeYears + '&s'


At the end of the "while NotEqualN" loop, we call GetDateForHolidayIdOfYear again to move to the next occurrence of the same holiday in the same year (if there is one). Once that loop ends, nYTemp is increased and the above loop is repeated for the next year.

Topic 108273 updated on 01-Nov-2016.
Topic URL: