Reputation: 11
perl guru's
I got a question, I need to print the log file between two date range, that includes times as well.
for instance
log file btw (Current date & time and 1 week ago) or
log file btw (Current date & time and 1 month ago)
this is how my log file look.
02/12/15 13:05 some text here
02/11/15 12:05 some text here
02/10/15 10:05 some text here
02/09/15 09:05 some text here
02/08/15 08:05 some text here
02/07/15 01:05 some text here
--
-
-o/p truncated
-
01/29/15 13:05 some text here
01/29/15 12:05 some text here
01/29/15 10:05 some text here
01/29/15 09:05 some text here
01/29/15 08:05 some text here
01/29/15 01:05 some text here
Upvotes: 1
Views: 524
Reputation: 53478
So, to accomplish this what you need is:
Time::Piece
- which is a core module that lets you extract the numeric value of the respective dates.
In particular - Time::Piece
has strptime
which converts formatted time to a comparable value.
E.g.
#!/usr/bin/perl
use strict;
use warnings;
use Time::Piece;
while (<DATA>) {
my ( $date, $time ) = split;
my $t = Time::Piece->strptime( "$date $time", "%m/%d/%y %H:%M" );
print $t, "\n";
}
__DATA__
01/29/15 13:05 some text here
01/29/15 12:05 some text here
01/29/15 10:05 some text here
01/29/15 09:05 some text here
01/29/15 08:05 some text here
01/29/15 01:05 some text here
You can then use $t
in comparisons, in order to figure out relative timing. (It's a special value, such that if you compare it numerically, it works as 'epoch' time.
E.g.:
my $one_week_ago = time() - 7 * 24 * 60 * 60;
if ( $t < $one_week_ago ) { #do something };
Upvotes: 1