Permalink
Browse files

Correct timezone on systems where strftime doesn't support %z

  • Loading branch information...
1 parent 53f0f90 commit ab6f9f0981a9c0de475fd2c672b2e348041b4df7 @dex4er dex4er committed Aug 15, 2012
Showing with 11 additions and 1 deletion.
  1. +11 −1 lib/Plack/Middleware/AccessLog.pm
@@ -13,6 +13,13 @@ my %formats = (
);
use POSIX ();
+use Time::Local ();
+
+my $tzoffset = POSIX::strftime("%z", localtime) !~ /^[+-]\d{4}$/ && do {
+ my @t = localtime(time);
+ my $s = Time::Local::timegm(@t) - Time::Local::timelocal(@t);
+ sprintf '%+03d%02u', int($s/60/60), $s % (60*60)
+};
sub call {
my $self = shift;
@@ -35,9 +42,11 @@ sub log_line {
my $h = Plack::Util::headers($headers);
my $strftime = sub {
+ my ($fmt, @time) = @_;
+ $fmt =~ s/%z/$tzoffset/g if $tzoffset;
my $old_locale = POSIX::setlocale(&POSIX::LC_ALL);
POSIX::setlocale(&POSIX::LC_ALL, 'C');
- my $out = POSIX::strftime(@_);
+ my $out = POSIX::strftime($fmt, @time);
POSIX::setlocale(&POSIX::LC_ALL, $old_locale);
return $out;
};
@@ -106,6 +115,7 @@ sub _safe {
$string;
}
+1;
__END__

0 comments on commit ab6f9f0

Please sign in to comment.