创建一个 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),
);