The script I am writing is supposed to return the next business day (there's a list of holidays the script knows about, and it also calculates whether or not it's the weekend).
I haven't had any problems getting to this point (finding out whether a date passed to the script is one of the known holidays or a weekend) but now I'm stuck at determining the next business day.
if the date passed to the script is NOT a weekend date, and it's NOT listed in one of the hashes I'm using as a holiday, then the script is supposed to figure out the next business day (next day that is not the weekend, and does not have a holiday associated with it)
everything's wrriten and works, except for my get_next_business_day() sub. I'm drawing a blank right now. May be it's becaused it's been a long day :)
Was wondering if any of you guys would have comments / suggestions / solutions ?
#!/usr/bin/perl use Time::Local 'timelocal_nocheck'; my $date_to_check = shift; my $zone = shift; $zone = lc($zone); my $dayoff = 0; ############################# # was a proper date given? ############################# unless ($date_to_check =~ /^\d{8}$/) { print "The date must be in the yyyymmdd format. Example: 20090101\ +n"; exit(-1); } ############################################### # is the zone correct? was it even given? ############################################### unless (($zone =~ /^ny$/) || ($zone =~ /^hk$/) || ($zone =~ /^mo$/) || + ($zone =~ /^ln$/) || ($zone =~ /^tk$/ ) || ($zone =~ /^se$/ )) { print "The acceptable zones are:\nNY\nHK\nMO\nLN\nTK\nSE\n"; exit(-1); } ############################################### # does the date fall on a weekend? ############################################### my @weekday = qw(Sunday Monday Tuesday Wednesday Thursday Friday Satur +day); my $day_of_week = get_day($date_to_check); unless (($day_of_week =~ /Monday/) || ($day_of_week =~ /Tuesday/) || ( +$day_of_week =~ /Wednesday/) || ($day_of_week =~ /Thursday/) || ($day +_of_week =~ /Friday/) ) { print "$date_to_check falls on $day_of_week - weekend\n"; print "DAYOFF\n"; $dayoff = 1; } sub get_day { my $date = shift || return(0); my ($year,$mon,$mday) = $date =~ /(\d{4})(\d{2})(\d{2})/; my $epochtime = timelocal_nocheck(0, 0, 0, $mday, $mon-1, $year); my $day = (localtime($epochtime))[6]; return $weekday[$day]; } ########################################## # holiday definitions ########################################## my %ny_holidays = (); my %hk_holidays = (); my %mo_holidays = (); my %ln_holidays = (); my %tk_holidays = (); my %se_holidays = (); ####################################### # United States ####################################### $ny_holidays{'20090101'} = 'New Years Day'; $ny_holidays{'20090115'} = 'MLK'; $ny_holidays{'20090219'} = 'Presidents day'; $ny_holidays{'20090406'} = 'Good Friday'; $ny_holidays{'20090528'} = 'Memorial Day'; $ny_holidays{'20090704'} = 'Independence Day'; $ny_holidays{'20090903'} = 'Labor day'; $ny_holidays{'20091122'} = 'Thanksgiving'; $ny_holidays{'20091225'} = 'Christmas Day'; ####################################### # Hong Kong ####################################### $hk_holidays{'20090101'} = 'New Years Day'; $hk_holidays{'20090217'} = 'Day preceding Lunar New Years day'; $hk_holidays{'20090219'} = 'Second day of Lunar New Year'; $hk_holidays{'20090220'} = 'Third Day of Lunar New Year'; $hk_holidays{'20090405'} = 'Ching Ming festival'; $hk_holidays{'20090406'} = 'Good Friday'; $hk_holidays{'20090407'} = 'The day following Good Friday'; $hk_holidays{'20090409'} = 'Easter Monday'; $hk_holidays{'20090501'} = 'Labour day'; $hk_holidays{'20090524'} = 'Buddhas birthday'; $hk_holidays{'20090619'} = 'Tuen Ng Festival'; $hk_holidays{'20090702'} = 'The day following HK SAR Establishment Day +'; $hk_holidays{'20090926'} = 'The day following Chinese Mid-Autumn Festi +val'; $hk_holidays{'20091001'} = 'National Day'; $hk_holidays{'20091019'} = 'Cheung Yeung Festival'; $hk_holidays{'20091225'} = 'Christmas Day'; $hk_holidays{'20091226'} = 'The first weekday after Christmas Day'; ####################################### # Russia ####################################### $mo_holidays{'20090101'} = 'New Year'; $mo_holidays{'20090102'} = 'New Year'; $mo_holidays{'20090103'} = 'New Year'; $mo_holidays{'20090104'} = 'New Year'; $mo_holidays{'20090105'} = 'New Year'; $mo_holidays{'20090107'} = 'New Year'; $mo_holidays{'20090223'} = 'Fatherland Defense Day'; $mo_holidays{'20090308'} = 'Womens day'; $mo_holidays{'20090501'} = 'Labour day'; $mo_holidays{'20090509'} = 'Victory Day'; $mo_holidays{'20090612'} = 'Russias day'; $mo_holidays{'20091104'} = 'Unity day'; ####################################### # United Kingdom ####################################### $ln_holidays{'20090101'} = 'New Year'; $ln_holidays{'20090102'} = 'New Year'; $ln_holidays{'20090406'} = 'Good friday'; $ln_holidays{'20090409'} = 'Easter Monday'; $ln_holidays{'20090507'} = 'May Day'; $ln_holidays{'20090528'} = 'Spring Holiday'; $ln_holidays{'20090806'} = 'Summer holiday'; $ln_holidays{'20090827'} = 'Summer holiday'; $ln_holidays{'20091225'} = 'Christmas day'; $ln_holidays{'20091226'} = 'Boxing day'; ####################################### # Japan ####################################### $tk_holidays{'20090101'} = 'New Years Day'; $tk_holidays{'20090102'} = 'New Years Day'; $tk_holidays{'20090103'} = 'New Years Day'; $tk_holidays{'20090104'} = 'New Years Day'; $tk_holidays{'20090108'} = 'Coming of Age Day'; $tk_holidays{'20090211'} = 'National Foundation Day'; $tk_holidays{'20090212'} = 'Substitute Holiday for the National Founda +tion Day'; $tk_holidays{'20090321'} = 'Vernal Equinox Day'; $tk_holidays{'20090429'} = 'Greenery Day'; $tk_holidays{'20090430'} = 'Substitute Holiday for the Greenery Day'; $tk_holidays{'20090503'} = 'Constitution Memorial Day'; $tk_holidays{'20090504'} = 'National Holiday'; $tk_holidays{'20090505'} = 'Childrens Day'; $tk_holidays{'20090616'} = 'Marine Day'; $tk_holidays{'20090917'} = 'Respect for the Aged Day'; $tk_holidays{'20090923'} = 'Autumnal Equinox Day'; $tk_holidays{'20090924'} = 'Substitute Holiday for the Autumnal Equino +x Day'; $tk_holidays{'20091008'} = 'Health and Sports Day'; $tk_holidays{'20091103'} = 'National Culture Day'; $tk_holidays{'20091123'} = 'Labour Thanksgiving Day'; $tk_holidays{'20091223'} = 'Emperors birthday'; $tk_holidays{'20091224'} = 'Substitute Holiday for the Emperors Birthd +ay'; $tk_holidays{'20091228'} = 'New Years Holiday - Half Day Off'; $tk_holidays{'20091231'} = 'New Years Holiday'; ####################################### # Korea ####################################### $se_holidays{'20090101'} = 'New Year'; $se_holidays{'20090217'} = 'The day preceeding Lunar New Years Day'; $se_holidays{'20090219'} = 'The Second Day of the Lunar New Year'; $se_holidays{'20090301'} = 'Indepedence Movement Day'; $se_holidays{'20090501'} = 'Labour Day '; $se_holidays{'20090505'} = 'Childrens Day'; $se_holidays{'20090524'} = 'Buddhas Birthday'; $se_holidays{'20090606'} = 'Memorial Day'; $se_holidays{'20090717'} = 'Constitution Day'; $se_holidays{'20090815'} = 'Independence Day'; $se_holidays{'20090924'} = 'Thanksgiving Day'; $se_holidays{'20090925'} = 'Thanksgiving Day'; $se_holidays{'20090926'} = 'Thanksgiving Day'; $se_holidays{'20091003'} = 'National Foundation Day'; $se_holidays{'20091225'} = 'Christmas Day'; if ($zone =~ /ny/) { if (exists($ny_holidays{$date_to_check})) { $dayoff = 1; my $next_business_day = get_next_business_day($date_to_check); print "The next business day is: $next_business_day\n"; } } if ($zone =~ /hk/) { if (exists($hk_holidays{$date_to_check})) { $dayoff = 1; my $next_business_day = get_next_business_day($date_to +_check); print "The next business day is: $next_business_day\n" +; } } if ($zone =~ /mo/) { if(exists($mo_holidays{$date_to_check})) { $dayoff = 1; my $next_business_day = get_next_business_day($date_to +_check); print "The next business day is: $next_business_day\n" +; } } if ($zone =~ /ln/) { if(exists($ln_holidays{$date_to_check})) { $dayoff = 1; my $next_business_day = get_next_business_day($date_to +_check); print "The next business day is: $next_business_day\n" +; } } if ($zone =~ /tk/) { if(exists($tk_holidays{$date_to_check})) { $dayoff = 1; my $next_business_day = get_next_business_day($date_to +_check); print "The next business day is: $next_business_day\n" +; } } if ($zone =~ /se/) { if(exists($se_holidays{$date_to_check})) { $dayoff = 1; my $next_business_day = get_next_business_day($date_to +_check); print "The next business day is: $next_business_day\n" +; } } if ($dayoff == 1) { print "$date_to_check is a day off\n"; } elsif ($dayoff == 0) { print "$date_to_check is NOT a day off\n"; } sub get_next_business_day { my $date = shift; ###################### # ???????????????????? ###################### return $date; }
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |