I have to run a script on the 1st working day of every month. Please suggest how I can do it in Perl.
Lets say if its national holiday in that country, the script should ru开发者_开发技巧n on 2nd working day. I have one binary which give me output of previous working day if its holiday for specific country.
I suggest you run your script using cron
once every day Mon-Fri.
Then your script would have an initial test, and exit if the test fails.
The test would be (pseudo code):
if ( isWeekend( today ) ) {
exit;
} elsif ( public_holiday( today ) ) {
exit;
}
for ( day_of_month = 1; day_of_month < today; day_of_month++ ) {
next if ( isWeekend( day_of_month ) );
if ( ! public_holiday( day_of_month ) ) {
# a valid day earlier in the month wasn't a public holiday
# thus this script MUST have successfully run, so exit
exit;
}
}
# run script, because today is NOT the weekend, NOT a public holiday, and
# no possible valid days for running exist earlier in this month
1;
For example, an isWeekend
function might look like this in Perl:
sub isWeekend {
my ( $epoch_time ) = @_;
my $day_of_week = ( localtime( $epoch_time ) )[6];
return( 1 ) if ( $day_of_week == 0 ); # Sunday
return( 1 ) if ( $day_of_week == 6 ); # Saturday
return( 0 );
}
You would have to write your own public_holiday
function to return a truth value depending on whether a date was a public holiday in your specific state/country.
The CPAN package Date::Manip
has all sort of good stuff to support this sort of thing. 'Date_NextWorkDay()' would seem the most appropriate to you.
精彩评论