Take the 2-minute tour ×

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;


I can set the timezone using - local $ENV{TZ} = ":/usr/share/lib/zoneinfo/America/Los_Angeles";

How can I extract the timezone from localtime() ?

share|improve this question
$yr = $yr-100+2000 is really (documented) year+1900, though I sure your form is optimized away. –  JRFerguson Dec 12 '11 at 23:35
add comment

3 Answers

up vote 5 down vote accepted

You could use strftime():

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

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

share|improve this answer
add comment

You can have the timezone as well as the offset from UTC:

perl -MPOSIX -e 'print strftime "%Z (%z)\n",localtime'
share|improve this answer
add comment
$ perl -MPOSIX -le 'tzset; print for tzname'

tzset() and tzname() functions from POSIX module can be a easily remembered answer.

share|improve this answer
add comment

Your Answer


