For comparison, you should use timestamps / epochs. Here is an example:
use DateTime::Format::Strptime;
use DateTime;
my $year = DateTime->now->year;
my $date_parser = DateTime::Format::Strptime->new(
pattern => '%Y %B %d',
);
my $start_date = 'June 10';
my $end_date = 'June 20';
my $start_epoch = $date_parser->parse_datetime("$year $start_date")
->epoch();
my $end_epoch = $date_parser->parse_datetime("$year $end_date")
->add( days => 1 )
->epoch();
my $parser = DateTime::Format::Strptime->new(
pattern => '%Y %b %d %T',
);
print "Start Epoch : $start_epoch [ $start_date ]\n";
print "End Epoch : $end_epoch [ $end_date ]\n";
for my $log_date ('May 27 09:33:33',
'Jun 05 09:33:33',
'Jun 10 09:33:33',
'Jun 20 09:33:33',
'Jun 30 09:33:33',) {
my $epoch = $parser->parse_datetime("$year $log_date")->epoch();
print "Log Epoch : $epoch [ $log_date ]\n";
if ( $start_epoch <= $epoch and $epoch < $end_epoch) {
print "==> Log Epoch is in range\n";
}
}
Outputs the following:
Start Epoch : 1433894400 [ June 10 ]
End Epoch : 1434844800 [ June 20 ]
Log Epoch : 1432719213 [ May 27 09:33:33 ]
Log Epoch : 1433496813 [ Jun 05 09:33:33 ]
Log Epoch : 1433928813 [ Jun 10 09:33:33 ]
==> Log Epoch is in range
Log Epoch : 1434792813 [ Jun 20 09:33:33 ]
==> Log Epoch is in range
Log Epoch : 1435656813 [ Jun 30 09:33:33 ]
, unix (jan 1, 1970), , , , . . :
- , , , , .
, - , () :
use strict;
use warnings;
my %months = ( jan => 1, feb => 2, mar => 3, apr => 4,
may => 5, jun => 6, jul => 7, aug => 8,
sep => 9, oct => 10, nov => 11, dec => 12 );
my $year = 2015;
my @log_dates = (
'May 27 09:33:33',
'Jun 05 09:33:33',
'Jun 10 09:33:33',
'Jun 20 09:33:33',
'Jun 30 09:33:33',
);
my $start_date = 'June 10';
my $end_date = 'June 20';
my $start_canonical = canonical_date_for_mmmdd_hhmmss("$year $start_date 00:00:00");
my $end_canonical = canonical_date_for_mmmdd_hhmmss("$year $end_date 23:59:59");
for my $log_date (@log_dates) {
my $canonical_date = canonical_date_for_mmmdd_hhmmss("$year $log_date");
print "Log Canonical Date : $canonical_date [ $log_date ]\n";
if ($start_canonical <= $canonical_date and
$canonical_date <= $end_canonical) {
print "===> Date in range\n";
}
}
sub canonical_date_for_mmmdd_hhmmss {
my ($datestr) = @_;
my ($year, $mon, $day, $hr, $min, $sec) =
$datestr =~ m|^(\d+)\s+(\w+)\s+(\d+)\s+(\d+):(\d+):(\d+)$|;
$year > 1900
or die "Unable to handle year '$year'";
my $month_first_three = lc( substr($mon,0,3) );
my $month_num = $months{$month_first_three};
defined $month_num
or die "Unable to handle month '$mon'";
(1 <= $day and $day <= 31)
or die "Unable to handle day '$day'";
(0 <= $hr and $hr <= 23)
or die "Unable to handle hour '$hr'";
(0 <= $min and $min <= 59)
or die "Unable to handle minute '$min'";
(0 <= $sec and $sec <= 59)
or die "Unable to handle second '$sec'";
my $fmt = "%04d%02d%02d%02d%02d%02d";
return sprintf($fmt, $year, $month_num, $day, $hr, $min, $sec);
}
:
Log Canonical Date : 20150527093333 [ May 27 09:33:33 ]
Log Canonical Date : 20150605093333 [ Jun 05 09:33:33 ]
Log Canonical Date : 20150610093333 [ Jun 10 09:33:33 ]
===> Date in range
Log Canonical Date : 20150620093333 [ Jun 20 09:33:33 ]
===> Date in range
Log Canonical Date : 20150630093333 [ Jun 30 09:33:33 ]
. ISO 8601 ( ) / .