On 2007-05-06 13:22:26 -0400, Matt Sergeant wrote: > On 6-May-07, at 10:34 AM, Peter J. Holzer wrote: > > >It's a bit tricky to compute the timezone correctly, though.
Actually, its easier than I remembered:
my @localtime = localtime($t);
my @gmtime = gmtime($t);
$gmtime[8] = $localtime[8]; # copy DST flag
my $t2 = mktime(@gmtime);
my $tzs = $t - $t2;
my $tz2 = $tzs >= 0 ? "+" : "-";
$tzs = abs($tzs);
$tz2 .= sprintf("%02d%02d", $tzs / 3600, ($tzs / 60) % 60);
Quite straightforward (the last 4 lines could probably be simplified). I
thought there was a nasty edgecase during the DST switch times where
that wouldn't work but my tests didn't reveal that.
> Why not just use gmtime and output +0000 ?
I think that's what qmail does, so there's a precedent.
> It makes everyone's life easier IMHO.
Sometimes when reading Received headers, the timezone is useful.
Sometimes it's just annoying. Most of the time I don't care.
How about doing something like this
sub rfc822_date {
my ($t) = @_;
if (strftime("%a %b %z", gmtime(0)) ~= m/Thu Jan [-+]\d{4}$/) {
# strftime works as expected, so use that
return strftime("...", localtime($t));
} else {
# something's wrong - play it safe and simple:
my @weekdays = qw(Sun Mon Tue Wed Thu Fri Sat);
my @months = qw(Jan Feb Mar ... Dec);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime(time);
my $date = sprintf("%s, %02d %s %d %02d:%02d:%02d +0000",
$weekdays[$wday],
$mday,
$monts[$mon],
$year + 1900,
$hour, $min, $sec,
):
return $date;
}
}
The test result could probably be cached.
hp
--
_ | Peter J. Holzer | I know I'd be respectful of a pirate
|_|_) | Sysadmin WSR | with an emu on his shoulder.
| | | [EMAIL PROTECTED] |
__/ | http://www.hjp.at/ | -- Sam in "Freefall"
signature.asc
Description: Digital signature
