05
Sun, May
6 New Articles

TechTip: Considerations for Holiday Determination

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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
BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: