sjessie has asked for the wisdom of the Perl Monks concerning the following question:
Greetings All. I thought this should be really simple using Time::Piece, however I am not getting the result I would expect. As part of a larger script, I just want find if a datetime in a specific format is within the last 30 minutes.
use strict; use warnings; use Time::Piece; my $ctime = Time::Piece->new; my $oldtime = Time::Piece->strptime('Dec 19 17:01:00 2016', '%b %d %T +%Y'); print "The current time is $ctime\n"; print "The old time is $oldtime\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"; }
I ran the script just now, and this is the output:
The current time is Mon Dec 19 17:07:06 2016 The old time is Mon Dec 19 17:01:00 2016 Half an hour ago is Mon Dec 19 16:37:06 2016 Old time is outside a half hour
It doesn't matter what time I set in the script, it always drops down to the else block. I though perhaps I saw something about strptime and GMT, but now I can't find that page again... I think I am probably totally frazzled at this point and am missing something simple. Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Simple (I thought) time comparison?
by davido (Cardinal) on Dec 19, 2016 at 23:40 UTC | |
|
Re: Simple (I thought) time comparison?
by hippo (Archbishop) on Dec 19, 2016 at 23:43 UTC | |
by sjessie (Novice) on Dec 20, 2016 at 00:18 UTC | |
by sjessie (Novice) on Dec 20, 2016 at 00:21 UTC | |
by sjessie (Novice) on Dec 20, 2016 at 00:26 UTC | |
by soonix (Chancellor) on Dec 20, 2016 at 09:16 UTC | |
|
Re: Simple (I thought) time comparison?
by haukex (Archbishop) on Dec 20, 2016 at 14:08 UTC | |
|
Re: Simple (I thought) time comparison?
by sjessie (Novice) on Dec 20, 2016 at 16:02 UTC |