Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: How to calculate earliest date from log file

by mbethke (Hermit)
on Nov 11, 2012 at 04:27 UTC ( #1003298=note: print w/replies, xml ) Need Help??

in reply to How to calculate earliest date from log file

The easiest if not fastest way would be Date::Parse:
$ perl -MDate::Parse -E'say scalar gmtime str2time("08/Oct/2007:04:54: +20 -0400");' Mon Oct 8 08:54:20 2007

str2time() gives you the seconds since the epoch that you can easily compare.

Because Date::Parse has to do its format guessing every time you call the parsing function, you'd be much faster using a hand-crafted regexp with a little hash like

%months = ( Jan => '01', Feb => '02', ... ); s!(..)/(...)/(....):(..):(..):(..)!$3.$months{$2}.$1.$4.$5.$6!e;
or something along these lines, maybe correcting for the time zone if these should be allowed differ too in your log lines. However, if you're doing anything significant with your data afterwards, the speedup may not even be noticeable. A program I wrote in 2008 uses Date::Parse and parses millions and millions of lines a day---it was just never worth fixing and testing the fix for such a little speed gain.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003298]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2017-09-21 11:34 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (245 votes). Check out past polls.