Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date::Calendar::Profile functions to calc holidays 1

Status
Not open for further replies.

MrSm1th

MIS
Jun 3, 2005
6
US
I'm using Date::Calendar::profile to calcualate holidays; but I don't understand how to use the functions to calc memorial day or anyother holiday for that matter.

Here's a sample of my code :

use Strict;
use Win32::process;
use Date::Calc qw(Delta_Days Today Nth_Weekday_of_Month_Year Day_of_Week
Day_of_Week_to_Text Date_to_Text_Long Month_to_Text Date_to_Days);
use Env;
use Date::Calendar::profiles qw($Profiles);
use Date::Calendar;
$calendar_US_CA = Date::Calendar->new($Profiles->{'US-CA'});

($year,$month,$day) = Today();
@names = $calendar->labels();
@holidays = $calendar->labels();
$holidays = $calendar->labels();

my ($sec,$min,$hour,$mday,$mon,$year,
$wday,$yday,$isdst) = localtime time;

$week = int(($day + Day_of_Week($year,$month,1) - 2) / 7) + 1;

$dow = Day_of_Week($year,$month,$day);

$DayOfWeek = Day_of_Week_to_Text($dow);
 
Hi

There is a very good documenteation:


All holidays are defined in the module "profile.pm". in case that you need to check the dates for an specific area...

An example:


Getting all working days:

#!/usr/bin/perl -w


use strict;
use Date::Calendar::profiles qw($Profiles);
use Date::Calendar;


my $year_to_display = 2005;


# use common US holidays
my $cal = Date::Calendar->new($Profiles->{'US'});


# get first business day of year
my $date = $cal->add_delta_workdays($year_to_display-1, 12, 31, 1);


# current date
my ($year, $month, $day) = $date->date;


while ($year_to_display == $year) {
# print date
printf "You have to work: %d-%02d-%02d\n", $year, $month, $day;
# increment date
$date = $cal->add_delta_workdays($date, 1);
($year, $month, $day) = $date->date;



}


dmazzini
GSM System and Telecomm Consultant
 
Calculating July 4:

use Date::Calc::Object qw:)all);

Date::Calc->date_format(3);

$date = 0;
$holiday_day = "04-07-2005";
$date = Date::Calc->new( Decode_Date_EU( $holiday_day ) );
print "Holiday is $date\n";


dmazzini
GSM System and Telecomm Consultant

 
Thanks for the reply! I still have a question. The link you referenced I have already been to and I do realize that the profiles.pm has a list of holidays included. I'm just not sure how to utilize it to calculate memorial day, specifically. I need to know every year - when the day before memorial occurs, so I can stop a script from executing. I'm still a novice when it comes to perl, so I apologize if this is a stupid question..

Thanks
 

use Date::Calc qw:)all);
use Date::Calendar;

$MyHolidays =
{
"Independence Day" => "04.07."

};

@MyYears = (1998,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010);

$calendar = Date::Calendar->new( $MyHolidays );



foreach $yearselected (@MyYears) {
$calendar->year( $yearselected );
@list = $calendar->search( "Independence Day" );

}

print '|' . '=' x 70 . "|\n";
foreach $date (@list){

@labels = $calendar->labels( $date );
$dow = shift(@labels);

foreach $person (@labels) {
printf
(
" Independence Day year %4s %3.3s %2d-%3.3s-%4d%s\n",
$date->year(),
$dow,
$date->day(),
Month_to_Text($date->month()),
$date->year(),
);
$anio=$date->year();
$dia= $date->day();
$mes= $date->month();
my ($yesteryear,$yestermonth,$yesterday) = Add_Delta_Days($anio,$mes,$dia, -1);
$yestermonth= Month_to_Text($yestermonth);
@previousday= $calendar->labels( $date-1 );
$previousday= shift(@previousday);
print " A day before independence day year $yesteryear was $previousday $yesterday-$yestermonth $yesteryear \n";
print '|' . '=' x 70 . "|\n";
}


}


Output:

