Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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!

Comment on Time matching YYYY-MM-DD HH:MM:SS.SSS
Select or Download Code
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 (Parson) 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"?

    Dave.

      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:

        Add

        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.

        Dave.

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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://983886]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-09-02 21:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (29 votes), past polls