Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Simple (I thought) time comparison?

by hippo (Bishop)
on Dec 19, 2016 at 23:43 UTC ( [id://1178143]=note: print w/replies, xml ) Need Help??


in reply to Simple (I thought) time comparison?

Replacing your time with one here which should trigger the if branch (rather than the else) I am unable to reproduce your findings.

$ perl 1178137.pl The current time is Mon Dec 19 23:40:37 2016 The old time is Mon Dec 19 23:31:00 2016 Half an hour ago is Mon Dec 19 23:10:37 2016 Old time is within a half hour

If you think you have timezone (or DST) problems it is always best to output the epoch times as a diagnostic aid. I suggest you give that a try.

Replies are listed 'Best First'.
Re^2: Simple (I thought) time comparison?
by sjessie (Novice) on Dec 20, 2016 at 00:18 UTC

    Strange... Or more likely I don't understand what is going on here, being a noob. Your suggestion about using the epochs helped me at least figure out a way around my problem. If I add:

    $oldtime = $oldtime - $ctime->tzoffset;

    I get the desired results, although my statement

    print "The old time is $oldtime\n";

    Then prints a formatted time 6 hours in the future. I needed to update that to

    print "The old time is ", $oldtime + $ctime->tzoffset, "\n";

    To get it to print the correct time. That isn't a great concern as I actually won't ever be using the print statements, only the calculations, in the bigger script I am retrofitting. Still, it shows I am missing something basic in how this all works and that concerns me.

      Note: where I am tzoffset is -21600 (-6 hours, CST).

        And further illustrating my noob-ness, I didn't include the entire updated script so it actually made sense. Sorry. Here it is:

        use strict; use warnings; use Time::Piece; my $ctime = Time::Piece->new; my $oldtime = Time::Piece->strptime('Dec 19 17:44:00 2016', '%b %d %T +%Y'); $oldtime = $oldtime - $ctime->tzoffset; print "The current time is $ctime\n"; print "The old time is ", $oldtime + $ctime->tzoffset, "\n"; my $halfago = $ctime - 1800; print "Half an hour ago is $halfago\n"; if ($oldtime > $halfago) { print "Old time is within a half hour\n"; } else { print "Old time is outside a half hour\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1178143]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-04-25 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found