|======================================================================|
Independence Day year 1998 Sat 4-Jul-1998
A day before independence day year 1998 was Friday 3-July 1998
|======================================================================|
Independence Day year 2000 Tue 4-Jul-2000
A day before independence day year 2000 was Monday 3-July 2000
|======================================================================|
Independence Day year 2001 Wed 4-Jul-2001
A day before independence day year 2001 was Tuesday 3-July 2001
|======================================================================|
Independence Day year 2002 Thu 4-Jul-2002
A day before independence day year 2002 was Wednesday 3-July 2002
|======================================================================|
Independence Day year 2003 Fri 4-Jul-2003
A day before independence day year 2003 was Thursday 3-July 2003
|======================================================================|
Independence Day year 2004 Sun 4-Jul-2004
A day before independence day year 2004 was Saturday 3-July 2004
|======================================================================|
Independence Day year 2005 Mon 4-Jul-2005
A day before independence day year 2005 was Sunday 3-July 2005
|======================================================================|
Independence Day year 2006 Tue 4-Jul-2006
A day before independence day year 2006 was Monday 3-July 2006
|======================================================================|
Independence Day year 2007 Wed 4-Jul-2007
A day before independence day year 2007 was Tuesday 3-July 2007
|======================================================================|
Independence Day year 2008 Fri 4-Jul-2008
A day before independence day year 2008 was Thursday 3-July 2008
|======================================================================|
Independence Day year 2009 Sat 4-Jul-2009
A day before independence day year 2009 was Friday 3-July 2009
|======================================================================|
Independence Day year 2010 Sun 4-Jul-2010
A day before independence day year 2010 was Saturday 3-July 2010
|======================================================================|



dmazzini
GSM System and Telecomm Consultant

 
Thank You once again! I appreciate all your help. Your pretty darn good at this. I should be able to figure out memorial day from what you've done.. I hope. Here's how I've approached this, my problem was with calc memorial day, because it falls on the last Monday of May.

Thanks Again!

use Strict;
use Win32::process;
use Date::Calc qw(Delta_Days Today Nth_Weekday_of_Month_Year Day_of_Week
Day_of_Week_to_Text Date_to_Text_Long Month_to_Text Date_to_Days);
use Env;
use Date::Calendar::profiles qw($Profiles);
use Date::Calendar;
$calendar_US_CA = Date::Calendar->new($Profiles->{'US-CA'});

($year,$month,$day) = Today();
#@holidays = $calendar->labels();
#$holidays = $calendar->labels($year,$month,$day);

my ($sec,$min,$hour,$mday,$mon,$year,
$wday,$yday,$isdst) = localtime time;


$week = int(($day + Day_of_Week($year,$month,1) - 2) / 7) + 1;

$dow = Day_of_Week($year,$month,$day);

$DayOfWeek = Day_of_Week_to_Text($dow);


# Check the day before a given holiday
CASE: {
($month == 7 && $day == 3) && do{ # Independence Day
exit();
last CASE;
};

($month == 9 && $week == 1 && $DayOfWeek eq "Sunday") && do{ # Labor Day
exit();
last CASE;
};

($month == 8 && $week == 2 && $DayOfWeek eq "Sunday") && do{ # Columbus Day
exit();
last CASE;
};

($month == 9 && $week == 2 && $DayOfWeek eq "Sunday") && do{ # Veterans Day
exit();
last CASE;
};

($month == 5 && $week == $lastweek && $DayOfWeek eq "Sunday") && do{ # Memorial Day
exit();
last CASE;
};

($month == 12 && $day == 24) && do{ # Christmas Day
exit();
last CASE;
};

} # end of CASE block

# Here is where the script runs if it's not the day before # one of the above holidays.
 
Here we go again, now I understand a little bit more what you want to do..It's no so elegant but it seems to work fine.

Try changing some holidays days, for instance using birthday as tomorrrow an you will see some messages. I also tested it setting Columbus Day as tomorrow. It worked ok.

Cheers!




use Date::Calc qw:)all);
use Date::Calendar;

