How do I set the timezone for Perl's localtime()?

后端 未结 6 2046

In Perl, I\'d like to look up the localtime in a specific timezone. I had been using this technique:

$ENV{TZ} = \'America/Los_Angeles\';
my $now = scalar loc         


        
6条回答
  •  渐次进展
    2020-12-05 19:19

    Use 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
    

提交回复
热议问题