Determining if a particular date is a holiday is a global
problem that has several solutions among iSeries shops. Some shops set up a
calendar file of some sort, keyed by date, with a field flagging it as a
holiday. This is OK, as long as you remember to generate it every year. I prefer
a program that receives a date, of any year, and determines if it is a holiday.
With an algorithmic program, you would not need to remember to generate new
records in a calendar file every year, and you wouldn't use up disk
space.
There are several things to consider when determining if a date is
a holiday:
- Some holidays are dated--that is, they occur on a specific date,
such as 12/25, 11/11, 1/1 or 7/4. Sounds easy enough, but if they fall on the
weekend, your company may not observe them on their actual date. Then,
you have to determine when your company observes the holiday during the work
week.
- Other holidays float--that is, they occur on the nth xxxday of a month, such
as the third Monday of January, the fourth Thursday of November, or, even
trickier, the last Monday of May.
- Then there's Good Friday and Easter, but even that can be determined
algorithmically.
I have marveled at the lengths to which some
code goes to determine if a day is the nth xxxday of the month, sometimes
involving complicated comparison and counting calculations. I have seen similar
complex coding for determining observed holidays. I have discovered that such
complicated processing is not necessary. To determine if a date is a holiday,
you need only two pieces of information:
1. The date itself (specifically
the month and day)
2. The day of the week
Determining the day of the
week in RPG is simple:
weekDay = %rem(%diff(yourDate:d'0001-01-01':*D):7) + 1;
This produces 1 for Monday through 7 for Sunday.
For dated
holidays like 7/4, 11/11 and 12/25, the actual date is the holiday. If that
holiday falls during the work week, you're set. But if it falls on the weekend,
you have everything you need to determine when the holiday might be observed by
your company during the work week. Normally, if a holiday falls on Saturday, it
is observed on Friday; likewise, a Sunday or Tuesday holiday is observed on
Monday. You can then deduce, for example, that Friday, 7/3, or Monday, 7/5, and
so on, could be observed holidays.
Floating holidays are just as easy to
determine. There is one simple fact to keep in mind when determining if a date
is the nth xxxday, and this fact is easy to overlook: There are only seven
xxxdays (Monday through Sunday). It follows then that the first xxxday of the
month will be on one of the first seven days. We can now determine easy ranges
of seven days that we need.
Nth Range
|
Days
|
Use for...
|
1st xxxday
|
1 to 7
|
Labor Day (1st Monday in September)
|
2nd xxxday
|
8 to 14
|
Columbus Day (2nd Monday in October)
|
3rd xxxday
|
15 to 21
|
Martin Luther King Day (3rd Monday in January) President's
Day (3rd Monday in February)
|
4th xxxday
|
22 to 28
|
Thanksgiving (4th Thursday in November)
|
5th xxxday
|
29 to 31
|
|
Last xxxday
|
25 to 31
|
Memorial Day (Last Monday in May)
|
If you have a September Monday between 9/1 and 9/7, then you have Labor
Day. If you have a November Thursday between 11/22 and 11/28, it is
Thanksgiving. Likewise, a May Monday between 5/25 and 5/31 is Memorial Day.
Other useful ranges are 11/23 through 11/29 to determine the Friday
after Thanksgiving (that Friday will follow the fourth Thursday), and 11/2
through 11/8 to determine Election Day (the first Tuesday in November on or
after 11/2).
As for Easter and Good Friday, there is an accepted
algorithm called the Mallen
Algorithm. It is easy to implement in RPG, COBOL, or Java. Remember also
that Easter will always fall somewhere between 3/22 and 4/25.
Now, what
about calculating a holiday date? In other words, how would you code a function
to answer "When is Thanksgiving in 2002?"
For dated holidays, that's
easy. You already know the answer. "When is Christmas in 1999?" 12/25/1999, of
course. If this falls on the weekend or even the middle of the week, when might
your company observe it? This involves some work, but not much. Simply
determine the day of week of the dated holiday. If it is 6 (Saturday), subtract
a day from the date. For 7 (Sunday), add a day. In 1999, Christmas is on
Saturday, so it is observed Friday, 12/24/1999.
For the floating
holidays, it's not that difficult, either. We need the following
information:
- The month and year. We know these already.
- x: The xxxday we are looking for. For Thanksgiving, x would be 4
(Thursday)
- d: The first day of the range. For Thanksgiving, d would be 22, because the
fourth Thursday falls somewhere between the 22nd and the
28th.
- n: The day of the week that d falls on, for the given month and
year.
Now, to determine h, the day the holiday falls on, we use
the following formula:
if (n <= x);
h = d + (x - n);
else
h = d + ((x + 7) - n);
endif;
So for Thanksgiving in 2003, we have:
- Month and Year: 11/2003
- x = 4
- d = 22
- n = Weekday of 11/22/2003 = 6 (Saturday)
h = 22 + ((4 + 7)
- 6)
h = 22 + (11 - 6)
h = 22 + 5
h =
27
Thanksgiving in 2003 is 11/27.
The sample program below
receives a date (numeric CYMD) and determines if it is a holiday. If it is, it
returns a code indicating which holiday as well as a text description of the
holiday.
Doug Eckersley is the iSeries
programmer with a homebuilder in Columbus, Ohio. He has been programming on the
AS/400 for ten years. He is certified by IBM. He can be reached by email
at This email address is being protected from spambots. You need JavaScript enabled to view it..
*****************************************************************
* *
* Program - UTRSHD00 *
* Module - None Release - ?.?? *
* *
* Narrative : Determine if a passed in date is a holiday. *
* *
* Pass in an instruction in outCode to check for *
* certain holidays: *
* *
* *MAJ: Check for major holidays only: *
* New Year, Memorial, Independence, *
* Labor, Thanksgiving, Christmas. *
* *
* *FED: Check for *MAJ holidays and also: *
* Martin Luther King, Presidents', *
* Columbus and Veterans'. *
* *
* xxxx: Check for the specific holiday. *
* *
*****************************************************************
********------------ Program Maintenance Log ------------********
*****************************************************************
* Personnel | Date | Comments/Description *
*------------|----------|---------------------------------------*
* MDE | 06/18/02 | Program Written *
* | | *
*****************************************************************
D*=====================================================================
D* Parameters
D*=====================================================================
D inDate S 8S 0
D outCode S 4A
D outDesc S 40A
D
D*=====================================================================
D* Switches
D*=====================================================================
D*Switches DS
D
D*=====================================================================
D* Other Work
D*=====================================================================
D Instruction S LIKE(outCode)
D
D DS
D hx 2S 0
D Holidays 44A DIM(15) CTDATA PERRCD(1)
D hCode 4A OVERLAY(Holidays:*NEXT)
D hDesc 40A OVERLAY(Holidays:*NEXT)
D
D DS
D wData 1 9S 0
D wDate 1 8S 0
D wY 1 4S 0
D wDay 5 9S 0
D wMD 5 8S 0
D wM 5 6S 0
D wD 7 8S 0
D wW 9 9S 0
D
D DS
D cDate D DATFMT(*ISO)
D Dur 9S 0
D
D*---------------------------------------------------------------------
D* Good Friday Work Area - Mallen Algorithm
D*---------------------------------------------------------------------
D DS
D gf 1 4S 0
D gfM 1 2S 0
D gfD 3 4S 0
D
D FirstDig S 9S 0
D Remain19 S 9S 0
D temp S 9S 0
D tA S 9S 0
D tB S 9S 0
D tC S 9S 0
D tD S 9S 0
D tE S 9S 0
C**********************************************************************
C**********************************************************************
C *entry plist
C parm inDate
C parm outCode
C parm outDesc
C
C exsr $inzsr
C exsr $Main
C
C eval *inlr = *on
C*=====================================================================
C $inzsr begsr
C*=====================================================================
C eval Instruction = outCode
C
C eval wDate = inDate
C clear outCode
C clear outDesc
C
C xinzsr endsr
C*=====================================================================
C $Main begsr
C*=====================================================================
C*---------------------------------------------------------------------
C* Make Date a Date Field
C*---------------------------------------------------------------------
C *ISO test(de) wDate
C if %error
C leavesr
C endif
C *ISO move wDate cDate
C
C*---------------------------------------------------------------------
C* Get Day of Week: 1=Monday...7=Sunday
C*---------------------------------------------------------------------
C cDate SUBDUR d'0001-01-01' Dur:*D
C eval wW = %rem(Dur:7) + 1
C
C* Skip weekends
C if wW >= 6
C leavesr
C endif
C
C*---------------------------------------------------------------------
C* Determine if this is a holiday
C*---------------------------------------------------------------------
C exsr $Holiday
C
C*---------------------------------------------------------------------
C* Load description
C*---------------------------------------------------------------------
C if hx = *zero
C leavesr
C endif
C
C if (Instruction <> *blanks)
C and (%subst(Instruction:1:1) <> '*')
C and (Instruction <> hCode(hx))
C leavesr
C endif
C
C eval outCode = hCode(hx)
C eval outDesc = hDesc(hx)
C
C xMain endsr
C*=====================================================================
C $Holiday begsr
C*=====================================================================
C eval hx = *zero
C
C* Major Holidays first
C*---------------------------------------------------------------------
C* New Years - Jan 1
C*---------------------------------------------------------------------
C if (wMD = 0101)
C or (wDay = 01021)
C or (wDay = 12315)
C eval hx = 1
C leavesr
C endif
C
C*---------------------------------------------------------------------
C* Memorial Day - Last Mon in May
C*---------------------------------------------------------------------
C if (wW = 1)
C and (wMD >= 0525)
C and (wMD <= 0531)
C eval hx = 2
C leavesr
C endif
C
C*---------------------------------------------------------------------
C* Independence Day - Jul 4
C*---------------------------------------------------------------------
C if (wMD = 0704)
C or (wDay = 07035)
C or (wDay = 07051)
C eval hx = 3
C leavesr
C endif
C
C*---------------------------------------------------------------------
C* Labor Day - 1st Mon in Sep
C*---------------------------------------------------------------------
C if (wW = 1)
C and (wMD >= 0901)
C and (wMD <= 0907)
C eval hx = 4
C leavesr
C endif
C
C*---------------------------------------------------------------------
C* Thanksgiving - 4th Thu in Nov
C*---------------------------------------------------------------------
C if (wW = 4)
C and (wMD >= 1122)
C and (wMD <= 1128)
C eval hx = 5
C leavesr
C endif
C
C*---------------------------------------------------------------------
C* Christmas - Dec 25
C*---------------------------------------------------------------------
C if (wMD = 1225)
C or (wDay = 12245)
C or (wDay = 12261)
C eval hx = 6
C leavesr
C endif
C
C*---------------------------------------------------------------------
C*---------------------------------------------------------------------
C* If looking for Major Holiday only, leave now
C*---------------------------------------------------------------------
C*---------------------------------------------------------------------
C if Instruction = '*MAJ'
C leavesr
C endif
C
C*---------------------------------------------------------------------
C* MLK - 3rd Mon in Jan
C*---------------------------------------------------------------------
C if (wW = 1)
C and (wMD >= 0115)
C and (wMD <= 0121)
C eval hx = 7
C leavesr
C endif
C
C*---------------------------------------------------------------------
C* Presidents' Day - 3rd Mon in Feb
C*---------------------------------------------------------------------
C if (wW = 1)
C and (wMD >= 0215)
C and (wMD <= 0221)
C eval hx = 8
C leavesr
C endif
C
C*---------------------------------------------------------------------
C* Columbus Day - 2nd Mon in Oct
C*---------------------------------------------------------------------
C if (wW = 1)
C and (wMD >= 1008)
C and (wMD <= 1014)
C eval hx = 9
C leavesr
C endif
C
C*---------------------------------------------------------------------
C* Veterans' Day - Nov 11
C*---------------------------------------------------------------------
C if (wMD = 1111)
C or (wDay = 11105)
C or (wDay = 11121)
C eval hx = 10
C leavesr
C endif
C
C*---------------------------------------------------------------------
C*---------------------------------------------------------------------
C* If looking for Federal Holiday only, leave now
C*---------------------------------------------------------------------
C*---------------------------------------------------------------------
C if Instruction = '*FED'
C leavesr
C endif
C
C*---------------------------------------------------------------------
C* Thanksgiving Friday - Fri after Thanksgiving
C*---------------------------------------------------------------------
C if (wW = 5)
C and (wMD >= 1123)
C and (wMD <= 1129)
C eval hx = 11
C leavesr
C endif
C
C*---------------------------------------------------------------------
C* Election Day - 1st Tue on/after Nov 2
C*---------------------------------------------------------------------
C if (wW = 2)
C and (wMD >= 1102)
C and (wMD <= 1108)
C eval hx = 12
C leavesr
C endif
C
C*---------------------------------------------------------------------
C* Good Friday - Special Calculation
C*---------------------------------------------------------------------
C exsr $GoodFriday
C if hx > *zero
C leavesr
C endif
C
C xHoliday endsr
C*=====================================================================
C $GoodFriday begsr
C* This subroutine calculates the date of Good Friday using the
C* Mallen Algorithm of Easter date calculation (Good Friday is
C* two days before Easter).
C*
C* See www.assa.org.au/edm.html for information regarding
C* the Mallen Algorithm.
C*=====================================================================
C*---------------------------------------------------------------------
C* Is passed in date in range for Good Friday?
C*---------------------------------------------------------------------
C if (wMD < 0320)
C or (wMD > 0423)
C leavesr
C endif
C
C*---------------------------------------------------------------------
C* Calculate Paschal Full Moon Date
C*---------------------------------------------------------------------
C eval FirstDig = %div(wY:100)
C eval Remain19 = %rem(wY:19)
C
C eval temp = %div((FirstDig - 15):2)
C + 202 - (11 * Remain19)
C
C select
C when (FirstDig = 21)
C or (FirstDig = 24)
C or (FirstDig = 25)
C or (FirstDig = 27)
C or (FirstDig = 28)
C or (FirstDig = 29)
C or (FirstDig = 30)
C or (FirstDig = 31)
C or (FirstDig = 32)
C or (FirstDig = 34)
C or (FirstDig = 35)
C or (FirstDig = 38)
C eval temp = temp - 1
C
C when (FirstDig = 33)
C or (FirstDig = 36)
C or (FirstDig = 37)
C or (FirstDig = 39)
C or (FirstDig = 40)
C eval temp = temp - 2
C endsl
C eval temp = %rem(temp:30)
C
C eval tA = temp + 21
C if temp = 29
C eval tA = tA - 1
C endif
C if (temp = 28)
C and (Remain19 > 10)
C eval tA = tA - 1
C endif
C
C*---------------------------------------------------------------------
C* Get Easter
C*---------------------------------------------------------------------
C eval tB = %rem((tA - 19):7)
C
C eval tC = %rem((40 - FirstDig):4)
C if tC = 3
C eval tC = tC + 1
C endif
C if tC > 1
C eval tC = tC + 1
C endif
C
C eval temp = %rem(wY:100)
C eval tD = %rem(temp+%div(temp:4):7)
C
C eval tE = %rem((20-tB-tC-tD):7) + 1
C
C eval gfD = tA + tE
C
C* Two days earlier - Good Friday:
C eval gfD = gfD - 2
C
C eval gfM = 3
C if gfD > 31
C eval gfD = gfD - 31
C eval gfM = 4
C endif
C
C*---------------------------------------------------------------------
C* Now compare to passed in date.
C*
C* Because of the special calc for Good Friday, we want to do it
C* last. Therefore, it's entry in the table will always be the
C* last entry.
C*---------------------------------------------------------------------
C if wMD = gf
C eval hx = %elem(Holidays)
C endif
C
C xGoodFriday endsr
**CTDATA Holidays
NY New Year's Day
MEM Memorial Day
ID4 Independence Day
LAB Labor Day
THK Thanksgiving
XMASChristmas
MLK Martin Luther King Day
PRESPresidents' Day
COL Columbus Day
VET Veterans' Day
THKFThanksgiving Friday
ELECElection Day
****
****
GF Good Friday
|
|
LATEST COMMENTS
MC Press Online