首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当我更改$ENV{TZ}时,为什么Windows7上的ActivePerl中的本地时间不正确?

当我更改$ENV{TZ}时,为什么Windows7上的ActivePerl中的本地时间不正确?
EN

Stack Overflow用户
提问于 2015-03-19 00:08:02
回答 2查看 655关注 0票数 0

我在使用Perl的DateTime模块的IIS服务器上使用Bugzilla。不幸的是,时间显示不正确。gmtime以格林尼治标准时间显示正确的时间,但当我更改$ENV{TZ}时,localtime没有给出正确的时间。

以下是演示此问题的一些代码:

代码语言:javascript
运行
复制
#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";

这将打印:

代码语言:javascript
运行
复制
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

以下是预期结果:

代码语言:javascript
运行
复制
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版本信息:

代码语言:javascript
运行
复制
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说你必须跑

代码语言:javascript
运行
复制
POSIX::tzset();

在更改$ENV{TZ}之后,但当我尝试这样做时,我得到一个错误:

代码语言:javascript
运行
复制
POSIX::tzset not implemented on this architecture.

以下是IIS的服务器日志。时间是UTC的,并且是正确的。

代码语言:javascript
运行
复制
#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来宾操作系统。这就解决了问题。

EN

回答 2

Stack Overflow用户

发布于 2015-03-19 00:11:34

您需要在更改$ENV{TZ}后调用POSIX::tzset()

票数 1
EN

Stack Overflow用户

发布于 2015-03-19 00:49:55

您发布的代码未使用DateTime

代码语言:javascript
运行
复制
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):

代码语言:javascript
运行
复制
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:10
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29127031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档