Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Time matching YYYY-MM-DD HH:MM:SS.SSS

by PRyanRay (Novice)
on Jul 26, 2012 at 17:32 UTC ( #983886=perlquestion: print w/replies, xml ) Need Help??
PRyanRay has asked for the wisdom of the Perl Monks concerning the following question:

Hello, everybody! I am trying to compare dates of the form YYYY-MM-DD HH:MM:SS.SSS. I am pasing some files that have data based on these timestamps. IOnce I have the strings I am running into problems. Given,
$string1 = "2008/11/22 01:40:00.000"; $string2 = "2008/11/22 01:45:00.000";
I want to be able to compare so I know which input file is earlier, something like:
if($string1 eq $string2){$early = $ARGV[0]; $late = $ARGV[1];} if($string1 lt $string2){$early = $ARGV[0]; $late = $ARGV[1];} if($string1 gt $string2){$early = $ARGV[1]; $late = $ARGV[0];}
But this is not consistent and does not always work. Is there a better way to compare and execute code once I know which file is earlier? Thanks in advance, I'm a newbie!

Replies are listed 'Best First'.
Re: Time matching YYYY-MM-DD HH:MM:SS.SSS
by aitap (Deacon) on Jul 26, 2012 at 17:55 UTC
    Try using Date::Parse:
    use Test::Simple tests => 2; use Date::Parse; my $time1 = str2time "2008/11/22 01:40:00.000"; my $time2 = str2time "2008/11/22 01:40:00.000"; ok(defined $time1 && defined $time2); ok($time1 == $time2);
    Sorry if my advice was wrong.
Re: Time matching YYYY-MM-DD HH:MM:SS.SSS
by dave_the_m (Prior) on Jul 26, 2012 at 18:27 UTC
    The date format you've shown should be self-sorting, so there shouldn't be any need to parse the dates (barring possible DST issues). Can you give a concrete example (code plus data) where the sorting is "inconsistent"?


      Hi Dave, Here is a bit more code:
      $earliest0 = `more $ARGV[0]| grep -v Epoch > newfile.txt| head -1 newf +ile.txt`; @Array0 = split(' ',$earliest0); $date0 = "@Array0[0] @Array0[1]"; $earliest1 = `more $ARGV[1]| grep -v Epoch > newfile1.txt| head -1 new +file1.txt`; @Array1 = split(' ',$earliest1); $date1 = "@Array1[0] @Array1[1]"; if($date0 eq $date1){$early = $ARGV[0]; $late = $ARGV[1];} if($date0 lt $date1){$early = $ARGV[0]; $late = $ARGV[1]; $File = "newfile.txt"; open(File); $i = 0; while(<File>){ @check = split(' ',$_); $date = "@check[0] @check[1]"; if($date eq $date1){last;} $i++; } } if($date1 lt $date0){$early = $ARGV[1]; $late = $ARGV[0]; $File = "newfile1.txt"; open(File); $i = 0; while(<File>){ @check = split(' ',$_); $date = "@check[0] @check[1]"; if($date eq $date0){last;} $i = $i++; } }
      The  while statements are needed to determine which line number matches the latest date. I tried using regular expressions to do this but could not figure it out. When I execute this script it sometimes returns the correct $i, sometimes executes the wrong $if statement. Thanks!
        Well, I asked for code and data that demonstrates the inconsistency; you didn't show any data.

        Having said that, your code is ... well .. just God-awful.

        It violates just about every rule of good coding practice and style going. I haven't the will to go though a complete list (maybe some other kind soul will have the patience), but just for starters:


        use warnings; use strict;
        at the start of your code. You will be amazed!

        The two-arg form of open is frowned upon. The one-arg form is vomit inducing.

        In your second branch, you have $i = $i++; which is definitely not what you need, and may in fact be the cause of all your issues.


Re: Time matching YYYY-MM-DD HH:MM:SS.SSS
by Bloodnok (Vicar) on Jul 27, 2012 at 12:34 UTC
    Date::Calc has always done the trick for me for problems such as this ... well that [Data::Calc] and judicous use of sort.

    A user level that continues to overstate my experience :-))

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://983886]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2017-12-12 18:56 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (335 votes). Check out past polls.