[Chicago-talk] Calculating Holidays

Eric Ellington e.ellington at gmail.com
Fri Jul 13 07:58:32 PDT 2012


Richard,

I have done something similar to this before. Date::Manip can use config
files like Alan was showing, but your config for holidays can be more
complex. For example, here is a section from a Date::Manip config file I
made.

-----------DateManipConfig.txt

################################
# CONFIG VARIABLES
################################
# See Date::Manip man page for a description of all config variables.

# EraseHolidays        =
# PersonalCnf        = .DateManip.cnf
# PersonalCnfPath    = .:~
# Language        = English
# DateFormat        = US
# Must set time zone here for windows systems.
TZ            = CST
# ConvTZ        =
# Internal        = 0
# FirstDay        = 1
# WorkWeekBeg        = 1
# WorkWeekEnd        = 5
# WorkDay24Hr        = 0
# WorkDayBeg        = 08:00
# WorkDayEnd        = 17:00
# TomorrowFirst        = 1
# DeltaSigns        = 0
# Jan1Week1        = 0
# YYtoYYYY              = 89
# UpdateCurrTZ        = 0
# IntCharSet         = 0
# ForceDate        =

################################
# HOLIDAYS
################################
*HOLIDAY

# Make sure that the date part of every date can be followed by the year
# and be correctly parsed by ParseDate.

# The following holiday definitions are taken from pcal's config file.
# Some are commented out if these tend to be ordinary business days.

# For [system x] I only care about observed holidays.
# If a holiday falls on a Sat or Sun it is all the same to [system x]

1*1:0:1:0:0:0*NWD               = New Year's Day (observed)
3rd Monday in Jan        = Martin Luther King Day
third Monday in Feb             = Presidents' Day
last Monday in May              = Memorial Day
1*7:0:4:0:0:0*CWD               = Independence Day (observed)
1st Monday in Sep               = Labor Day
second Monday in Oct            = Columbus Day (observed)
1*11:0:11:0:0:0*CWD             = Veterans' Day (observed)
1*11:4:4:0:0:0                  = Thanksgiving
1*11:4:5:0:0:0                  = Day After Thanksgiving
1*12:0:25:0:0:0*CWD             = Christmas Day (observed)

# You can also use recurrences.

#1*0:0:0:0:0:0*EASTER        = Easter
#1*0:0:0:0:0:0*EASTER,PD5    = Good Friday


# Other complex holidays (date + delta, date - delta)

# first Monday in Nov + 1 day   = Election day
# The Friday after Thanksgiving is an unnamed holiday some places
# fourth Thu in Nov + 1 day     =

# State specific holidays (set for Florida)

# Alaska
#first Sat in March             = Iditarod starts

# Only for MA, ME
#3rd Monday in Apr              = Patriots' Day

# You can define specific holidays for specific years
# 1/5/1999                      = A one-year-only holiday
# 1st Monday in Sep 1998        = Another one.


-------------Code.pl
#Code.pl

use strict;
use Env;

use Data::Dump::Streamer;
use Date::Manip;
use Math::Round;
use DBI;

#datemanip config var and file
&Date_Init("GlobalCnf=DateManipConfig.txt");

-------snip----------
sub getDayType
  {
    my($d) = @_;
    # L - day is week day without a predceding or inpending day before a
holiday
    # VF - last working day before holiday
    # PR - first working day after holiday
    # F - hoilday/first day of a hoilday
    # RT - last day of holiday before next work day

    #print "\nDay:$d\n";

    #next day
    my $nd = DateCalc($d, "+ 1day");
    #day after next
    my $dan = DateCalc($nd, "+ 1day");
    #previous day
    my $pd = DateCalc($d, "- 1day");
    #day before previous day
    my $dbpd = DateCalc($pd, "- 1day");

    # if day is work day, and previous and next day is not a holiday
    if(Date_IsWorkDay($d) && Date_IsWorkDay($pd) && Date_IsWorkDay($nd))
      {
    return("L");
      }
    # if day is work day, and previous day is work day, and next day is
holiday
    if(Date_IsWorkDay($d) && Date_IsWorkDay($pd) && !Date_IsWorkDay($nd))
      {
    return("VF");
      }
    #if day is work day, and previous day was holiday, and next day is
workday
    if(Date_IsWorkDay($d) && !Date_IsWorkDay($pd) && Date_IsWorkDay($nd))
      {
    return("PR");
      }
    #if day is hoilday, and next day is holiday
    if(!Date_IsWorkDay($d) && !Date_IsWorkDay($nd))
      {
    return("F");
      }
    #if day is workday, and next day and previous day are holidays
    if(Date_IsWorkDay($d) && !Date_IsWorkDay($nd) && !Date_IsWorkDay($pd))
      {
    return("F");
      }
    #if day is hoiliday and next day is workday and day after next is
holiday
    if(!Date_IsWorkDay($d) && Date_IsWorkDay($nd) && !Date_IsWorkDay($dan))
      {
    return("F")
      }
    #if day is holiday, and next day is work day
    if(!Date_IsWorkDay($d) && Date_IsWorkDay($nd))
       {
     return("RT");
       }
    return();
  }

