Hello, I have a script using Time::Piece that compares dates/times to localtime. I am getting some unexpected behavior which I've tracked to a 7 hour difference in time zones (I'm on the U.S. west coast).

My snippet:

use Time::Piece; use Time::Seconds; use strict; my $dateformat = "%H:%M:%S, %m/%d/%Y"; my $date1 = "11:56:00, 09/17/2022"; my $date2 = "14:31:00, 09/16/2022"; my $date3 = "11:20:00, 09/13/2022"; my $t = localtime; print "Current time: \n", $t, $/,$/; $date1 = Time::Piece->strptime($date1, $dateformat); $date2 = Time::Piece->strptime($date2, $dateformat); $date3 = Time::Piece->strptime($date3, $dateformat); my(@ds) = ($date1,$date2,$date3); foreach my $dt (@ds){ print $dt, $/; if($dt < $t){ print " in the past...\n"; } else{ print " in the future...\n"; } }

This results in:

Current time: Fri Sep 16 07:39:44 2022 Sat Sep 17 11:56:00 2022 in the future... Fri Sep 16 14:31:00 2022 in the past... Tue Sep 13 11:20:00 2022 in the past...

As you can see the 2nd date for Fri Sep 16 should be "in the future...". If I change $t to gmtime I get the behavior I want, but I don't want to use gmtime for comparison. Is there a way to do this with localtime? Thank you.


In reply to Time::Piece and localtime? by slugger415

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.