?

Log in

No account? Create an account

Previous Entry | Next Entry

[Perl] black magic!

Let's take a broken-down time array, and format it according to some arbitrary timezone:

# Imagine $timezone is "US/Eastern" or similar and we just matched 2004:03:01T16:02:59Z
my($year, $mon, $mday, $hour, $min, $sec) = ($1, $2, $3, $4, $5, $5);
{
 local $ENV{TZ} = $timezone;

 my $t = timegm($sec, $min, $hour, $mday, $mon-1, $year-1900); # 1078156922
 my @localtime;
 my $timestr;

 @localtime = localtime($t); $timestr = POSIX::strftime($format, @localtime); # ARCANA TENEBRAE
 @localtime = localtime($t); $timestr = POSIX::strftime($format, @localtime); # ARCANA TENEBRAE

 $m->print("$ENV{TZ} $isotime $t $timestr");
}
If I take out one of the "ARCANA TENEBRAE" lines, it prints the wrong thing-- the time as if it was GMT, not corrected for $ENV{TZ}.

So where's the jank? Using local $ENV{FOO} = '...' is perhaps risky (it certainly didn't used to be reliable for, e.g. local $dbh->{LongReadLen} = 131072), or it could be the C library giving gyp, or... argh!

I wish glibc had a usable interface to all its timezone information and i18n that didn't require me to hack around with the environment etc.

Comments

( 2 comments — Leave a comment )
kilinrax
Mar. 2nd, 2004 09:17 am (UTC)
What platform is on? If I interperse the "ARCANA TENEBRAE" with print statements, I get two identical lines, vis:

$ENV{TZ}: US/Eastern - $isotime: 2004:03:01T16:02:59Z - $t: 1078156979 - $timestr: Mon, 01 Mar 2004 11:02:59 EST -0500 - @localtime: 59 2 11 1 2 104 1 60 0
$ENV{TZ}: US/Eastern - $isotime: 2004:03:01T16:02:59Z - $t: 1078156979 - $timestr: Mon, 01 Mar 2004 11:02:59 EST -0500 - @localtime: 59 2 11 1 2 104 1 60 0
araqnid
Mar. 2nd, 2004 09:54 am (UTC)
it's on my debian sid machine, so perl 5.8.3. Running under mod_perl, although I'm not sure that'd make a difference. I haven't tried reproducing it in other situations, yet.
( 2 comments — Leave a comment )

Profile

araqnid
Steve Haslam

Latest Month

March 2009
S M T W T F S
1234567
891011121314
15161718192021
22232425262728
293031    

Page Summary

Powered by LiveJournal.com
Designed by Tiffany Chow