-------Snip---------

Hope this helps,

Eric

On Fri, Jul 13, 2012 at 8:25 AM, <richard at rushlogistics.com> wrote:

> Thanks very much for all the replys. Jay I think you are correct. What I
> was looking to do is to write a script that alerts me of upcoming holidays.
> From what I coming to understand the existing perl modules allow me to "do
> things" with dates. But as I gather I have to supply the dates. What I
> though might exist was a perl module that actually calculates holidays such
> that you ask it what day is thanksgiving and it returns the date. It looks
> like Joel's website does this but I was hoping to avoid depending on an
> outside source. Also Alan you code is great but I want something that
> actually crunches the numbers and calculates the dates.
>
> I was on my way to doing this when I started to wonder if someone had
> already done it and put it in a perl module. I think I will resume the task
> of writing something on from scratch.
>
> Thanks again for all of the insightful and supportive replys.
>
> Richard
>
>
> El Jul 12, 2012, a las 7:53 PM, Jay Strauss <me at heyjay.com> escribió:
>
> > Richard, I get the feeling you are looking for a module with built in
> > knowledge of the dates of various holidays.  I'm not sure if such a
> > thing exists. (admittedly I haven't searched)
> >
> > The docs for DateTime::Event::Holiday::US; seem like it just a module
> > for naming dates and proving a wrapper to the underlying  modules
> >
> > I think you need a module that let's you supply a calendar (dates with
> > names) and then gives you functions for navigating that calendar
> >
> >
> > Thanks
> > Jay
> >
> > Sent from my iPhone
> >
> > On Jul 12, 2012, at 12:34 PM, Richard Reina <richard at rushlogistics.com>
> wrote:
> >
> >>
> >> Hi Mike,
> >>
> >> Thank you very much for the suggestion.  With some difficulty I was
> able to get the module installed. However, I am unclear on how it works.
>  The example given in the docs:
> >>
> >> use DateTime::Event::Holiday::US;
> >>
> >> my $thanksgiving = DateTime::Event::Holiday::US::holiday(
> 'Thanksgiving' );
> >>
> >>
> >> gives me a hash reference which when I try to dereference with:
> >>
> >> my %hash =  %$thanksgiving;
> >>
> >> foreach my $k (keys %hash) {
> >>       print "$k: $hash{$k}\n";
> >>   }
> >>
> >>
> >> gives me:
> >>
> >> as_ical: ARRAY(0x1e63190)
> >> set: Too complex
> >>
> >> I was hoping it might give me the date for Thanksgiving.
> >>
> >> If any one can give me an idea as to what I am doing wrong in trying to
> use this module I would really appreciate it.
> >>
> >> Thanks,
> >>
> >> Richard
> >>
> >>
> >>
> >>>
> >>> Mike Fragassi <mikefrag at gmail.com> wrote:
> >>
> >>> Try DateTime::Event::Holiday::US.
> >>
> >>> On Jul 3, 2012 3:46 PM, <richard at rushlogistics.com> wrote:
> >>>
> >>> I am lost trying to write a simple script that tells
> >>> me how many days until the next approaching holiday.
> >>> has anyone ever done anything similar or know of anything
> >>> helpful. I've looked at Date::Manip but that still
> >>> leaves me with the cumbersome task of finding out
> >>> which holiday is the next one from a given date.
> >>>
> >>> Any help would be appreciated as not solving it will
> >>> keep me preoccupied all through the holiday.
> >>>
> >>> Thanks
> >>>
> >>> Richard
> >>> _______________________________________________
> >>> Chicago-talk mailing list
> >>> Chicago-talk at pm.org
> >>> http://mail.pm.org/mailman/listinfo/chicago-talk
> >>
> >>
> >> --
> >> Richard Reina
> >> Rush Logistics, Inc.
> >> Watch our 3 minute movie:
> >> http://www.rushlogistics.com/movie
> >>
> >> _______________________________________________
> >> Chicago-talk mailing list
> >> Chicago-talk at pm.org
> >> http://mail.pm.org/mailman/listinfo/chicago-talk
> > _______________________________________________
> > Chicago-talk mailing list
> > Chicago-talk at pm.org
> > http://mail.pm.org/mailman/listinfo/chicago-talk
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk
>



-- 
Eric Ellington
e.ellington at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/chicago-talk/attachments/20120713/f7d061a5/attachment-0001.html>


More information about the Chicago-talk mailing list