Best way to compare date strings
minixman
created: 2006-01-04 06:45:15
All i have a question about the best way to compare date strings. I have the following two dates.
my $date1 = "2006/01/03 03:59:59:671";
my $date2 = "2006/01/03 04:00:00:562";
Now would the best way to get how many seconds / milliseconds there are between them to do. First strip out the 2006 day month as they are the same ? or leave them and do
$date1 =~ s/://g;
$date2 =~ s/://g;
my $ordermilliseconds = $clsendtimestamp - $clrectimestamp;
my $ordersecs = $ordermilliseconds / 1000;
Re: Best way to compare date strings
created: 2006-01-04 07:01:43

Use the DateTime family of modules.

#!/usr/bin/perl

use strict;
use warnings;
use DateTime::Format::Strptime;

my $fmt = DateTime::Format::Strptime->new(pattern => '%Y/%m/%d %H:%M:%S:%3N');

my $date1 = $fmt->parse_datetime('2006/01/03 03:59:59:671');
my $date2 = $fmt->parse_datetime('2006/01/03 04:00:00:562');

my $diff = $date2 - $date1;

print $diff->in_units('nanoseconds'), "\n";
--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Re^2: Best way to compare date strings
created: 2006-01-04 07:36:58
Strange when i install the mods and run i get.
$ perl test.pl
Can't call method "in_units" on an undefined value at test.pl line 22.
Re^3: Best way to compare date strings
created: 2006-01-04 07:50:28
I downloaed the modules again and installed them, still get strange errors
$ perl test.pl
Scalar found where operator expected at (eval 4) line 1, near "%+$nanosecond"
        (Missing operator before $nanosecond?)
syntax error at (eval 4) line 1, near "%+$nanosecond"
Re^4: Best way to compare date strings
created: 2006-01-04 11:59:08

It looks like you might have cut and pasted the code from your browser instead of using the 'download' link. That will add extra '+' characters into the code where it get split on a line break.

--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Re: Best way to compare date strings
created: 2006-01-04 07:49:30
my @seconds = ();
for my $date ( $date1, $date2 ) {
    my @fields = ( split( /:/, $date );
    my $frac = pop @fields;
    my $sec = pop @fields;
    push @seconds, join( '.', ( $sec, $frac ) );
}
Re: Best way to compare date strings
created: 2006-01-04 10:04:55

In the TMTOWTDI mode:


#!/usr/local/bin/perl -d

use strict;
use warnings;

use Time::Local;
use Time::HiRes qw( tv_interval );

my $date1 = "2006/01/03 03:59:59:671";
my $date2 = "2006/01/03 04:00:00:562";

my $t1 = convert_time( $date1 );
my $t2 = convert_time( $date2 );

my $elapsed = tv_interval( $t1, $t2 );
print $elapsed, "\n";

sub convert_time {
  my $date = shift;
  my( $yr, $mo, $dy ) = split( /\//, substr( $date, 0, 10 ) );
  my( $hr, $mn, $sc, $mi ) = split( /:/,  substr( $date, 11 ) );
  my $epoch = timelocal( $sc, $mn, $hr, $dy, $mo-1, $yr-1900 );
  [ $epoch, $mi*1000 ];
}


-derby
Re^2: Best way to compare date strings
created: 2006-01-04 11:25:18
That looks good

perlmonks.org content © perlmonks.org and Anonymous Monk, davorg, derby, minixman

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03