my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
$year+=1900; $mon+=1;
$mday="0".$mday if ($mday<10);
$mon="0".$mon if ($mon<10);
$hour="0".$hour if ($hour<10);
$min="0".$min if ($min<10);
$sec="0".$sec if ($sec<10);

$today_date="$year$mon$mday";
@MyYears= $year;
$week = int(($mday + Day_of_Week($year,$mon,1) - 2) / 7) + 1;
$dow = Day_of_Week($year,$mon,$mday);
$DayOfWeek = Day_of_Week_to_Text($dow);

$MyHolidays =
{
"Independence Day" => "04.07.",
"Christmas Day" => "25.12.",
"New Year" => "31.12.",
"My Birthday" => "27.12."

};

$calendar = Date::Calendar->new( $MyHolidays);

foreach $yearselected (@MyYears) {
$calendar->year( $yearselected );
@indepday = $calendar->search( "Independence Day" );
@chritsmasday = $calendar->search( "Christmas Day" );
@newyearsday = $calendar->search( "New Year" );
@birthday = $calendar->search( "My Birthday" );

}

@list = (@indepday,@chritsmasday,@newyearsday,@birthday);

foreach $date (@list){
@labels = $calendar->labels( $date );
$dow = shift(@labels);
@holydaymessage=@labels;
foreach (@labels) {
$anio=$date->year();
$dia= $date->day();
$mes= $date->month();
my ($yesteryear,$yestermonth,$yesterday) = Add_Delta_Days($anio,$mes,$dia, -1);
$yestermonth="0".$yestermonth if ($yestermonth<10);
$yesterday="0".$yesterday if ($yesterday<10);
$allyesterday= $yesteryear.$yestermonth.$yesterday;
@previousday= $calendar->labels( $date-1 );
@message=@previousday;
$previousday= shift(@previousday);

stop_routine() if ($today_date == $allyesterday); # Holidays

if ($mon == 9 && $week == 1 && $DayOfWeek eq "Sunday") { # Labor Day
@holydaymessage="Labor Day\n";
stop_routine();
}
if ($mon == 6 && $week == 2 && $DayOfWeek eq "Monday"){ # Columbus Day
@holydaymessage="Columbus Day";
stop_routine();
}

if ($mon == 9 && $week == 2 && $DayOfWeek eq "Sunday"){ # Veterans Day
@holydaymessage="Veterans Day";
stop_routine();
}


}

}


sub stop_routine{
print "I am sorry but today we can not continue running the script. Tomorrow will be \"@holydaymessage\".\n";
exit();
}



dmazzini
GSM System and Telecomm Consultant

 
Sir

I could not sleep well, so I woke up thinking in the "Holidays's script"....

Disregard the other post, it works but it does not use all modules functionalities..Some I came up with it. Mainly, It does not compare the weeks, month and day of the week for holidays like "columbus day". It gets the values from "$MyHolydays".

Here the code:

use Date::Calc qw:)all);
use Date::Calendar;

my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
$year+=1900; $mon+=1;
$mday="0".$mday if ($mday<10);
$mon="0".$mon if ($mon<10);

$today_date="$year$mon$mday";
@MyYears= $year;

$MyHolidays =
{
"Independence Day" => "04.07.",
"Christmas Day" => "25.12.",
"New Year" => "31.12.",
"My Birthday" => "27.12.",
"Columbus Day" => "2/Mon/Jun", # Second Monday Oct
"Labor Day" => "1/Mon/Sep", # First Monday Sep
"Test Date" => "2/Wed/Jun" # Test purposes, Second Wednesday of June
};

$calendar = Date::Calendar->new( $MyHolidays);

foreach $yearselected (@MyYears) {
$calendar->year( $yearselected );
@indepday = $calendar->search( "Independence Day" );
@chritsmasday = $calendar->search( "Christmas Day" );
@newyearsday = $calendar->search( "New Year" );
@birthday = $calendar->search( "My Birthday" );
@columbusday = $calendar->search( "Columbus Day" );
@laborday = $calendar->search( "Labor Day" );
@testdate = $calendar->search( "Test Date" );
}

