Debian Codegraph @

t/rt-timezone.t

raw
1 use strict;
2 use warnings;
3 use Test::More tests => 10;
4 use Date::Parse qw(str2time);
5 use Time::Zone;
6 use POSIX qw();
7
8 # IST (Indian Standard Time) should resolve to UTC+5:30
9 {
10 my $offset = tz_offset("ist");
11 is($offset, 19800, "tz_offset('ist') returns 19800 (UTC+5:30)");
12
13 my $time = str2time("2024-01-15 12:00:00 IST");
14 my $time_utc = str2time("2024-01-15 06:30:00 UTC");
15 is($time, $time_utc, "IST date parses to correct UTC equivalent");
16 }
17
18 # RT#76968: tz2zone("America/Chicago") doesn't work
19 # IANA timezone names like "America/Chicago" should be usable with tz_offset
20 {
21 SKIP: {
22 # Verify that the system supports IANA timezone names
23 my $has_iana = eval {
24 local $ENV{TZ} = "Etc/UTC";
25 POSIX::tzset();
26 my ($std) = POSIX::tzname();
27 defined $std && $std =~ /UTC|GMT/i;
28 };
29 skip "system does not support IANA timezone names", 6 unless $has_iana;
30
31 # tz_offset with IANA name "Etc/UTC" must return 0 (deterministic)
32 my $utc_offset = tz_offset("Etc/UTC");
33 is($utc_offset, 0, "RT#76968: tz_offset('Etc/UTC') returns 0");
34
35 # tz2zone with IANA name "Etc/UTC" must return a valid abbreviation
36 my $utc_name = tz2zone("Etc/UTC", undef, 0);
37 ok(defined $utc_name && length($utc_name) > 0,
38 "RT#76968: tz2zone('Etc/UTC') returns a defined non-empty name");
39
40 # tz_offset(tz2zone(IANA_name)) must return defined — the key failure in the bug
41 my $chicago_abbr = tz2zone("America/New_York", undef, 0);
42 ok(defined $chicago_abbr,
43 "RT#76968: tz2zone('America/New_York', dst=0) returns defined");
44
45 my $chicago_offset = tz_offset($chicago_abbr);
46 ok(defined $chicago_offset,
47 "RT#76968: tz_offset(tz2zone('America/New_York')) returns defined");
48
49 # tz_offset of IANA name directly must return defined
50 my $ny_offset = tz_offset("America/New_York");
51 ok(defined $ny_offset,
52 "RT#76968: tz_offset('America/New_York') returns defined");
53
54 # tz_offset("Etc/UTC") via tz2zone round-trip
55 my $utc_abbr = tz2zone("Etc/UTC", undef, 0);
56 my $utc_offset2 = tz_offset($utc_abbr);
57 is($utc_offset2, 0,
58 "RT#76968: tz_offset(tz2zone('Etc/UTC')) returns 0");
59 }
60 }
61
62 # RT#98949: Moscow Time Change in October 2014
63 # MSK (Moscow Standard Time) is UTC+3 permanently since 25 Oct 2014.
64 # Russia eliminated DST in 2011 (UTC+4 year-round), then reverted to UTC+3 in Oct 2014.
65 {
66 my $offset = tz_offset("msk");
67 is($offset, 10800, "RT#98949: tz_offset('msk') returns 10800 (UTC+3)");
68
69 my $time = str2time("2014-10-27 00:00:00 MSK");
70 my $time_utc = str2time("2014-10-26 21:00:00 UTC");
71 is($time, $time_utc, "RT#98949: MSK date after 2014 change parses to correct UTC");
72 }