Steve Haslam (araqnid) wrote,
Steve Haslam

  • Mood:
  • Music:

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


  • Reading Half-Marathon 2009

    Reading Half-Marathon 2009 Total 02:03:39 Position = 7626 out of ~17,000? The day didn't get off to too good a start: 6am on…

  • Getting back into training

    I guess it won't come as a surprise to those who know me that I've resolved to try and get fitter during the coming year. Which I've pretty much…

  • everyone's having a go...

    I see Mesh have a "God Rest Ye Merry Gentlemen" ditty on their Myspace page... Heading up to the parental home soon. Disorganised, as is…

  • Post a new comment


    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.