I have 2 linux machines that need to run a perl script and access a database.
For any reason, date itself is correct, but returning epoch from Perl has 1 hour difference resulting in having on one machine UTC epoch-timestamp and on the other the wanted CET.
date on A:
Tue Dec 11 13:34:09 CET 2012
date on B:
Tue Dec 11 13:33:20 CET 2012
To check whats going on I build a minimalistic example.
The script uses Time::HiRes and gathering the localtime->epoch to generate a date.
I build a minimalistic following script to output the behaviour:
#/usr/bin/perl
#
use Time::HiRes qw(time);
use Time::Piece;
#
my $date = Time::Piece->strptime(localtime->epoch,"%s");
print "$date->datetime"."\n";
print $date->tzoffset."\n";
print $date->epoch."\n";
Output Machine A:
Tue Dec 11 12:35:43 2012->datetime
0
1355229343
Output Machine B:
Tue Dec 11 13:34:25 2012->datetime
0
1355232865
So as you see something must be wrong with timezone or so. The differ with an hour. But I don’t know where to look and what to configure as date itself outputs the correct time.
This seems to be a bug in
Time::Piece->strptime(STRING, FORMAT). Here is the code in question:Let’s start here.
_strptimeis the operating system’s nativestrptimefunction. It appears to return local time, although that’s not documented anywhere.Okay, so we use our
_mktimemethod to turn the output of_strptimeinto aTime::Pieceobject. The second parameter is whether_mktimeshould interpret as local time or UTC. When called asTime::Piece->strptime(STRING, FORMAT),ref($time)will be false, and so_mktimewill be called with$islocal=0, i.e. that_strptimereturned a UTC time. This is wrong, and we’ve found the bug. (I don’t know enough about the C time functions to know how it should be done.)So you have to use
localtime->strptime(STRING, FORMAT). Except this will still fail on old versions of the module due to another bug in_mktime(my distribution comes with version 1.15 where this is still broken, but it’s fixed in 1.20).It’s not even a strange issue specific to
%s. It happens for anyTime::Piece->strptimecall: