Skip to content
Browse files

Merge pull request #318 from dex4er/fix_tz_offset

Correct timezone on systems where strftime doesn't support %z
  • Loading branch information...
2 parents 53f0f90 + ab6f9f0 commit c70f72d2d4f8ab5c1b348c87f56dcd8eb6fd12a6 @miyagawa miyagawa committed Aug 15, 2012
Showing with 11 additions and 1 deletion.
  1. +11 −1 lib/Plack/Middleware/AccessLog.pm
View
12 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 c70f72d

Please sign in to comment.
Something went wrong with that request. Please try again.