Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Comparing timestamps that are in 2 different but defined styles

by thunders (Priest)
on Jul 23, 2010 at 21:52 UTC ( #851094=note: print w/ replies, xml ) Need Help??


in reply to Comparing timestamps that are in 2 different but defined styles

POSIX::strptime look promising. Something like this (untested)

use POSIX::strptime; my $timestamp = "2010-07-23 19:44:43.523"; #remove milliseconds $timestamp =~ s/\.\d+$//; my ($t_sec, $t_min, $t_hour, $t_mday, $t_mon, $t_year, $t_wday, $t_yda +y) = POSIX::strptime($timestamp, "%Y-%m-%d %H:%M:%S"); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); if($t_year == $year && $t_mon == $mon && $t_mday == $mday && $t_hour == $hour && abs($t_min-$min) <= 1){ #dates match }


Comment on Re: Comparing timestamps that are in 2 different but defined styles
Download Code
Re^2: Comparing timestamps that are in 2 different but defined styles
by AnomalousMonk (Abbot) on Jul 24, 2010 at 01:04 UTC
    if($t_year == $year && $t_mon == $mon && $t_mday == $mday && $t_hour == $hour && abs($t_min-$min) <= 1){ ... }

    How about ten seconds before the hour/day/etc versus ten seconds after: Are these two times within 60 seconds of each other per this test?

Re^2: Comparing timestamps that are in 2 different but defined styles
by apl (Monsignor) on Jul 24, 2010 at 12:01 UTC
    Concept is great, but OP just has to be careful when implementing it, as edge conditions always bite us. 8:59:01 compared to 9:00:00 is less than a minute to my eye, but wouldn't pass the generally correct test.

    Similar problem with 9:00:59 and 9:01:01, when you're comparing seconds ...

Re^2: Comparing timestamps that are in 2 different but defined styles
by thunders (Priest) on Jul 28, 2010 at 19:52 UTC

    Doh, good point guys. I wasn't thinking.

    Here's another option: turn the values returned by POSIX::strptime into a UNIX epoch with Time::Local, then subtracting that from the value given by time() and seeing if abs($diff) < 60.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-11-23 21:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (134 votes), past polls