創建一個DateTime對象,並將其與DateTime->now
。 DateTime對象知道與其中的時間戳相關聯的時區,所以它可以做你想做的事,不用大驚小怪。
use strict;
use warnings;
use feature qw(say);
use DateTime qw();
use DateTime::Format::Strptime qw();
my $strp = DateTime::Format::Strptime->new(
pattern => '%b %d, %Y %H:%M:%S GMT%z',
locale => 'en',
on_error => 'croak',
);
my $target = 'Sep 10, 2011 12:00:00 GMT-0700';
my $target_dt = $strp->parse_datetime($target);
my $now_dt = DateTime->now();
if ($now_dt > $target_dt) {
say "It's too late";
} else {
say "It's not too late";
}
$target_dt->set_time_zone('local');
say "The deadline is $target_dt, local time";
上面我假設你錯誤地使用了日期格式。如果日期按照您提供的格式設置,則您將無法使用Strptime,因爲時間戳使用非標準名稱來表示月份,偏移量使用非標準格式。
my @months = qw(... Sept ...);
my %months = map { $months[$_] => $_+1 } 0..$#months;
my ($m,$d,$Y,$H,$M,$S,$offS,$offH,$offM) = $target =~
/^(\w+) (\d+), (\d+) (\d+):(\d+):(\d+) GMT ([+-])(\d+):(\d+)\z/
or die;
my $target_dt = DateTime->new(
year => $Y,
month => $months{$m},
day => 0+$d,
hour => 0+$H,
minute => 0+$M,
second => 0+$S,
time_zone => sprintf("%s%04d", $offS, $offH * 100 + $offM),
);
感謝這幫助了很多...我想我得到了我一直在尋找... – Sunyl
@Sunyl,我追加兩行第一個片段。我應該幫助澄清你的疑問。 – ikegami