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

Hi Monks,
I'm having a problem with some code to get the time/date for specific timezones no matter what the timezone on the actual server is set to...
#!/usr/bin/perl use Time::Local; #get the time in Sydney, regardless of what the server may be configur +ed for my @dayofweek = (qw(Sunday Monday Tuesday Wednesday Thursday Friday Sa +turday)); my @monthnames = (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)) +; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday); #$ENV{TZ} = ':/usr/share/zoneinfo/Australia/Sydney'; $ENV{TZ} = ':/usr/share/zoneinfo/Europe/Paris'; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = localtime($TimeInSeco +nds); $year += 1900; print "This date is $dayofweek[$wday], $monthnames[$mon] $mday, $year\ +n"; print "This time is $hour:$min:$sec\n";

for some reason I can't figure, it prints the following:
This date is Thursday, Jan 1, 1970
This time is 1:0:0

but the server time is set correctly to todays date. Can some kind monk point out my mistake(s)?

Replies are listed 'Best First'.
Re: different time zones
by jethro (Monsignor) on Aug 07, 2008 at 01:29 UTC
    Please use warnings;. If you had done that, you would have seen these warning messages:

    Name "main::TimeInSeconds" used only once: possible typo at ./t7.pl li +ne 11. Use of uninitialized value in localtime at ./t7.pl line 11.
    1970 is the start of the time scale in Unix or the date you get when you call localtime(0). Maybe this should be localtime(time()); in line 11 instead.
      Ah.. thank you for the 'warnings' tip. Your fix is correct. I'm still a newbie at perl :-)
Re: different time zones
by Illuminatus (Curate) on Aug 07, 2008 at 04:46 UTC
    You could also use Time::Timezone, which basically does the same thing.