@list = (@indepday,@chritsmasday,@newyearsday,@birthday,@columbusday,@laborday,@testdate);

foreach $date (@list){
@labels = $calendar->labels( $date );
$dow = shift(@labels);
@holydaymessage=@labels;
foreach (@labels) {
$anio=$date->year();
$dia= $date->day();
$mes= $date->month();
my ($yesteryear,$yestermonth,$yesterday) = Add_Delta_Days($anio,$mes,$dia, -1);
$yestermonth="0".$yestermonth if ($yestermonth<10);
$yesterday="0".$yesterday if ($yesterday<10);
$allyesterday= $yesteryear.$yestermonth.$yesterday;
@previousday= $calendar->labels( $date-1 );
@message=@previousday;
$previousday= shift(@previousday);
stop_routine() if ($today_date == $allyesterday); # 1 day before Holidays
}

}


sub stop_routine{
print "I am sorry but today we can not continue running the script. Tomorrow will be \"@holydaymessage\".\n";
exit();
}



dmazzini
GSM System and Telecomm Consultant

 
Actually you could remove these lines....

@previousday= $calendar->labels( $date-1 );
@message=@previousday;
$previousday= shift(@previousday);

And Then.....

use Date::Calc qw:)all);
use Date::Calendar;

my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
$year+=1900; $mon+=1;
$mday="0".$mday if ($mday<10);
$mon="0".$mon if ($mon<10);

$today_date="$year$mon$mday";
@MyYears= $year;

$MyHolidays =
{
"Independence Day" => "04.07.",
"Christmas Day" => "25.12.",
"New Year" => "31.12.",
"My Birthday" => "27.12.",
"Columbus Day" => "2/Mon/Jun", # Second Monday Oct
"Labor Day" => "1/Mon/Sep", # First Monday Sep
"Test Date" => "2/Wed/Jun" # Test purposes, Second Wednesday of June
};

$calendar = Date::Calendar->new( $MyHolidays);

foreach $yearselected (@MyYears) {
$calendar->year( $yearselected );
@indepday = $calendar->search( "Independence Day" );
@chritsmasday = $calendar->search( "Christmas Day" );
@newyearsday = $calendar->search( "New Year" );
@birthday = $calendar->search( "My Birthday" );
@columbusday = $calendar->search( "Columbus Day" );
@laborday = $calendar->search( "Labor Day" );
@testdate = $calendar->search( "Test Date" );
}

@list = (@indepday,@chritsmasday,@newyearsday,@birthday,@columbusday,@laborday,@testdate);

foreach $date (@list){
@labels = $calendar->labels( $date );
$dow = shift(@labels);
@holydaymessage=@labels;
foreach (@labels) {
$anio=$date->year();
$dia= $date->day();
$mes= $date->month();
my ($yesteryear,$yestermonth,$yesterday) = Add_Delta_Days($anio,$mes,$dia, -1);
$yestermonth="0".$yestermonth if ($yestermonth<10);
$yesterday="0".$yesterday if ($yesterday<10);
$allyesterday= $yesteryear.$yestermonth.$yesterday;
stop_routine() if ($today_date == $allyesterday); # 1 day before Holidays
}

}


sub stop_routine{
print "I am sorry but today we can not continue running the script. Tomorrow will be \"@holydaymessage\".\n";
exit();
}





dmazzini
GSM System and Telecomm Consultant

 
I wrote the subroutine for memorial day.. It works I tested it out over several years, but as you said before it's not too elegant. I need to learn more about references in perl, since this mod is loaded with them.

# Calculate day before Memorial Day
sub check {
$Holidays =

{
"Memorial Day" => "5/Mon/May" # LAST Monday of May
};

@today = Today();
$calendar = Date::Calendar->new( $Holidays );
$calendar->year( $today[0] );

foreach $key ("Memorial Day")
{
if (@list = $calendar->search($key))
{
foreach $date (@list)
{
@labels = $calendar->labels( $date );
$dow = shift(@labels);
$hday = $date->day();
if ($day == ($hday - 1)) {
exit() }
}
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top