Federal holiday calculations - *blush*

Devin Asay devin_asay at byu.edu
Tue Aug 12 12:50:37 EDT 2008


Sometimes looking at your old code is useful, because you see how it  
can be improved. When you send that piece of old code out to the world  
before really looking at it, it becomes a little embarrassing if you  
examine it later and find out that it was inefficient and buggy. In  
that spirit, I'd like to revise this function that I sent out  
yesterday. I discovered today, coincidentally, that it doesn't handle  
Daylight Savings Time changes very well. I revised and simplified it,  
and now that problem is solved. Here's the new getDateOfNthWeekday  
function:

# called by: generateHolidayEvents --, generateEventDateList
function getDateOfNthWeekday year,monthNum,dayOfWeek,nth
    -- find what day is 1st of month, convert to date items
    -- get difference between that day and the first occurrence of day  
of week you want
    -- add that difference and the number of needed weeks to the first  
day of month
    --   (by adding days to item 3 of date items)
    put line monthNum of the monthNames && 1 & ", " & year into  
firstDayOfMonth
    convert firstDayOfMonth to dateitems
    put item 7 of firstDayOfMonth into firstDayNum

    if firstDayNum < dayOfWeek then
       put dayOfWeek - firstDayNum into daysToAdd
    else if firstDayNum > dayOfWeek then
       put 7 -(firstDayNum - dayOfWeek) into daysToAdd
    else
       put 0 into daysToAdd
    end if
    put firstDayOfMonth into nthXDay
    add (daysToAdd + (nth - 1) * 7) to item 3 of nthXDay
    convert nthXDay to dateItems -- to force correction of date items
    return nthXDay
end getDateOfNthWeekday


On Aug 11, 2008, at 9:10 AM, Devin Asay wrote:

>
> 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
>
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your  
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution

Devin Asay
Humanities Technology and Research Support Center
Brigham Young University




More information about the use-livecode mailing list