develooper Front page | perl.perl5.porters | Postings from December 2004

[perl #7835] strftime time zone bug

From:
Steve Peters via RT
Date:
December 31, 2004 12:32
Subject:
[perl #7835] strftime time zone bug
Message ID:
rt-3.0.11-7835-104954.2.35140900486726@perl.org
> [spencer@zanshin.com - Mon Oct 22 12:28:09 2001]:
> 
> The workaround for NETaa14816 contained in ext/POSIX/POSIX.xs introduces
> a different bug, namely that the time zone name and offset reported
> by strftime %Z and %z are incorrect -- they report the time zone name and
> offset of the current time, even if the time you pass has a different DST
> setting.
> 
> I have a patch which fixes the problem, at least for our configuration.
> I have no idea how portable it is and I have no hope of figuring out all
> the ifdefs necessary, but I'll just show you my patch and hope you can
> make some use of it (so long as I get credited if you do :-) ).  If not,
> oh well, and if you've already dealt with it, so much the better.
> 
> First, a sample script which demonstrates the problem:
> 
> #!/usr/bin/perl
> 
> use strict;
> use POSIX qw(strftime);
> 
> my $t = 1003803105; # approx. 7:12 PM on Oct. 22, 2001, PDT
> 
> print_stuff($t);
> print "\n";
> 
> # now add 10 days, which takes us into PST
> 
> print_stuff($t+864000);
> 
> sub print_stuff
> {
>   my $t = shift;
>   my ($sec, $min, $hour, $mday, $mon, $year,
>       $wday, $yday, $isdst) = localtime($t);
>   printf("%02d/%02d/%04d %02d:%02d:%02d DST: %d\n",
>          $mon+1, $mday, $year+1900, $hour, $min, $sec, $isdst);
>   print strftime("%A, %d-%b-%g %I:%M %p %Z %z\n",
>                  $sec, $min, $hour, $mday, $mon, $year, $wday, $yday,
> $isdst);
> }
> 
> Using 5.6.0 on RedHat 6.2, version 2.2.16 of the kernel (also
> reproduced on perl 5.6.0/Linux 2.2.14 and perl 5.6.1/linux 2.2.19), the
> above script prints out:
> 
> 10/22/2001 19:11:45 DST: 1
> Monday, 22-Oct-01 07:11 PM PDT -0700
> 
> 11/01/2001 18:11:45 DST: 0
> Thursday, 01-Nov-01 06:11 PM PDT -0700
> 
> As you can see, for the second date, even though $isdst == 0, %Z and %z
> still say PDT and -0700, when they should say PST and -0800.  The problem
> is that init_tm uses localtime(&now) to initialize the tm structure, which
> is incorrect when the actual date we are considering is on the other side
> of the DST boundary (because 10/22/2001 is in DST, but 11/01/2001 is not).
> 
> I've attached my patch (diff against 5.6.0) so that Outlook won't
mangle the
> whitespace.
> 
> Obviously the patch depends on the correct value of isdst being passed in
> to whichever function is calling init_tm, and also on tzname and timezone
> being valid.  But like I said, it does work for us.  Post-patch:
> 
> 10/22/2001 19:11:45 DST: 1
> Monday, 22-Oct-01 07:11 PM PDT -0700
> 
> 11/01/2001 18:11:45 DST: 0
> Thursday, 01-Nov-01 06:11 PM PST -0800
> 

This appears to have been implemented with change 18267.



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About