needperlhelp has asked for the wisdom of the Perl Monks concerning the following question:

Why value returned by 'time' command is not influenced by changes to the environment variable TZ,

print "T1:" . time . "\n";
print "TZ:$ENV{TZ}\n";
my ($l_cur_tz) = $ENV{TZ};
$ENV{TZ}= 'GMT-8';
print "T2:" . time . "\n";
print "TZ:$ENV{TZ}\n";
$ENV{TZ}= $l_cur_tz;

--- o/p -----
T1:1196655462
TZ:US/Eastern
T2:1196655462
TZ:GMT-8

the value of time always remains the same, someone please explain this behavior.

Replies are listed 'Best First'.
Re: 'time' command & $ENV{TZ}
by ikegami (Patriarch) on Dec 03, 2007 at 05:23 UTC

    The value returned by time is the number of seconds since epoch (often 0:00, Jan 1st, 1970 GMT). The number of seconds since some point in time doesn't change depending on where you happen to be located, so it's not affected by $ENV{TZ}.

    Convert the value return by time into a local time (e.g. using localtime) to see the change.

    Note: I think you need to call POSIX's tzset for the change to be noticed by localtime.

    Note: I think changing $ENV{TZ} *after* perl starts doesn't work properly in Windows. It works fine if you change it *before* perl starts.

    use POSIX qw( tzset ); print localtime(time) . " in $ENV{TZ}\n"; $ENV{TZ} = 'GMT-8'; tzset(); print localtime(time) . " in $ENV{TZ}\n";
    Mon Dec 3 00:22:29 2007 in America/New_York Mon Dec 3 13:22:29 2007 in GMT-8
Re: 'time' command & $ENV{TZ}
by kyle (Abbot) on Dec 03, 2007 at 04:35 UTC

    See the documentation for time. It's not affected by time zone because its value is always relative to a specific time in a specific time zone.

Re: 'time' command & $ENV{TZ}
by dwu (Monk) on Dec 03, 2007 at 04:38 UTC

    From time:

    time Returns the number of non-leap seconds since whatever time the system considers to be the epoch, suitable for feeding to "gmtime" and "localtime". On most systems the epoch is 00:00:00 UTC, January 1, 1970; a prominent exception being Mac OS Classic which uses 00:00:00, January 1, 1904 in the current local time zone for its epoch.

    My time also outputs something in the same numerical region as yours (1196656266). Conclusion: You're using one of the various UNIX/Linux/etc. flavours, or maybe even just Windows (I don't know what they use as epoch, but meh); therefore, your output of time is number of seconds since epoch (which is always UTC, as per doc).