in reply to Timezones in Perl

I figured it out. I ended up doing the following:

my $inttime = time; my @local_tm = localtime($inttime); my @gmt_tm = gmtime($inttime); my $tz = $ENV{TZ}; print @local_tm[2],":",@local_tm[1],":",@local_tm[0]," ",$tz,"\n"; $tz = "EST5"; $ENV{TZ} = $tz; @local_tm = localtime($inttime); print @local_tm[2],":",@local_tm[1],":",@local_tm[0]," ",$tz,"\n";


This produces the following output, that while ugly is what I was hoping for..

11:5:40 EST5EDT 10:5:40 EST5
Ben

Replies are listed 'Best First'.
RE: RE: Timezones in Perl
by davorg (Chancellor) on Jun 23, 2000 at 19:33 UTC

    There are a couple of stylistic points that I'd like to make about your code - one more serious than the other.

    The more serious one is that you're using array slices where you want scalars. That is where you're using @local_tm[2] you should be using $local_tm[2].

    The less serious issue is that you don't need to split up the parameters to print. You can do something like this:

    print "$local_tm[2]:$local_tm[1]:$local_tm[0] $tz\n";

    or (to get the formatting a little nicer) you can use printf:

    printf '%02d:%02d:%02d %d', $local_tm[2], $local_tm[1], $local_tm[0], $tz;

    or (to really show people how well you know Perl) use printf and an array slice:

    printf '%02d:%02d:%02d %d', @local_tm[2, 1, 0], $tz;

    --
    <http://www.dave.org.uk>

    European Perl Conference - Sept 22/24 2000
    <http://www.yapc.org/Europe/>
      Thanks...

      The code I posted was basically just a quick hack as a proof of concept more than anything.. I obviously should have used $local_tm[2] rather than with the @. That was just stupidity on my part, combined with inexperience in perl. If I was doing something more valuable than just a proof of concept, I would have used printf rather than print.

      Thanks for the help though!

      Ben