| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More tests => 15; |
| 4 |
use Date::Parse qw(str2time); |
| 5 |
use Time::Zone; |
| 6 |
|
| 7 |
# IST (Indian Standard Time) should resolve to UTC+5:30 |
| 8 |
{ |
| 9 |
my $offset = tz_offset("ist"); |
| 10 |
is($offset, 19800, "tz_offset('ist') returns 19800 (UTC+5:30)"); |
| 11 |
|
| 12 |
my $time = str2time("2024-01-15 12:00:00 IST"); |
| 13 |
my $time_utc = str2time("2024-01-15 06:30:00 UTC"); |
| 14 |
is($time, $time_utc, "IST date parses to correct UTC equivalent"); |
| 15 |
} |
| 16 |
|
| 17 |
# RT#76968: tz2zone("America/Chicago") doesn't work |
| 18 |
# IANA timezone names like "America/Chicago" should be usable with tz_offset |
| 19 |
{ |
| 20 |
use POSIX qw(); |
| 21 |
|
| 22 |
SKIP: { |
| 23 |
# Verify that the system supports IANA timezone names |
| 24 |
my $has_iana = eval { |
| 25 |
local $ENV{TZ} = "Etc/UTC"; |
| 26 |
POSIX::tzset(); |
| 27 |
my ($std) = POSIX::tzname(); |
| 28 |
defined $std && $std =~ /UTC|GMT/i; |
| 29 |
}; |
| 30 |
skip "system does not support IANA timezone names", 6 unless $has_iana; |
| 31 |
|
| 32 |
# tz_offset with IANA name "Etc/UTC" must return 0 (deterministic) |
| 33 |
my $utc_offset = tz_offset("Etc/UTC"); |
| 34 |
is($utc_offset, 0, "RT#76968: tz_offset('Etc/UTC') returns 0"); |
| 35 |
|
| 36 |
# tz2zone with IANA name "Etc/UTC" must return a valid abbreviation |
| 37 |
my $utc_name = tz2zone("Etc/UTC", undef, 0); |
| 38 |
ok(defined $utc_name && length($utc_name) > 0, |
| 39 |
"RT#76968: tz2zone('Etc/UTC') returns a defined non-empty name"); |
| 40 |
|
| 41 |
# tz_offset(tz2zone(IANA_name)) must return defined — the key failure in the bug |
| 42 |
my $chicago_abbr = tz2zone("America/New_York", undef, 0); |
| 43 |
ok(defined $chicago_abbr, |
| 44 |
"RT#76968: tz2zone('America/New_York', dst=0) returns defined"); |
| 45 |
|
| 46 |
my $chicago_offset = tz_offset($chicago_abbr); |
| 47 |
ok(defined $chicago_offset, |
| 48 |
"RT#76968: tz_offset(tz2zone('America/New_York')) returns defined"); |
| 49 |
|
| 50 |
# tz_offset of IANA name directly must return defined |
| 51 |
my $ny_offset = tz_offset("America/New_York"); |
| 52 |
ok(defined $ny_offset, |
| 53 |
"RT#76968: tz_offset('America/New_York') returns defined"); |
| 54 |
|
| 55 |
# tz_offset("Etc/UTC") via tz2zone round-trip |
| 56 |
my $utc_abbr = tz2zone("Etc/UTC", undef, 0); |
| 57 |
my $utc_offset2 = tz_offset($utc_abbr); |
| 58 |
is($utc_offset2, 0, |
| 59 |
"RT#76968: tz_offset(tz2zone('Etc/UTC')) returns 0"); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
# RT#59298: tz_name() reports incorrect offset for unknown timezone names |
| 64 |
# The offset passed is in seconds, but the old code treated it as minutes, |
| 65 |
# producing e.g. "+33000" for IST (+5:30) instead of "+0530". |
| 66 |
# Also: negative fractional-hour offsets must format correctly. |
| 67 |
{ |
| 68 |
# UTC+5:30 = 19800s — not in the Zone table when the bug was filed |
| 69 |
is(tz_name(19800, 0), "ist", "RT#59298: tz_name(19800) returns ist (UTC+5:30 is now in table)"); |
| 70 |
# An offset not in the table: UTC+1:30 = 5400s → "+0130" (was "+9000" before fix) |
| 71 |
is(tz_name(5400, 0), "+0130", "RT#59298: tz_name(5400) returns +0130, not +9000"); |
| 72 |
# Negative fractional offset: UTC-1:30 = -5400s → "-0130" |
| 73 |
# (Note: -9000 is now NDT/Newfoundland Daylight, so use -5400 instead) |
| 74 |
is(tz_name(-5400, 0), "-0130", "RT#59298: tz_name(-5400) returns -0130 (UTC-1:30)"); |
| 75 |
} |
| 76 |
|
| 77 |
# RT#82271: tz_name should return CEST (not MEST) for Central European Summer Time |
| 78 |
# CEST (Central European Summer Time) is the standard/preferred abbreviation; |
| 79 |
# MEST (Middle European Summer Time) is a German-influenced variant. |
| 80 |
{ |
| 81 |
is(tz_name(7200, 1), "cest", "RT#82271: tz_name(+2h, dst=1) returns cest not mest"); |
| 82 |
is(tz_offset("MEST"), 7200, "RT#82271: tz_offset(MEST) still works for backward compat"); |
| 83 |
} |
| 84 |
|
| 85 |
# RT#98949: Moscow Time Change in October 2014 |
| 86 |
# MSK (Moscow Standard Time) is UTC+3 permanently since 25 Oct 2014. |
| 87 |
# Russia eliminated DST in 2011 (UTC+4 year-round), then reverted to UTC+3 in Oct 2014. |
| 88 |
{ |
| 89 |
my $offset = tz_offset("msk"); |
| 90 |
is($offset, 10800, "RT#98949: tz_offset('msk') returns 10800 (UTC+3)"); |
| 91 |
|
| 92 |
my $time = str2time("2014-10-27 00:00:00 MSK"); |
| 93 |
my $time_utc = str2time("2014-10-26 21:00:00 UTC"); |
| 94 |
is($time, $time_utc, "RT#98949: MSK date after 2014 change parses to correct UTC"); |
| 95 |
} |