我在使用Perl的DateTime模块的IIS服务器上使用Bugzilla。不幸的是,时间显示不正确。gmtime以格林尼治标准时间显示正确的时间,但当我更改$ENV{TZ}时,localtime没有给出正确的时间。
以下是演示此问题的一些代码:
#C:\Perl64\bin/perl -w
$ENV{TZ} = 'America/New_York';
my $now = scalar localtime;
$TZone = $ENV{'TZ'};
print "It is now $now $TZone\n";
$ENV{TZ} = 'America/Los_Angeles';
my $now = scalar localtime;
$TZone = $ENV{'TZ'};
print "It is now $now $TZone\n";
my $GMTime = scalar gmtime;
print "It is now $GMTime GMT\n";这将打印:
It is now Wed Mar 18 17:02:46 2015 America/New_York
It is now Wed Mar 18 17:02:46 2015 America/Los_Angeles
It is now Wed Mar 18 16:02:46 2015 GMT以下是预期结果:
It is now Wed Mar 18 12:02:46 2015 America/New_York
It is now Wed Mar 18 9:02:46 2015 America/Los_Angeles
It is now Wed Mar 18 16:02:46 2015 GMT下面是我的perl版本信息:
This is perl 5, version 20, subversion 1 (v5.20.1) built for MSWin32-x64-multi-thread
(with 1 registered patch, see perl -V for more detail)
Copyright 1987-2014, Larry Wall
Binary build 2000 [298557] provided by ActiveState http://www.ActiveState.com
Built Oct 15 2014 14:56:57
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.我见过posts说你必须跑
POSIX::tzset();在更改$ENV{TZ}之后,但当我尝试这样做时,我得到一个错误:
POSIX::tzset not implemented on this architecture.以下是IIS的服务器日志。时间是UTC的,并且是正确的。
#Software: Microsoft Internet Information Services 7.5
#Version: 1.0
#Date: 2015-03-18 20:15:34
#Fields: date time s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) sc-status sc-substatus sc-win32-status time-taken
2015-03-18 20:17:52 127.0.0.1 GET /bugzilla/skins/standard/show_bug.css - 80 - 127.0.0.1 Mozilla/5.0+(Windows+NT+6.1;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/42.0.2311.39+Safari/537.36 304 0 0 1
2015-03-18 20:17:52 127.0.0.1 GET /bugzilla/js/comments.js - 80 - 127.0.0.1 Mozilla/5.0+(Windows+NT+6.1;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/42.0.2311.39+Safari/537.36 304 0 0 24
2015-03-18 20:18:06 127.0.0.1 POST /bugzilla/process_bug.cgi - 80 - 127.0.0.1 Mozilla/5.0+(Windows+NT+6.1;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/42.0.2311.39+Safari/537.36 200 0 0 9498
2015-03-18 20:18:23 127.0.0.1 GET /bugzilla/show_bug.cgi id=1 80 - 127.0.0.1 Mozilla/5.0+(Windows+NT+6.1;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/42.0.2311.39+Safari/537.36 200 0 0 1164我该如何解决这个问题呢?
我们的解决方案是将Bugzilla迁移到VM上的Linux来宾操作系统。这就解决了问题。
发布于 2015-03-19 00:11:34
您需要在更改$ENV{TZ}后调用POSIX::tzset()。
发布于 2015-03-19 00:49:55
您发布的代码未使用DateTime。
perl -MDateTime -E" say DateTime->now(time_zone => 'local');
say DateTime->now(time_zone => 'America/New_York');
say DateTime->now(time_zone => 'America/Los_Angeles');
say DateTime->now(time_zone => 'GMT');
my $now = DateTime->now(time_zone => 'local');
say 'before conversion: ', $now;
$now->set_time_zone('America/New_York');
say 'after conversion: ', $now;
"输出(来自CST):
2015-03-18T11:47:10
2015-03-18T12:47:10
2015-03-18T09:47:10
2015-03-18T16:47:10
before conversion: 2015-03-18T11:47:10
after conversion: 2015-03-18T12:47:10https://stackoverflow.com/questions/29127031
复制相似问题