Article View: news.software.nntp
Article #72964Re: Date bug at year end
From: Russ Allbery
Date: Mon, 02 Jan 2006 21:47
Date: Mon, 02 Jan 2006 21:47
72 lines
4254 bytes
4254 bytes
Jeffrey M Vinocur <jeff@litech.org> writes:
> Russ Allbery <rra@stanford.edu> wrote:
>> syslog has an utterly obnoxious date format, but unfortunately so many
>> things parse it that you really can't ever change it. It doesn't sort,
>> it uses English month names, it doesn't have the year... bleh.
> Which makes the code in innreport so scary I almost ran away
> screaming. I -think- this:
> # Adjust the year because syslog doesn't record it. We assume that
> # it's the current year unless the last date is in the future.
> my $ld = &ConvDate($last_date);
> $dy-- if $ld > $ts + 60 * ($tm + 60 * ($th + 24 * ($dd - 1 +
> substr("000031059090120151181212243273304334", $dm * 3, 3)))) ||
> $ld < &ConvDate($first_date);
> might actually be code to detect this scenario (although I can't tell
> for sure because it makes my head hurt), but it seems to only affect
> parts of the HTML-ized results and not the news.daily email. On the
> other hand, if it actually does something useful, I think it might be a
> big step towards a fix.
Yes, that substr thing is evil. It's a string consisting of the
cumulative days of the year at the start of each month, each as three
digits, concatenated together.
There are better ways to do this, such as the below that makes mktime do
the heavy lifting and gets year transitions right, but I never wanted to
tackle the restructuring required to use them. $fileyear and $filemonth
in this case should be taken from the mtime of the file.
use POSIX qw(mktime);
{
my $i = 1;
%MONTHS = map { $_ => $i++ }
qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
}
# Given a single line of a log file, the year of the log file, and the month
# of the log file, strip off the leading timestamp and convert it into seconds
# since epoch and then return a list consisting of that timestamp and the rest
# of the line.
#
# A syslog date is in the form:
#
# mmm DD HH:MM:SS
#
# with no year. We therefore have to be able to guess at the year by taking
# into account year and month of the file and assuming that if the month of
# the date is greater than the month of the file, the date is actually from
# the previous year.
#
# Warns and returns undef on a failure to parse the date.
sub parse_time {
my ($self, $line, $fileyear, $filemonth) = @_;
$line =~ s/^(\w\w\w) ([ \d]\d) (\d\d):(\d\d):(\d\d) //
or return;
my ($month, $day, $hour, $min, $sec) = ($1, $2, $3, $4, $5);
$month = $MONTHS{$month} or return;
my $year = ($month <= $filemonth ) ? $fileyear : $fileyear - 1;
$year -= 1900;
$month -= 1;
my $time = mktime ($sec, $min, $hour, $day, $month, $year);
return ($time, $line);
}
--
Russ Allbery (rra@stanford.edu) <http://www.eyrie.org/~eagle/>
Please post questions rather than mailing me directly.
<http://www.eyrie.org/~eagle/faqs/questions.html> explains why.
Technical Details
Message-ID:
<874q4m8fpu.fsf@windlord.stanford.edu>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.20141118.mbox.gz!number1.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!news2.google.com!news4.google.com!newsfeed.stanford.edu!shelby.stanford.edu!not-for-mailReferences:
<BdLtf.212470$qk4.79698@bgtnsc05-news.ops.worldnet.att.net> <87u0cn270k.fsf@windlord.stanford.edu> <QGcuf.217035$qk4.37653@bgtnsc05-news.ops.worldnet.att.net> <87sls65x1n.fsf@windlord.stanford.edu> <dpc38g$tt4$1@puck.litech.org>Show Full Headers
Complete Article Headers
Message-ID: <874q4m8fpu.fsf@windlord.stanford.edu> Subject: Re: Date bug at year end Date: Mon, 02 Jan 2006 13:47:25 -0800 From: Russ Allbery <rra@stanford.edu> References: <BdLtf.212470$qk4.79698@bgtnsc05-news.ops.worldnet.att.net> <87u0cn270k.fsf@windlord.stanford.edu> <QGcuf.217035$qk4.37653@bgtnsc05-news.ops.worldnet.att.net> <87sls65x1n.fsf@windlord.stanford.edu> <dpc38g$tt4$1@puck.litech.org> Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.20141118.mbox.gz!number1.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!news2.google.com!news4.google.com!newsfeed.stanford.edu!shelby.stanford.edu!not-for-mail Newsgroups: news.software.nntp Organization: The Eyrie Lines: 72 NNTP-Posting-Host: windlord.stanford.edu Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.Stanford.EDU 1136238445 24789 171.64.19.147 (2 Jan 2006 21:47:25 GMT) X-Complaints-To: news@news.stanford.edu User-Agent: Gnus/5.110004 (No Gnus v0.4) XEmacs/21.4.17 (linux) Cancel-Lock: sha1:s1BFi+kUfZMzCtLKkDE3mtaGUhI=