Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comparing timestamps that are in 2 different but defined styles

by technojosh (Priest)
on Jul 23, 2010 at 20:03 UTC ( #851089=perlquestion: print w/ replies, xml ) Need Help??
technojosh has asked for the wisdom of the Perl Monks concerning the following question:

I have the following problem, and I've put *a little* thought and search into the solution but nothing is smacking me in the face just yet:

I maintain an automated test tool that validates a web app sitting on a MSSQL db. We have added a set of 'audit' tables to our MSSQL database, and these tables all have 'DateTime' columns, which give dates in the format:

2010-07-23 19:44:43.523

I need to compare that date with Perl's localtime(time) value, and decide if this stamp is within ~1 minute of the value stored in the database.

FYI: on my system (windows7), localtime(time) returns the following format:

perl -e "print localtime(time).$/" Fri Jul 23 14:57:39 2010

Comment on Comparing timestamps that are in 2 different but defined styles
Select or Download Code
Replies are listed 'Best First'.
Re: Comparing timestamps that are in 2 different but defined styles
by Corion (Pope) on Jul 23, 2010 at 20:08 UTC

    Maybe you want to bring both timestamps into the same format, and then compare them? See POSIX::strftime, and localtime.

Re: Comparing timestamps that are in 2 different but defined styles
by JediWizard (Deacon) on Jul 23, 2010 at 20:31 UTC

    There are also a number of CPAN modules that can parse and reformat date/timestamps into a common format. Date::Manip I belive also has date comparison functions.


    They say that time changes things, but you actually have to change them yourself.

    —Andy Warhol

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

    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 }
      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 ...

      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?

      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.

Re: Comparing timestamps that are in 2 different but defined styles
by JavaFan (Canon) on Jul 23, 2010 at 23:38 UTC
    Why not have the database do the comparison?
Re: Comparing timestamps that are in 2 different but defined styles
by kp2a (Sexton) on Jul 24, 2010 at 01:36 UTC
    Date::Manip understands just about any format and computes just about any way you like. I have used it for a decade!
Re: Comparing timestamps that are in 2 different but defined styles
by intel (Beadle) on Jul 24, 2010 at 15:41 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2015-08-29 04:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...













    Results (343 votes), past polls