Reproduction of the "error"
This issue occurs because strptime uses UTC instead of the local time zone. This can be demonstrated in the following code, which takes the current time, prints it, then repeats it and shows the difference:
use strict;
use warnings;
use Time::Piece;
my $now = Time::Piece->new();
print $now->strftime(), "\n";
my $fmt = "%Y-%m-%d %H:%M:%S";
my $nowstr = $now->strftime($fmt);
my $parsed = Time::Piece->strptime("$nowstr", $fmt);
print "($nowstr)\n";
print $parsed->strftime(), "\n";
my $diff = $now - $parsed;
print $diff->hours, " hours difference\n";
Outputs:
Wed, 26 Mar 2014 21:42:08 Pacific Daylight Time
(2014-03-26 21:42:08)
Wed, 26 Mar 2014 21:42:08 UTC
7 hours difference
One hacky solution is to get parsed read time as local
, , perl. strptime
: $now->strptime
.
my $nowstr = "2014-03-26 21:51:00";
my $parsed = $now->strptime("$nowstr", $fmt);
print "($nowstr)\n";
print $parsed->strftime(), "\n";
my $diff = $now - $parsed;
print $diff->hours, " hours difference\n";
, strptime
, , , 6 . :
Wed, 26 Mar 2014 21:57:00 Pacific Daylight Time
(2014-03-26 21:51:00)
Wed, 26 Mar 2014 21:51:00 Pacific Standard Time
0.1 hours difference
c_islocal
$now
. $now
localtime
->new()
, gmtime
, .
, , DST , - . , strptime
, _mktime
new
.
, -, Time::Piece
, .