/usr/share/perl5/Lire/Time.pm is in lire 2:2.1.1-2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | package Lire::Time;
use strict;
use base qw/ Exporter /;
use Time::Local;
use Time::Timezone;
use Carp;
use vars qw/ @EXPORT /;
BEGIN {
@EXPORT = qw/ date2cal syslog2cal clf2cal cal2rfc cal2ymdhms getMonthName /;
}
=pod
=head1 NAME
Lire::Time - parses and prints date in formats common to many log files.
=head1 SYNOPSIS
use Lire::Time qw/ syslog2cal /;
my @ltime = localtime;
while ( <LOGFILE> ) {
#...
my $time = syslog2cal( $m, $d, $t, \@ltime );
}
=head1 DESCRIPTION
This module supplies many functions to parse dates in formats that you
are likely to encounter in log files. It also offers many functions to format
epoch time in useful format.
=head2 NOTE ABOUT FUNCTION EXPORT
Altough all documented functions are exported by default to the caller
namespace, you should explicitely import the functions you require
since exporting by default isn't recommanded by the perl modules
guidelines.
=cut
# convert Jan and friends to localtime(3) tm_mon thingies
my %monthnumbers = (
'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 @monthnames = qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/;
my @daynames = qw/Sun Mon Tue Wed Thu Fri Sat/;
# convert Mar 20 09:13:32 to ($tm_sec, $tm_min, $tm_hour, $tm_day, $tm_month)
sub syslog2tm($$$)
{
my $sub = 'syslog2tm';
# month name (e.g. May) we tolerate e.g. jan
# day, possibly 0 padded
# time as hh:mm:ss or hh:mm:ss.sss
my ( $month, $day, $time ) = @_;
# process $time
# We tolerate an extra : at the end of the time.
# HH:MM:SS[.MSEC]:
my ( $hour, $min, $sec, $msec ) = $time =~ /^(\d\d):(\d\d):(\d\d)(\.\d+)?:?$/
or croak "$sub time '$time' should be hh:mm:ss[.msec]:?";
# convert from string to integers
my $tm_hour = $hour + 0;
my $tm_min = $min + 0;
my $tm_sec = $sec + 0; # get rid of leading zero
# process day
my $tm_day = $day + 0;
# process month
my $tm_month;
if ($month =~ /\d+/) {
$tm_month = $month;
} else {
$tm_month = $monthnumbers{ lc $month };
die("$sub cannot get monthnumber from monthname '$month'")
unless defined $tm_month;
}
return [ $tm_sec, $tm_min, $tm_hour, $tm_day, $tm_month ];
}
# iso 8601:1988 5.3.3.1 ,
# http://doc.mdcc.cx/~joostvb/doc/iso-8601:1988-representation_of_dates_and_times.pdf
sub tzdiff2sec($)
{
# e.g. +0100 or -0900 ; +hh:mm, +hhmm, or +hh
my ( $sign, $hour, $min ) = $_[0] =~ /^([+-])?(\d\d):?(\d\d)?$/
or croak "invalid tzdiff format: $_[0]. It must looks like +0100 or -01:00\n";
$sign ||= "+";
$hour ||= 0;
$min ||= 0;
my $sec = $hour * 60 * 60 + $min * 60;
$sec *= -1 if $sign eq '-';
return $sec;
}
=pod
=head1 DATE PARSING FUNCTIONS
This module includes several functions that convert between a more
human readable date format and UNIX epoch time. All parsing functions
will return the number of seconds since Jan 1 1970 00:00:00 UTC and
will die() when passed invalid arguments.
=head2 date2cal()
my $time = date2cal( $year, $month, $day, $time, [$timezone] );
my $time = date2cal( "2001 Mar 20 09:32:29 +0100" );
This function will convert a date in the date(1) default output format
to UNIX epoch time. The function accepts either the date in a string
or splitted on whitespace. If the timezone component is omitted, the
local timezone is assumed (usually based on the value of the TZ
environment variable).
=cut
# Tue, 20 Mar 2001 09:46:40 +0100 is Tue Mar 20 08:46:50 UTC 2001
#
# http://www.cl.cam.ac.uk/~mgk25/iso-time.html says:
#
# 12:00 UTC (aka 12:00Z) = 13:00+01:00 = 0700-0500
#
# There exists no international standard that specifies abbreviations for
# civil time zones like CET, EST, etc.
#
# nice to handle apache stuff like [18/Mar/2001:16:00:26 +0100]
#
sub date2cal($;$$$$)
{
my $sub = 'date2cal';
@_ = split /\s+/, $_[0] if @_ == 1;
croak "$sub give 1, 4 or 5 args" if @_ < 4;
# tzdiff e.g. +0100 or -0900 ; +hh:mm, +hhmm, or +hh
my ($year, $month, $day, $time, $tzdiff ) = @_;
if ( defined $tzdiff ) {
$tzdiff = tzdiff2sec( $tzdiff );
} else {
$tzdiff = tz_local_offset();
}
my $tm = syslog2tm($month, $day, $time);
# tm still has to get fixed with respect to $tzdiff
my $tm_year = $year - 1900;
push @$tm, ($tm_year);
# We use timegm because the time will be modified by tzdiff
my $cal = timegm( @$tm );
# If tzdiff is undefined, it will use the current time zone
return $cal - $tzdiff;
}
=pod
=head2 syslog2cal()
my $time = syslog2cal( $month, $day, $time, $local_tm_ref );
my $time = syslog2cal( "Mar 11 13:21:00", $local_tm_ref );
This function will convert a date in the syslog default output format
to UNIX epoch time. The function accepts either the date in a string
or splitted on whitespace.
Since the syslog format doesn't contain timezone information, the
local timezone is assumed (usually determined by the TZ environment
variable).
The last argument is a reference to an array returned by localtime().
my $local_tm_ref = [localtime()];
It is used to determine the year.
=cut
sub syslog2cal($$;$$)
{
my $sub = 'syslog2cal';
@_ = ( split( /\s+/, $_[0]), $_[1] )
if (@_ == 2 );
# month name (e.g. May) we tolerate e.g. jan
# day, possibly 0 padded
# time as hh:mm:ss or hh:mm:ss.sss
# reference point of now, to guess the year
my ( $month, $day, $time, $now_ltime ) = @_;
croak "$sub ltime arg should be a reference to array as returned by localtime()"
unless ref $now_ltime eq 'ARRAY' && @$now_ltime == 9;
my $tm = syslog2tm($month, $day, $time );
# Guess the year based on the difference between now and tm
my $tm_year;
my $now_year = $now_ltime->[5];
my $now_month = $now_ltime->[4];
my $now_day = $now_ltime->[3];
if ( $tm->[4] < $now_month ) {
# Current year, unless the month is january, the day the first
# and now is the last day of december
if ( $tm->[4] == 0 && $tm->[3] == 1 &&
$now_month == 11 && $now_day == 31)
{
$tm_year = $now_year + 1;
} else {
$tm_year = $now_year;
}
} elsif ( $tm->[4] == $now_month ) {
# Current year if the day is in the past, today, or one day ahead
# last year otherwise
if ( $tm->[3] <= ($now_day + 1) ) {
$tm_year = $now_year;
} else {
$tm_year = $now_year - 1;
}
} elsif ( $tm->[4] == $now_month + 1 && $tm->[3] == 1 ) {
# One month ahead: this means last year, unless we are on the
# last day of the month and it is the first day of the month
$tm_year = $now_year - 1;
if ( $now_month =~ /^(0|2|4|6|7|9|11)$/ && $now_day == 31) {
# Last day of jan, march, may, jul, aug, oct, dec
$tm_year = $now_year;
} elsif ( $now_month =~ /^(3|5|8|10)$/ && $now_day == 30) {
# Last day of apr, jun, sep, nov
$tm_year = $now_year;
} elsif ( $now_day == 28 || $now_day == 29 ) {
# Last day of february
$tm_year = $now_year;
}
} else {
# Last year
$tm_year = $now_year - 1;
}
return timelocal( @$tm, $tm_year );
}
=pod
=head2 clf2cal()
my $time = clf2cal( "[18/Mar/2001:15:59:30 +0100]" );
This function will convert a date as found in Common Log Format to
UNIX epoch time.
=cut
sub clf2cal($)
{
my $sub = 'clf2cal';
my ($time) = @_;
# ($dom, $month, $year, hour:min:sec, $tzdiff)
my @date =
$time =~ /^\[(\d+)\/(\w+)\/(\d{4}):(\d{2}:\d{2}:\d{2})\s+([-+][:\d]+)\]$/
or croak "$sub time '$time' should be something like [18/Mar/2001:15:59:30 +0100]\n";
# year month day time tzdiff
return date2cal($date[2], $date[1], $date[0], $date[3], $date[4]);
}
=pod
=head1 DATE FORMATING FUNCTIONS
This module includes some functions to convert date in UNIX epoch time to
some more human readable output. All functions will die() when passed
invalid arguments.
=head2 cal2rfc()
print cal2rfc( $time );
This function will convert a date in UNIX epoch time to the RFC822 format
(used in email, for example). A RFC822 date looks like
Wed, 30 May 2001 12:45:13 +0000
The timezone offset specification will correspond to the local
timezone (usually determined by the TZ environment variable).
=cut
sub cal2rfc($)
{
my $sub = 'cal2rfc';
my $time = $_[0];
my ( $tm_sec, $tm_min, $tm_hour, $tm_mday, $tm_mon, $tm_year, $tm_wday )
= localtime $time ;
my $off = tz_local_offset;
my $tzoff = sprintf("%+05d", int($off / 3600) * 100 + $off % 60);
return $daynames[$tm_wday] .
", $tm_mday " .
$monthnames[$tm_mon] . " " .
($tm_year + 1900) . " " .
($tm_hour < 10 ? '0' : '') . "$tm_hour:" .
($tm_min < 10 ? '0' : '') . "$tm_min:" .
($tm_sec < 10 ? '0' : '') . "$tm_sec " .
$tzoff;
}
=pod
=head2 cal2ymdhms()
print cal2ymdhms( $time );
This function converts a date in UNIX epoch time to a string of the form:
YYYYMMDDHHMMSS
This representation will correspond the time in the local timezone (usually determined by the TZ environment variable.)
=cut
sub cal2ymdhms($) {
my $sub = 'cal2ymdhms';
my ( $tm_sec, $tm_min, $tm_hour, $tm_mday, $tm_mon, $tm_year )
= localtime $_[0];
$tm_mon++;
return '' . ($tm_year + 1900) .
($tm_mon < 10 ? '0' : '') . $tm_mon .
($tm_mday < 10 ? '0' : '') . $tm_mday .
($tm_hour < 10 ? '0' : '') . $tm_hour .
($tm_min < 10 ? '0' : '') . $tm_min .
($tm_sec < 10 ? '0' : '') . $tm_sec;
}
=pod
=head2 getMonthName()
print getMonthName( 0 ); # Gives 'Jan'
This function takes as parameter a number (0-11) representing the
month (as returned by localtime() for example) and will return the
English abbreviated name of that month ( Jan, Feb, etc. ).
=cut
sub getMonthName($) {
my $sub = 'getMonthName';
my ( $month ) = @_;
croak "$sub month should be between 0 and 11"
unless $month >= 0 && $month <= 11;
return $monthnames[$month];
}
# keep perl happy
1;
__END__
=pod
=head1 AUTHOR
Joost van Baal <joostvb@logreport.org>
=head1 VERSION
$Id: Time.pm,v 1.10 2006/07/23 13:16:30 vanbaal Exp $
=head1 COPYRIGHT
Copyright (C) 2000-2002 Stichting LogReport Foundation LogReport@LogReport.org
This file is part of Lire.
Lire is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (see COPYING); if not, check with
http://www.gnu.org/copyleft/gpl.html.
=cut
|