| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
# GH#10 / RT#80649: Wrong timezone abbreviation during DST fall-back |
| 5 |
# |
| 6 |
# format_Z was calling timelocal() to reconstruct the original epoch from |
| 7 |
# broken-down time. During the "fall-back" hour, the broken-down time is |
| 8 |
# ambiguous (1:xx AM occurs twice: once in PDT and once in PST). timelocal() |
| 9 |
# resolves the ambiguity to the first occurrence (PDT), returning the PDT |
| 10 |
# epoch (offset -25200). tz_name(-25200, dst=0) then finds -25200 in %zoneOff |
| 11 |
# as "mst" (Mountain Standard), yielding MST instead of PST. |
| 12 |
# |
| 13 |
# Fix: use the original epoch stored in $_[0]->[9] instead of timelocal(). |
| 14 |
# |
| 15 |
# The TZ is set in a BEGIN block so the C library timezone is initialised |
| 16 |
# before any module loads and before any localtime/tzset calls happen. |
| 17 |
# Using a runtime $ENV{TZ} assignment after `use` statements is unreliable |
| 18 |
# because `use` executes at compile time, and on some platforms restoring a |
| 19 |
# `local $ENV{TZ}` automatically resets the C library timezone, which a later |
| 20 |
# POSIX::tzset() call may not fully undo. |
| 21 |
|
| 22 |
BEGIN { |
| 23 |
$ENV{TZ} = 'America/Los_Angeles'; |
| 24 |
require POSIX; |
| 25 |
eval { POSIX::tzset() }; |
| 26 |
} |
| 27 |
|
| 28 |
use POSIX qw(); |
| 29 |
use Test::More; |
| 30 |
use Date::Format qw(time2str); |
| 31 |
|
| 32 |
# Skip the whole file if the platform does not support IANA timezone names. |
| 33 |
# Use epoch 1352019262 (2012-11-04 01:04:22 PDT): DST=1 in America/Los_Angeles |
| 34 |
# but DST=0 in UTC, so this check actually distinguishes the two and won't |
| 35 |
# produce a false-positive on a UTC-only system. |
| 36 |
my $has_la_tz = eval { |
| 37 |
my @lt = localtime(1352019262); # 2012-11-04 01:04:22 PDT (before fall-back) |
| 38 |
$lt[8] == 1; # Expect DST flag = 1 in PDT |
| 39 |
}; |
| 40 |
plan( skip_all => "system does not support America/Los_Angeles timezone" ) |
| 41 |
unless $has_la_tz; |
| 42 |
|
| 43 |
plan tests => 3; |
| 44 |
|
| 45 |
# 2012-11-04 01:25:05 PST — the second occurrence of 01:xx AM after fall-back |
| 46 |
# This is the timestamp from the original bug report that returned MST. |
| 47 |
is( time2str("%Z", 1352021105), "PST", |
| 48 |
"GH#10/RT#80649: repeated hour after fall-back formats as PST, not MST" ); |
| 49 |
|
| 50 |
# 2012-11-04 01:54:22 PDT — before the fall-back (first occurrence of 01:xx AM) |
| 51 |
is( time2str("%Z", 1352019262), "PDT", |
| 52 |
"GH#10/RT#80649: pre-fall-back timestamp formats as PDT" ); |
| 53 |
|
| 54 |
# 2012-11-04 02:25:05 PST — after the repeated hour, clearly PST |
| 55 |
is( time2str("%Z", 1352024705), "PST", |
| 56 |
"GH#10/RT#80649: post-repeated-hour timestamp formats as PST" ); |