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

前端之家收集整理的这篇文章主要介绍了如何设置Perl当地时间()的时区?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在Perl中,我想在特定的时区查找当地时间.我一直在使用这种技术:
$ENV{TZ} = 'America/Los_Angeles';
my $now = scalar localtime;
print "It is now $now\n";
# WORKS: prints the current time in LA

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

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

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

解决方法

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

my $was = localtime;
print "It was      $was\n";

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

$was = localtime;
print "It is still $was\n";

tzset;

my $now = localtime;
print "It is now   $now\n";
$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
原文链接:https://www.f2er.com/Perl/172745.html

猜你在找的Perl相关文章