I'm trying to parse a timestamp with Time::Piece, and then stringify the result back into the original date string.
The timestamp includes the zone. I can do it with Time::Local and it works as expected. But with Time::Piece, no matter what I seem to try, the result is displayed as GMT.
See this test:
#!/usr/bin/env perl use strict; use warnings; use Time::Local; use Time::Piece; my $datestr = '2017-06-19 10:07:42-0700'; print "Original date string: $datestr\n\n"; print "Using Time::Local\n"; my $time = timelocal(42, 7, 10, 19, 6, 2017); print " Epoch seconds: $time\n"; print " CORE::localtime: ", scalar CORE::localtime($time), "\n"; print "\n"; print "Using Time::Piece:\n"; print " strptime as static method:\n"; my $obj = Time::Piece->strptime($datestr, '%Y-%m-%d %H:%M:%S%z'); print " stringified: $obj\n"; print " datetime: ", $obj->datetime, "\n"; print " tzoffset: ", $obj->tzoffset, "\n"; print " hour: ", $obj->hour, "\n"; print " strptime as localtime instance method:\n"; $obj = localtime->strptime($datestr, '%Y-%m-%d %H:%M:%S%z'); print " stringified: $obj\n"; print " datetime: ", $obj->datetime, "\n"; print " tzoffset: ", $obj->tzoffset, "\n"; print " hour: ", $obj->hour, "\n"; print " strptime as gmtime instance method:\n"; $obj = gmtime->strptime($datestr, '%Y-%m-%d %H:%M:%S%z'); print " stringified: $obj\n"; print " datetime: ", $obj->datetime, "\n"; print " tzoffset: ", $obj->tzoffset, "\n"; print " hour: ", $obj->hour, "\n";
For me, that produces:
Original date string: 2017-06-19 10:07:42-0700 Using Time::Local Epoch seconds: 1500484062 CORE::localtime: Wed Jul 19 10:07:42 2017 Using Time::Piece: strptime as static method: stringified: Mon Jun 19 17:07:42 2017 datetime: 2017-06-19T17:07:42 tzoffset: 0 hour: 17 strptime as localtime instance method: stringified: Mon Jun 19 17:07:42 2017 datetime: 2017-06-19T17:07:42 tzoffset: -25200 hour: 17 strptime as gmtime instance method: stringified: Mon Jun 19 17:07:42 2017 datetime: 2017-06-19T17:07:42 tzoffset: 0 hour: 17
The CORE::localtime result is what I want. But how do I get that back out of the Time::Piece instances?
I'm using the latest Time::Piece, 1.31
In reply to Time::Piece reversibility by mla12
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |