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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.