Yossi has asked for the wisdom of the Perl Monks concerning the following question:

i have this code:
sub time { my $thistime = shift; my @a = gmtime($thistime); return sprintf "%s, %02d %s %04d, %02d:%02d:%02d", (qw(Sun Mon Tue Wed Thu Fri Sat))[$a[6]], $a[3], (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$a[4]], 1900+$a[5],@a[2,1,0]; }
it gives the time in the format i need, but it gives me the time in GMT and i need GMT + 3, how can i change that?

Replies are listed 'Best First'.
Re: time zones
by clintp (Curate) on Jul 29, 2001 at 00:37 UTC
    For me, it works well enough to say:
    { local $ENV{TZ}="GMT+3"; print scalar localtime; }
    And I get the time string for GMT+3. Of course, this depends on the behaviour of your system's localtime() function.
Re: time zones
by tachyon (Chancellor) on Jul 29, 2001 at 04:00 UTC

    Hi, you seem to have missed the fact that calling gmtime in scalar context will format the return value as a scalar and produce exactly the same result as you get manually using spintf. This is one of those cases where calling a funtion in scalar context produces a different result to calling it in list context. You can save yourself a lot of effort by taking advantage of the scalar return value of gmtime() as all you actually need is this:

    sub g3time{ return scalar gmtime( shift() + 3*60*60 ); } print g3time( time() );

    Remove the scalar to see return impose list context on the result of gmtime and thus return the numerical list. As pointed out above calling your sub time conflicts with the built in time function and is highly *not* recommended, so I've renamed it g3time.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: time zones
by wog (Curate) on Jul 29, 2001 at 00:43 UTC
    One way to do this would be to adjust $thistime appropriately.

    sub time { # ** conflicts with builtin 'time'! Yow! my $thistime = shift; $thistime -= 3 * 3600; # - three hours, since GMT+3 is that much ear +lier. my @a = gmtime($thistime); ... }

    You should probably look at the strftime function offered by the POSIX module, it may make stringifing the date and changing it's format in the future simplier.

      this code works!!!!
Re: time zones
by mitd (Curate) on Jul 29, 2001 at 03:12 UTC
    Date::Format built for comfort, built for speed.

    mitd-Made in the Dark
    'My favourite colour appears to be grey.'