Federal holiday calculations
Devin Asay
devin_asay at byu.edu
Mon Aug 11 11:10:16 EDT 2008
On Aug 10, 2008, at 2:36 PM, Shari wrote:
> I found a Date/Time stack at http://www.troz.net/Rev/libraries.php
> for determining what day Easter falls on.
>
> Does anyone know of a stack or library that calculates the day of the
> month for U.S. major holidays for any given year?
>
> Such as thanksgiving(2011) returns the date Thanksgiving falls on in
> 2011
Hi, Shari.
I wrote a handler to do this, based on information I found at the U.S.
Naval Observatory web site. The hard part is figuring out the actual
date for holidays like Memorial Day, AKA "the last Monday in May." The
calling handler supplies the input in integers, and the function
returns the date that the holiday falls on as date items. Then in the
calling handler, you do whatever you need to do with that information.
I use it in a scheduling application, so some aspects of the code,
such as actual days off in cases where holidays fall on weekends, are
specific to my app. But you may find it helpful in adapting to your
own situation. Beware line wraps!
## Here are the basic formulas for computing U.S. holidays:
# FIXED DATE HOLIDAYS
## New Year's Holiday: January 1 if on Sunday > Monday Jan 2nd
off
## if on Monday > Monday Jan 1st
off
## if on Tuesday, Wed, Fri > 31st
and 1st off
## if on Thurs > 1st and 2nd off
## if on Sat > Dec 31st off
## Independence Day: July 4 if on Sat > Friday 3rd off
## if on Sun > Mon 5th off
## Christmas Day: if on Sunday > Monday 26th off
## if on Monday > Monday 25th off
## if on Tuesday, Wed, Fri > 24th
and 25th off
## if on Thurs > 25th and 26th off
## if on Sat > 31st off
# HOLIDAYS DETERMINED BY DAY OF WEEK AND MONTH
## M.L. King Birthday: 3rd Monday in January
## Presidents' Day: 3rd Monday in February
## Memorial Day: Last Monday in May
## Labor Day: First Monday in September
## Thanksgiving: Fourth Thursday and following Friday in November
# called by: generateHolidayEvents
# This function takes four integers as parameters:
## the year, month, day of week, and an integer representing the
ordinal.
## For example, if I want to find out the date of the fourth Thursday
of November, 2008,
## I would call:
## put getDateOfNthWeekday(2008,11,5,4) into tDate
## The function would return
## 2008,11,26,23,0,0,4
function getDateOfNthWeekday year,monthNum,dayOfWeek,nth
-- find what day is 1st of month
-- get difference between that day and next monday
-- add that difference in seconds to first day
-- add number of needed weeks in seconds to get to nth monday
put line monthNum of the monthNames && 1 & ", " & year into
firstDayOfMonth
convert firstDayOfMonth to dateitems
put item 7 of firstDayOfMonth into theDayNum
convert firstDayOfMonth to seconds
--add 60*60 to firstDayOfMonth -- compensate for a quirk in
converting between dateitems and seconds
put 60*60*24 into scndsInDay
if theDayNum < dayOfWeek then
put dayOfWeek - theDayNum into daysToAdd
put firstDayOfMonth + (daysToAdd * scndsInDay) into firstXday --
X refers to the weekday we're interested in
else if theDayNum > dayOfWeek then
put 7 -(theDayNum - dayOfWeek) into daysToAdd
put firstDayOfMonth + (daysToAdd * scndsInDay) into firstXday
else
put firstDayOfMonth into firstXday
end if
put firstXday + ((nth - 1) * 7 * scndsInDay) into nthXDay
convert nthXDay to dateItems
return nthXDay
end getDateOfNthWeekday
HTH
Devin
Devin Asay
Humanities Technology and Research Support Center
Brigham Young University
More information about the use-livecode
mailing list