gpt4 book ai didi

perl - 如何在每个月的第一个工作日运行脚本?

转载 作者:行者123 更新时间:2023-12-04 22:08:24 24 4
gpt4 key购买 nike

我必须在每个月的第一个工作日运行一个脚本。请建议我如何在 Perl 中做到这一点。

假设如果该国是国定假日,脚本应该在第二个工作日运行。
如果特定国家/地区的假期,我有一个二进制文件,可以为我提供前一个工作日的输出。

最佳答案

我建议你使用 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根据某个日期是否是您所在州/国家/地区的公共(public)假日来返回真值的函数。

关于perl - 如何在每个月的第一个工作日运行脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3801241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com