我正在存储一个日期集合,这些日期是从月初开始的前12个月。所以我有:
my @t = localtime time();
my $m = $t[4];
my $y = $t[5];
foreach my $date (keys %$dates_ref) {
$m -= $comparison{$date}; # a hash of numbers to go back the correct number of months
$dates_ref->{$date} = mktime(0,0,0,1,$m,$y);
}
我最终得到了一堆日期:
当前月份:1356998400
即:
Tue Jan 01 2013 00:00:00 GMT+0000 (GMT Standard Time)
过去12个月:
1325376000 - Sun Jan 01 2012 00:00:00 GMT+0000 (GMT Standard Time)
1328054400 - Wed Feb 01 2012 00:00:00 GMT+0000 (GMT Standard Time)
1330560000 - Thu Mar 01 2012 00:00:00 GMT+0000 (GMT Standard Time)
1333238400 - Sun Apr 01 2012 01:00:00 GMT+0100 (GMT Daylight Time)
1335830400 - Tue May 01 2012 01:00:00 GMT+0100 (GMT Daylight Time)
1338508800 - Fri Jun 01 2012 01:00:00 GMT+0100 (GMT Daylight Time)
1341100800 - Sun Jul 01 2012 01:00:00 GMT+0100 (GMT Daylight Time)
1343779200 - Wed Aug 01 2012 01:00:00 GMT+0100 (GMT Daylight Time)
1346457600 - Sat Sep 01 2012 01:00:00 GMT+0100 (GMT Daylight Time)
1349049600 - Mon Oct 01 2012 01:00:00 GMT+0100 (GMT Daylight Time)
1351728000 - Thu Nov 01 2012 00:00:00 GMT+0000 (GMT Standard Time)
1354320000 - Sat Dec 01 2012 00:00:00 GMT+0000 (GMT Standard Time)
如您所见,4月至10月增加了GMT小时。获取不的最佳方法是什么,所以它只是从00:00
开始?
E.g。 1333238400
将成为1333234800
答案 0 :(得分:1)
您应该使用DateTime模块,并正确处理时区。这是一个例子:
my $dt = DateTime->new(
year => 2000,
month => 5,
day => 10,
hour => 15,
minute => 15,
time_zone => 'America/Los_Angeles',
);
print $dt->hour; # prints 15
$dt->set_time_zone( 'America/Chicago' );
print $dt->hour; # prints 17
答案 1 :(得分:0)
如果您能够安装CPAN模块,我建议使用Date :: Calc - 它会让您转换为GMT而不是当地时区,
答案 2 :(得分:0)
在脚本的第一行使用gmtime而不是localtime