in reply to Time::Piece reversibility

I'm using the latest Time::Piece, 1.31

I'm using 1.31_04 and at least in that, strftime works for me for your second spin (which as the only one using localtime makes sense):

#!/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 " strftime: ", $obj->strftime, "\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 " strftime: ", $obj->strftime, "\n"; # Has non-UTC timezone +- yay! 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"; print " strftime: ", $obj->strftime, "\n";

BTW, are you aware that the 2 dates you are using are a month apart?

Replies are listed 'Best First'.
Re^2: Time::Piece reversibility
by mla12 (Acolyte) on Jun 17, 2017 at 23:05 UTC

    I did try _04 too. I hadn't noticed the TZ difference, but it's still showing as 5pm rather than 10am:

    strftime: Mon, 19 Jun 2017 17:07:42 PDT

    And no, I forgot about the month being zero based for the test. Thx.

      it's still showing as 5pm rather than 10am:

      Yes, that's definitely the problem. It seems that localtime is changing the epoch as well as setting the offset so you're always getting back to the same displayed hour when you apply both (and gmtime is of course doing neither). Good spot.