使用此功能:
perl -e 'use Time::Local; print timelocal("00","00","00","01","01","2000"),"\n";'
它将返回一个纪元时间 - 但仅在GMT中 - 如果我想要GMT + 1的结果(系统本地时间(TZ)),我需要更改什么?
提前致谢,
的Anders
答案 0 :(得分:6)
use DateTime;
my $dt = DateTime->now;
$dt->set_time_zone( 'Europe/Madrid' );
答案 1 :(得分:4)
基于UTC,epochtime只有一个标准定义,而不同时区的时间段不同。
如果您想找到offset between gmtime and localtime,请使用
use Time::Local;
@t = localtime(time);
$gmt_offset_in_seconds = timegm(@t) - timelocal(@t);
答案 2 :(得分:2)
您只需设置时区即可。尝试:
env TZ=UTC+1 perl -e 'use Time::Local; print timelocal("00","00","00","01","01","2000"),"\n";'
答案 3 :(得分:2)
Time::Local::timelocal
是localtime
的倒数。结果将在您主持人的当地时间:
$ perl -MTime::Local -le \ 'print scalar localtime timelocal "00","00","00","01","01","2000"' Tue Feb 1 00:00:00 2000
您想要gmtime
对应的localtime
吗?
$ perl -MTime::Local' -le \ 'print scalar gmtime timelocal "00","00","00","01","01","2000"' Mon Jan 31 23:00:00 2000
您是否希望它与localtime
对应的gmtime
相反?
$ perl -MTime::Local -le \ 'print scalar localtime timegm "00","00","00","01","01","2000"' Tue Feb 1 01:00:00 2000
答案 4 :(得分:2)
虽然Time :: Local是一个合理的解决方案,但最好使用更现代的DateTime面向对象模块。这是一个例子:
use strict;
use DateTime;
my $dt = DateTime->now;
print $dt->epoch, "\n";
对于时区,您可以使用DateTime :: TimeZone模块。
use strict;
use DateTime;
use DateTime::TimeZone;
my $dt = DateTime->now;
my $tz = DateTime::TimeZone->new(name => "local");
$dt->add(seconds => $tz->offset_for_datetime($dt));
print $dt->epoch, "\n";
CPAN链接:
答案 5 :(得分:0)
另一个基于DateTime::Format::Strptime
的示例use strict;
use warnings;
use v5.10;
use DateTime::Format::Strptime;
my $s = "2016-12-22T06:16:29.798Z";
my $p = DateTime::Format::Strptime->new(
pattern => "%Y-%m-%dT%T.%NZ",
time_zone => "UTC"
);
my $dt = $p->parse_datetime($s);
$dt->set_time_zone("Europe/Berlin");
say join ' ', $dt->ymd, $dt->hms; # shows 2016-12-22 07:16:29