Skip to content
This repository
Browse code

Merge pull request #318 from dex4er/fix_tz_offset

Correct timezone on systems where strftime doesn't support %z
  • Loading branch information...
commit c70f72d2d4f8ab5c1b348c87f56dcd8eb6fd12a6 2 parents 53f0f90 + ab6f9f0
Tatsuhiko Miyagawa miyagawa authored

Showing 1 changed file with 11 additions and 1 deletion. Show diff stats Hide diff stats

  1. +11 1 lib/Plack/Middleware/AccessLog.pm
12 lib/Plack/Middleware/AccessLog.pm
@@ -13,6 +13,13 @@ my %formats = (
13 13 );
14 14
15 15 use POSIX ();
  16 +use Time::Local ();
  17 +
  18 +my $tzoffset = POSIX::strftime("%z", localtime) !~ /^[+-]\d{4}$/ && do {
  19 + my @t = localtime(time);
  20 + my $s = Time::Local::timegm(@t) - Time::Local::timelocal(@t);
  21 + sprintf '%+03d%02u', int($s/60/60), $s % (60*60)
  22 +};
16 23
17 24 sub call {
18 25 my $self = shift;
@@ -35,9 +42,11 @@ sub log_line {
35 42 my $h = Plack::Util::headers($headers);
36 43
37 44 my $strftime = sub {
  45 + my ($fmt, @time) = @_;
  46 + $fmt =~ s/%z/$tzoffset/g if $tzoffset;
38 47 my $old_locale = POSIX::setlocale(&POSIX::LC_ALL);
39 48 POSIX::setlocale(&POSIX::LC_ALL, 'C');
40   - my $out = POSIX::strftime(@_);
  49 + my $out = POSIX::strftime($fmt, @time);
41 50 POSIX::setlocale(&POSIX::LC_ALL, $old_locale);
42 51 return $out;
43 52 };
@@ -106,6 +115,7 @@ sub _safe {
106 115 $string;
107 116 }
108 117
  118 +1;
109 119
110 120 __END__
111 121

0 comments on commit c70f72d

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