Perl – 如何在不使用DateTime的情况下从给定日期获取上周三的日
发布时间:2020-12-15 23:26:02 所属栏目:大数据 来源:网络整理
导读:所有, 我想知道从给定日期开始的上周三的日期. 例如.我的日期为“20150804”,我需要“20150729”. DateTime不可用,我也无法安装它. 我看了几个例子,但他们正在使用DateTime. 你可以转发我的地方,我可以得到一些帮助吗?谢谢. 我打算编写类似下面的代码. 码:
所有,
我想知道从给定日期开始的上周三的日期. 例如.我的日期为“20150804”,我需要“20150729”. DateTime不可用,我也无法安装它. 我看了几个例子,但他们正在使用DateTime. 你可以转发我的地方,我可以得到一些帮助吗?谢谢. 我打算编写类似下面的代码. 码: #!/opt/perl-5.8.0/bin/perl use warnings; use strict; my $dt="20150804"; my $prevWednesday=getPrevWednesday($dt); sub getPrevWednesday() { my $givenDt=shift; ... } 解决方法
另一种蛮力方法,这次使用另一个核心模块
Time::Local.
#!/usr/bin/perl use warnings; use strict; use Time::Local; sub prev_wednesday { my $date = shift; my ($year,$month,$day) = $date =~ /(....)(..)(..)/; my $time = timelocal(0,12,$day,$month - 1,$year); do { $time -= 60 * 60 * 24 } until (localtime $time)[6] == 3; # <- Wednesday my ($y,$m,$d) = (localtime $time)[5,4,3]; return sprintf "%4d%02d%02dn",1900 + $y,$m + 1,$d; } print $_,' ',prev_wednesday($_),for qw( 20150804 20150805 20150806 20150101 20000301 20010301 ); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |