所有,
我想知道从给定日期开始的上周三的日期.
例如.我的日期为“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%02d\n",1900 + $y,$m + 1,$d; } print $_,' ',prev_wednesday($_),for qw( 20150804 20150805 20150806 20150101 20000301 20010301 );