reyjrar/Parse-Syslog-Line — pull request #1 by ag4ve (2013-04-24 10:45:31+00:00)
Title: allow for a date parser function

Add an epoch key.

Use a parser function for time.
$Parse::Syslog::Line::DateTimeCreate = 0;
$Parse::Syslog::Line::FmtDate = \&parse_func;

Then you can return information that datetime would.

It's >2x as fast in my script / sample log data.
datetime: 130.23s user 0.13s system 99% cpu 2:10.58 total
parse_func: 53.92s user 0.05s system 93% cpu 57.758 total

date parse function used:
sub epdate
{
my ($str) = @_;

my $curyear = strftime("%Y", localtime) - 1900;

# comply with localtime month enumeration
my %months = ('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 ($month, $day, $hour, $minute, $second) = $str =~ /
^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\ ? # Month
\ ?([1-9][0-9]?)?\ ? # Day
([0-2][0-9])? # Hour
(?::([0-5][0-9]))? # Minute
(?::([0-5][0-9]))? # Second
/x;

$month //= "Jan";
$day //= 1;
$hour //= 0;
$minute //= 0;
$second //= 0;

my $eptime = timegm(
$second,
$minute,
$hour,
$day,
$months{$month},
$curyear
);

my $date = $curyear . '-' . $months{$month} . '-' . $day;
my $time = $hour . ':' . $minute . ':' . $second;
my $date_str = $date . ' ' . $time;

return ($date, $time, $eptime, $date_str);
}