我必须在每个月的第一个工作日运行一个脚本。请建议我如何在Perl中做到这一点。
假设它在那个国家的国庆节,脚本应该在第二个工作日运行。我有一个二进制,给我的输出前一个工作日,如果它的假日为特定的国家。
发布于 2010-09-27 15:34:41
我建议你每天周一到周五使用cron
运行一次脚本。
然后,您的脚本将进行初始测试,如果测试失败,则退出。
测试将是(伪代码):
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;
例如,isWeekend
函数在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 );
}
您必须编写自己的public_holiday
函数来返回真值,这取决于日期在您所在的州/国家是否为公共假日。
发布于 2010-09-27 16:24:27
CPAN包Date::Manip
有各种好东西来支持这类事情。'Date_NextWorkDay()‘对您来说似乎是最合适的。
https://stackoverflow.com/questions/3801241
复制相似问题