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 (or have a function to do the combo). The power of a tied variable comes when using local
to have a different timezone temporarily. Any goto
, return
, die
, etc, exiting the block will restore the old setting, including a tzset
for it.
{ local $TZ = 'GMT';
print ctime();
# TZ restored at block exit
}
{ local $TZ = 'GMT';
die 'Something';
# TZ restored when the die unwinds
}
Storing undef
to $TZ
deletes $ENV{'TZ'}
, unsetting the environment variable. This generally means the timezone goes back to the system default local time (usually per from /etc/timezone).
As an optimization, if a store to $TZ
is already what $ENV{'TZ'}
contains then tzset
is not called. This is helpful if some of the settings you're working are all the same. If there's never any different value 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 functions. But tzset
keeps you out of trouble on older systems, or with any external libraries directly accessing the C global variables like timezone
or daylight
.
Perl's own calls to localtime
etc do a tzset
themselves where necessary to cope with old C libraries (based on a configure test, see Config). However in 5.8.8 and earlier Perl didn't do that on localtime_r
, and the latter in some versions of GNU C can need an explicit tzset
; the net result being that you need tzset
in threaded Perl 5.8.8 (whether using threads or not). Of course even when 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 shorthand
use Tie::TZ '$TZ';
$TZ = 'GMT';
OTHER NOTES
The local
trick above of course works with $ENV{'TZ'}
directly too if you're happy that you don't need tzset
, eg. local $ENV{'TZ'} = 'EST+10'
. The Env
module can let you tie a $TZ
name for that too if desired. However undef
ends up meaning an empty string instead of unset, which may be subtly different, and it also provokes a warning from Perl prior to 5.10.
When you get sick of the C library timezone handling have a look at DateTime::TimeZone. Its data tables make it big (though no doubt you could turf what you don't use) but it's all Perl and is much friendlier if you're working in multiple zones more or less simultaneously.
SEE ALSO
POSIX, Env, DateTime::TimeZone
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.