Hi sjessie,

As hippo and davido have already identified, the issue is with time zones. Personally, I like to use the DateTime module since it includes a lot of functionality, including time zones. (Because of that it's a little more heavyweight, but that should only be noticeable if your script does a lot of date/time calculations, so personally I just throw all of my date/time stuff at the DateTime suite because it can handle it all, and only consider using other modules as an optimization if necessary.)

Anyway, here are two methods that work. Note that this handles time zones properly, for example try changing the first "America/New_York" to "America/Chicago". Update: Just to emphasize this, the key is that all the DateTime objects involved in the calculations and comparisons need to have their time zones set properly.

use warnings; use strict; use DateTime; use DateTime::Format::Strptime; my $old_str = 'Dec 19 17:01:00 2016'; # normally, $now = DateTime->now my $now = DateTime->new(year=>2016,month=>12,day=>19, hour=>17,minute=>7,second=>6,time_zone=>'America/New_York'); print "now=", $now->strftime('%Y-%m-%d-%H-%M-%S %Z'), "\n"; my $strp = DateTime::Format::Strptime->new( on_error=>'croak', pattern => '%b %d %T %Y', time_zone=>'America/New_York' ); my $old = $strp->parse_datetime($old_str); print "old=", $old->strftime('%Y-%m-%d-%H-%M-%S %Z'), "\n"; # approach one: difference my $diff_sec = $now->subtract_datetime_absolute($old) ->in_units('seconds'); print "diff_sec: $diff_sec (", sprintf("%.1f",$diff_sec/60), " minutes)\n"; # approach two: compare to another DateTime my $halfago = $now->clone->subtract(minutes=>30); print "halfago=", $halfago->strftime('%Y-%m-%d-%H-%M-%S %Z'), "\n"; # note comparators are overloaded for DateTime objects print "halfago ", ($halfago>$old?'>':'<='), " old\n";

Hope this helps,
-- Hauke D


In reply to Re: Simple (I thought) time comparison? by haukex
in thread Simple (I thought) time comparison? by sjessie

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.