March 2nd, 2004

[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.

  • Current Music
    :Wumpscut: - Is It You