localtime() not displaying correct time on Windows

Posted by abs on 2007-07-11 08:28
Forums: ActivePerl discussion | OS: Windows

I have a Windows box that is in EST (Daylight Savings Time enabled). The "date/tome" command via the command prompt and the Perl localtime() function return different times.

The date/time command via command prompt returns:
The current date is: Wed 07/11/2007

When executeing the following perl code on this same box (running ActiveState Perl 5.8.6 Build 811):

($seconds, $minutes, $hour, $dayofmonth, $month, $year, $dayofweek, $dayofyear, $dst) = localtime();

print $seconds, " Second\n";
print $minutes, " Minute\n";
print $hour, " Hour\n";
print $dayofmonth, " Day of Month\n";
print $month, " Month\n";
print $year, " Year\n";
print $dayofweek, " Day of week\n";
print $dayofyear, " Day of year\n";
print $dst, " DST\n";

37 Second
26 Minute
17 Hour
11 Day of Month
6 Month
107 Year
3 Day of week
191 Day of year

As you can see, the "hour" value returned by localtime is ahead by 5 hours.
Can you please help? Thanks in advance.

rblasch | Tue, 2007-07-17 08:46

Is your TZ environment variable set to anything?

abs | Tue, 2007-07-17 09:07



rblasch | Tue, 2007-07-17 09:42

Perl's localtime uses the the C Runtime function of the same name. Microsoft's implementation uses the TZ environment variable, see _tzset (CRT)

I guess it's best to unset it, but TZ=EST+5EDT (Eastern Standard Time, 5 Hours to UTC, Eastern Daylight Saving Time) should work for you too.

abs | Tue, 2007-07-17 11:17

Thanks! That seems to be the issue. I will work around this in my script. Thanks for your help.

sbatter | Thu, 2009-12-10 15:46

Sorry to bring up an old thread...

Same code that worked on 2003 doesn't work on 2008.
Anybody have this working?

$ENV{'TZ'} = "GMT-0BST"; # among others.

doesnt work.