That's very helpful, @cguevara, thank you.

So to summarize that result, Time::Piece->strptime always returns a gmtime instance (tzoffset = 0). If you want it in localtime, you need to convert that to a localtime instance. Correct?

My follow-up question, then, is how to achieve that same result if the time zone is not part of the date string? IOW, I want to parse "2017-06-19 10:07:42" into a localtime Time::Piece instance with epoch seconds of 1497892062 and a tzoffset of -25200.

I can use Time::Local and do this:

$ENV{'TZ'} = 'America/Los_Angeles'; my $epoch = timelocal(42,07,10,19,05,2017); print "timelocal epoch: $epoch\n"; print "timelocal date: ", scalar localtime($epoch), "\n";

Produces:

timelocal epoch: 1497892062 timelocal date: Mon Jun 19 10:07:42 2017

But since strptime() always returns gmtime, is there a way to do that with Time::Piece currently? It will be treated as UTC and converting it to a localtime() will offset it by 7 hours (in this case).

#!/usr/bin/env perl use strict; use warnings; use Time::Local; use Time::Piece; $ENV{'TZ'} = 'America/Los_Angeles'; my $datestr = '2017-06-19 10:07:42'; my $gmt = Time::Piece->strptime($datestr, '%Y-%m-%d %H:%M:%S'); my $local = localtime($gmt->epoch); print "gmt epoch: ", $gmt->epoch, "\n", "local epoch: ", $local->epoch, "\n", "datestr: $datestr\n", "T::P gmt strftime: ", $gmt->strftime, "\n", "T::P local strftime: ", $local->strftime, "\n" ;

Produces

gmt epoch: 1497866862 local epoch: 1497866862 datestr: 2017-06-19 10:07:42 T::P gmt strftime: Mon, 19 Jun 2017 10:07:42 UTC T::P local strftime: Mon, 19 Jun 2017 03:07:42 PDT

Of course you could always append the zone that you want to the date string, but that's cheating ;)


In reply to Re^2: Time::Piece reversibility by mla12
in thread 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.