I have a load of date/time strings that have the following format
Your approach using yyyymmdd as the sorting string may not be adequate if more than one of those date/time strings falls on the same day. If this is likely to be a problem you should consider transforming the date/time into an epoch time value using the timelocal() subroutine in the Time::Local module. This can then be sorted numerically.
use strict; use warnings; use Time::Local; my %monthNumber = ( Jan => 0, Feb => 1, Mar => 2, Apr => 3, May => 4, Jun => 5, Jul => 6, Aug => 7, Sep => 8, Oct => 9, Nov => 10, Dec => 11, ); my $rxDate = qr {(?x) ^ (\D{3}) \s+ (\d\d?) \s+ (\d{4}) \s+ (\d\d): (\d\d): (\d\d): (\d{3}) ([AP]M) }; my @dateStrs = ( q{Oct 16 2004 11:09:19:943AM}, q{Mar 3 2007 10:30:31:170PM}, q{Oct 16 2004 09:27:24:012PM}, q{Jun 4 2005 03:13:45:508AM}, ); print map { qq{$_->[0]\n} } sort { $a->[1] <=> $b->[1] } map { my $hr = $_->[4]; $hr += 12 if $_->[8] =~ m{^P}; my $timeVal = timelocal( $_->[6], $_->[5], $hr, $_->[2], $monthNumber{$_->[1]}, $_->[3] - 1900) + $_->[7] / 1000; [ $_->[0], $timeVal ] } grep { defined $_->[1] } map { [ $_, m{$rxDate} ] } @dateStrs;
and the output is
Oct 16 2004 11:09:19:943AM Oct 16 2004 09:27:24:012PM Jun 4 2005 03:13:45:508AM Mar 3 2007 10:30:31:170PM
I hope this is of use.
Cheers,
JohnGG
In reply to Re: Pattern matching for extracting dates
by johngg
in thread Pattern matching for extracting dates
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |