Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare string date time in perl?

Tags:

perl

I have this date time format in string "11:56:41, 11/22/2011".

Here's what I want:

Compare two date time strings like.

$date1 = "11:56:41, 11/22/2011";
$date2 = "11:20:41, 11/20/2011";
if($date2 < $date1) {
    do something...
} else {
    do nothing...
}

Any ideas how could i achieve this in perl?

like image 524
quinekxi Avatar asked Nov 22 '11 04:11

quinekxi


4 Answers

One more solution that uses the overloaded comparisons after converting the times to Time::Piece objects. Creating the objects may be overkill for something simple, but they can become very useful if you need to do other things with the times.

use Time::Piece;

my $dateformat = "%H:%M:%S, %m/%d/%Y";

my $date1 = "11:56:41, 11/22/2011";
my $date2 = "11:20:41, 11/20/2011";

$date1 = Time::Piece->strptime($date1, $dateformat);
$date2 = Time::Piece->strptime($date2, $dateformat);

if ($date2 < $date1) {
    do something...
} else {
    do nothing...
}
like image 100
stevenl Avatar answered Nov 14 '22 18:11

stevenl


An efficient method is to reorder the fields to something lexically comparable.

sub to_comparable {
   my ($date) = @_;
   my ($H,$M,$S,$d,$m,$Y) = $date =~ m{^([0-9]{2}):([0-9]{2}):([0-9]{2}), ([0-9]{2})/([0-9]{2})/([0-9]{4})\z}
      or die;
   return "$Y$m$d$H$M$S";
}

if (to_comparable($date2) lt to_comparable($date1)) {
   ...
} else {
   ...
}
like image 44
ikegami Avatar answered Nov 14 '22 18:11

ikegami


What, already 4 hours and not a single DateTime (all hail the mighty DateTime) answer in sight? You're slacking, perl subscribers… ☻

use DateTime::Format::Strptime qw();
my $p = DateTime::Format::Strptime->new(pattern => '%T, %D', on_error => 'croak',);

my $date1 = $p->parse_datetime('11:56:41, 11/22/2011');
my $date2 = $p->parse_datetime('11:20:41, 11/20/2011');

if($date2 < $date1) {
    say "$date2 comes before $date1";
} else {
    say "$date2 does not come before $date1";
}

The method parse_datetime returns instances of DateTime whose comparison operators and stringification are overloaded to DTRT.

like image 9
daxim Avatar answered Nov 14 '22 17:11

daxim


Convert the datetimes (in your case these are local datetimes because they have no time zones) into ISO8601 then you can do regular string comparison.

To perform the conversion, you should extract the six components from your format

HH:MM:SS, mm/DD/YYYY

and reassemble them into ISO 8601:

YYYY-MM-DDTHH:MM:SS

Then a normal lexicographic comparison will work.

See http://codepad.org/berle9um

Repeated here:

sub my_format_to_iso8601 {
    $_[0] =~ /(\d\d):(\d\d):(\d\d), (\d\d)\/(\d\d)\/(\d\d\d\d)/;
    return "$6-$4-$5T$1:$2:$3";
}

$date1 = "11:56:41, 11/22/2011";
$date2 = "11:20:41, 11/20/2011";
$d1 = my_format_to_iso8601($date1);
$d2 = my_format_to_iso8601($date2);
print "first is $d1\n";
print "second is $d2\n";
if ($d2 < $d1) {
    print "second is earlier\n";
} else {
    print "first is earlier\n";
}

ADDENDUM

  • ikegami's Perl code is much better.
  • A date library would be your friend here; simply specify a format string and use the library's parse function to get your date object, which it should then be able to compare directly. (That said, it is always fun to point out that ISO8601 is, by design, sortable in string form.)
like image 2
Ray Toal Avatar answered Nov 14 '22 16:11

Ray Toal