NAME
Tie::TZ - tied $TZ setting %ENV and calling tzset()
SYNOPSIS
use Tie::TZ qw($TZ);
$TZ = 'GMT';
{ local $TZ = 'EST+10';
...
}
DESCRIPTION
This module provides a tied $TZ
variable which gets and sets $ENV{'TZ'}
. When it sets %ENV
it calls POSIX::tzset()
, ensuring the C library notices the change for subsequent localtime
, etc.
$TZ = 'GMT';
# does $ENV{'TZ'} = 'GMT'; POSIX::tzset();
For a plain set you can just as easily store and tzset
yourself. The power comes when using local
to have a different timezone temporarily. Any goto
, return
, die
, etc, exiting the block will restore the old setting and do tzset
.
{ local $TZ = 'GMT';
print ctime();
# TZ restored at block exit
}
{ local $TZ = 'GMT';
die 'Something';
# TZ restored when the die unwinds
}
As an optimization, if a store to $TZ
is already what $ENV{'TZ'}
contains then tzset
is not called. This is helpful when some settings you're working with might be all the same. If there's never any changes applied then POSIX
module is not even loaded at all.
Uses
Quite often tzset
is not actually needed. Decent C libraries look for a new TZ each time in the various localtime
etc. But tzset
keeps you out of trouble on older systems, or with any external libraries directly accessing the tzname
and daylight
global variables.
Perl's own calls to localtime
etc do a tzset
themselves where necessary (based on a configure test, see Config) to cope with old C libraries. But Perl doesn't that on localtime_r
until 5.8.9 (or some such), and the latter in fact in some versions of GNU C can need an explicit tzset
; the net result being that you need tzset
in a threaded Perl 5.8.8 (even when not using threads). Of course even if Perl recognises C library limitations you might not be so lucky deep in external libraries.
EXPORTS
By default nothing is exported and you can use the full name $Tie::TZ::TZ
,
use Tie::TZ;
$Tie::TZ::TZ = 'GMT';
Or import $TZ
in the usual way (see Exporter) as a convenient shorthand
use Tie::TZ '$TZ';
$TZ = 'GMT';
OTHER NOTES
When you get sick of the C library's fairly ordinary timezone handling have a look at DateTime::TimeZone. Its data tables make it big, though no doubt you can turf what you don't use. It's all Perl and can be a lot friendlier if you're working in multiple zones more or less simultaneously.
SEE ALSO
HOME PAGE
http://www.geocities.com/user42_kevin/tie-tz/index.html
COPYRIGHT
Copyright 2008 Kevin Ryde
Tie-TZ is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.
Tie-TZ is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Tie-TZ. If not, see http://www.gnu.org/licenses.