` Printed Icetips Article

Icetips Article



Par2: Federal Holiday (U.S.) Function
2001-09-24 -- Marty Mohr
 
Usage: IF Holiday(date) ...

You need two procedures:

1) Holiday PROCEDURE( pDate LONG )

Holiday LONG,DIM(10)

  CODE
  SetFederalHolidays(YEAR(pDate),Holiday[])
  IF
INLIST(pDate,Holiday[1],Holiday[2],Holiday[3],Holiday[4],Holiday[5],Holiday[
6],Holiday[7],Holiday[8],Holiday[9],Holiday[10])
    RETURN TRUE
  ELSE
    RETURN FALSE
  END


2) SetFederalHolidays   PROCEDURE  ( pYear LONG , pHoliday *LONG[])      !
Declare Procedure

DayOfWeek LONG

  CODE

  !New Year's Day - January 1st
  pHoliday[1] = DATE(1,1,pYear)

  !Martin Luther King Day - Third Monday in January
  DayOfWeek   = DATE(1,1,pYear) % 7
  pHoliday[2] = DATE(1,21-((DayOfWeek + 5) % 7),pYear)

  !President's Day - Third Monday in February
  DayOfWeek   = DATE(2,1,pYear) % 7
  pHoliday[3] = DATE(2,21-((DayOfWeek + 5) % 7),pYear)

  !Memorial Day (Observed) - Last Monday in May
  DayOfWeek   = (DATE(6,1,pYear)-1) % 7
  pHoliday[4] = DATE(5,31-((DayOfWeek + 6) % 7),pYear)

  !Independence Day - July 4th
  pHoliday[5] = DATE(7,4,pYear)

  !Labor Day - First Monday in September
  DayOfWeek   = DATE(9,1,pYear) % 7
  pHoliday[6] = DATE(9,7-((DayOfWeek + 5) % 7),pYear)

  !Columbus Day - Second Monday in October
  DayOfWeek   = DATE(10,1,pYear) % 7
  pHoliday[7] = DATE(10,14-((DayOfWeek + 5) % 7),pYear)

  !Veterans Day - November 11th
  pHoliday[8] = DATE(11,11,pYear)

  !Thanksgiving - Fourth Thursday in November
  DayOfWeek   = DATE(11,1,pYear) % 7
  pHoliday[9] = DATE(11,28-((DayOfWeek + 2) % 7),pYear)

  !Christmas - December 25th
  pHoliday[10] = DATE(12,25,pYear)

Obviously you can expand it for more "holidays" than those listed.

Carl Barnes adds:
I think an Array would be a lot faster than a QUEUE, it would be much
smaller and simpler, just 40 bytes. I proved that in the ClaMag Challenge
Parse Test where my array based solution beat the next fastest by twice.

I probably would do this without any Array or Queue and a Big CASE. In many
ways the code is much simpler and faster as just the current date can be
analyzed to figure out what Nth Day-of-week it is, etc. I tested this code
and it works.

Holiday PROCEDURE(LONG pDate)

Day    LONG,AUTO
Dow     LONG,AUTO
DowNum  LONG,AUTO
Holiday    BYTE(0)

Dows    ITEMIZE(0)
Sunday          EQUATE
Monday          EQUATE
Tuesday         EQUATE
Wednesday       EQUATE
Thursday        EQUATE
Friday          EQUATE
Saturday        EQUATE
        END

  CODE
  Day   = DAY(pDate)
  Dow    = pDate % 7
  DowNum = (Day-1) / 7 + 1   !1st,2nd,3rd DOW etc

  CASE MONTH(pDate)
  OF 1
        IF Day=01 THEN Holiday=True.    !1/1 New years Day
        IF Day=20 THEN Holiday=True.    !20 jan - inauguration day
        IF Dow=Monday AND DowNum=3 THEN Holiday=True.     !Martin Luther
King Day - Third Monday in January
  OF 2
        IF Dow=Monday AND DowNum=3 THEN Holiday=True.     !President's Day -
Third Monday in February
  OF 6
        IF Day=14 THEN Holiday=True.                     !14 june - flag day
        IF Dow=Monday AND Day+7>31 THEN Holiday=True.    !Memorial Day
(Observed) - Last Monday in May
  OF 7
        IF Day=4 THEN Holiday=True.                      !7/4 July 4th
  OF 9
        IF Dow=Monday AND DowNum=1 THEN Holiday=True.     !Labor Day - First
Monday in September
  OF 10
        IF Dow=Monday AND DowNum=2 THEN Holiday=True.     !Columbus Day -
Second Monday in October
  OF 11
        IF Day=11 THEN Holiday=True.                     !Veterans Day -
November 11th
        IF Dow=Thursday AND DowNum=4 THEN Holiday=True.   !Thanksgiving -
Fourth Thursday in November
  OF 12
        IF Day=25 THEN Holiday=True.                     !Christmas -
December 25th
  END

  IF Holiday AND ~INRANGE(Day,Monday,Friday) THEN Holiday=2.   !Return 2 if
on a Weekend

  RETURN( Holiday )



Printed November 23, 2024, 9:19 pm
This article has been viewed/printed 35243 times.