File Coverage

blib/lib/SyslogScan/ParseDate.pm
Criterion Covered Total %
statement 3 27 11.1
branch 0 22 0.0
condition 0 3 0.0
subroutine 1 3 33.3
pod n/a
total 4 55 7.2


line stmt bran cond sub pod time code
1             package main;
2              
3             require 'timelocal.pl';
4              
5             package SyslogScan::ParseDate;
6 6     6   32 use strict;
  6         10  
  6         3522  
7              
8             my $gDefaultYear;
9             sub setDefaultYear
10             {
11 0     0     $gDefaultYear = shift;
12             }
13              
14             my $ONE_MONTH = 30*24*60*60;
15             my $ELEVEN_MONTH = 11 * $ONE_MONTH;
16              
17             my @gMonthList = qw ( jan feb mar apr may jun jul aug sep oct nov dec );
18             my %gMonthTable = ();
19             my ($i, $month);
20             foreach $month (@gMonthList)
21             {
22             $gMonthTable{$month} = $i++;
23             }
24              
25             sub parseDate {
26 0     0     my $date = shift;
27              
28 0           my $defaultYear = $gDefaultYear;
29              
30             # assume pure number is already in time_t format
31 0 0         return $date if ($date !~ /[^\d]/);
32              
33 0 0         if ($date =~ /(\d\d?)\.(\d\d?)\.(\d\d) (\d\d):(\d\d):(\d\d)/)
34             {
35             # 06.01.96 01:20:30 is June 1 1996 at 1:20:30
36             #print STDERR "$6,$5,$4,$2,$1,$3 resolves to ", scalar(localtime(timelocal($6,$5,$4,$2,$1,$3))), "\n";
37 0           return ::timelocal($6,$5,$4,$2,$1-1,$3);
38             }
39              
40             # assume date is in syslog format
41 0 0         my ($engMonth, $mday, $year, $hour, $min, $sec) =
42             $date =~ /(\w\w\w) ?(\d\d?)( \d\d\d\d)? (\d\d):(\d\d):(\d\d)/ or
43             die "unknown date format: $date";
44              
45 0 0         $year -= 1900 if defined $year; #compatibility with timelocal()
46            
47 0           $engMonth =~ tr/A-Z/a-z/;
48 0           my $mon;
49 0 0         defined ($mon = $gMonthTable{$engMonth}) or
50             die "unknown month: $engMonth";
51              
52 0 0         if (! defined($year))
53             {
54             # no year was specified, look for default
55 0 0         if (defined($defaultYear))
56             {
57 0 0 0       $defaultYear > 1970 and $defaultYear < 2030 or
58             die "default year $defaultYear does not look right";
59 0           $year = $defaultYear - 1900;
60             }
61             else
62             {
63             # try to guess time closest to now
64 0           my $now = time;
65 0           $year = (localtime($now))[5];
66              
67 0           my $candidate = ::timelocal($sec, $min, $hour, $mday, $mon, $year);
68              
69 0 0         if (($candidate - $now) > $ONE_MONTH)
    0          
70             {
71             # log entry was probably made last year
72 0           $year--;
73             }
74             elsif (($now - $candidate) > $ELEVEN_MONTH)
75             {
76             # log entry was made 'next year', possible if
77             # computers on LAN have different times
78 0           $year++;
79             }
80             }
81             }
82              
83 0           my $parsedDate = ::timelocal($sec, $min, $hour, $mday, $mon, $year);
84 0 0         $parsedDate == -1 and die "could not parse date: $date";
85 0           return $parsedDate;
86             };
87              
88             1;