加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何设置Perl当地时间()的时区?

发布时间:2020-12-15 21:14:06 所属栏目:大数据 来源:网络整理
导读:在Perl中,我想在特定的时区查找当地时间.我一直在使用这种技术: $ENV{TZ} = 'America/Los_Angeles';my $now = scalar localtime;print "It is now $nown";# WORKS: prints the current time in LA 但是,这并不可靠 – 特别是如果在设置$ENV {TZ}之前预先添
在Perl中,我想在特定的时区查找当地时间.我一直在使用这种技术:
$ENV{TZ} = 'America/Los_Angeles';
my $now = scalar localtime;
print "It is now $nown";
# WORKS: prints the current time in LA

但是,这并不可靠 – 特别是如果在设置$ENV {TZ}之前预先添加另一个localtime()调用,则会中断:

localtime();
$ENV{TZ} = 'America/Los_Angeles';
my $now = scalar localtime;
print "It is now $nown";
# FAILS: prints the current time for here instead of LA

有没有更好的方法来做到这一点?

解决方法

使用 POSIX::tzset.
use POSIX qw(tzset);

my $was = localtime;
print "It was      $wasn";

$ENV{TZ} = 'America/Los_Angeles';

$was = localtime;
print "It is still $wasn";

tzset;

my $now = localtime;
print "It is now   $nown";
$perl -v

This is perl,v5.8.8 built for x86_64-linux-thread-multi

Copyright 1987-2006,Larry Wall

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.

$perl tzset-test.pl
It was      Wed Apr 15 15:58:10 2009
It is still Wed Apr 15 15:58:10 2009
It is now   Wed Apr 15 12:58:10 2009

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读