perltimezonelocaltime

How to extract timezone from localtime()?


I'm using the following code to print the current time.

use Getopt::Long;
use Time::Local;
sub gettime
{
        my $t = time();
        my ($sec,$mn,$hr,$mday,$mon,$yr,@left, $dstr);

        ($sec,$mn,$hr,$mday,$mon,$yr,@left) = localtime($t);
        $yr  = $yr-100+2000;
        $mon += 1;
        $dstr = sprintf "%02d:%02d:%02d (%02d-%02d-%04d)", $hr, $mn, $sec, $mon, $mday, $yr;
        print $dstr;
}

gettime();

I can set the timezone using:

local $ENV{TZ} = ":/usr/share/lib/zoneinfo/America/Los_Angeles";

How can I extract the timezone from localtime()?


Solution

  • You could use strftime():

    use POSIX;
    $tz = strftime("%Z", localtime());
    

    Or, calculate the difference between localtime() and gmtime().