我有系统时区为 EST 的服务器
它被设置为
# timedatectl set-timezone EST
# timedatectl
Local time: Tue 2019-06-18 03:29:42 EST
Universal time: Tue 2019-06-18 08:29:42 UTC
RTC time: Tue 2019-06-18 08:29:42
Time zone: EST (EST, -0500)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
然后在我的perl代码中,我尝试检测时区并将时间转换为 UTC 并且由于某种原因,时区被检测为 EDT。目前(夏季)EST 是我们在 EDT 之后的一个
我的 perl 代码是
# cat test.cgi # code file is named test.cgi
#!/usr/bin/perl -w
use Date::Manip;
print "Local time: ".UnixDate("now","%Y-%m-%d %H:%M:%S %Z")."\n";
# get local system timezone
my $TZ = UnixDate("now","%Z");
# convert time to GMT
my $dtlocal = Date_ConvTZ(ParseDate(UnixDate("now","%g")),$TZ,"GMT");
# format GMT time
my $dt = UnixDate($dtlocal,"%Y-%m-%d %H:%M:%S");
print "Timezone: $TZ\n";
print "UTC time: $dt\n";
我执行脚本
# date
Tue Jun 18 04:17:56 EST 2019
# date -u
Tue Jun 18 09:17:58 UTC 2019
# ./test.cgi
Local time: 2019-06-18 04:18:00 EDT
Timezone: EDT
UTC time: 2019-06-18 08:18:00
Date::Manip 错误检测时区的原因是什么?还有其他检测系统时区的好方法吗?
更新。
我刚刚发现 POSIX 正确检测时区。所以,问题在于 Date::Manip
use POSIX;
print strftime("%Z", localtime()), "\n";