Debian Codegraph @

t/zone.t

raw
1 use strict;
2 use warnings;
3 use Test::More;
4 use Time::Zone;
5
6 # tz_offset: standard timezone abbreviations
7 is(tz_offset("GMT"), 0, "tz_offset GMT = 0");
8 is(tz_offset("UTC"), 0, "tz_offset UTC = 0");
9 is(tz_offset("EST"), -18000, "tz_offset EST = -18000");
10 is(tz_offset("CST"), -21600, "tz_offset CST = -21600");
11 is(tz_offset("MST"), -25200, "tz_offset MST = -25200");
12 is(tz_offset("PST"), -28800, "tz_offset PST = -28800");
13 is(tz_offset("CET"), 3600, "tz_offset CET = 3600");
14 is(tz_offset("JST"), 32400, "tz_offset JST = 32400");
15 is(tz_offset("IST"), 19800, "tz_offset IST = 19800");
16 is(tz_offset("NST"), -12600, "tz_offset NST = -12600 (Newfoundland Standard)");
17 is(tz_offset("NFT"), -12600, "tz_offset NFT = -12600 (Newfoundland)");
18
19 # tz_offset: Newfoundland Daylight (half-hour DST)
20 is(tz_offset("NDT"), -9000, "tz_offset NDT = -9000 (Newfoundland Daylight)");
21
22 # tz_offset: DST timezone abbreviations
23 is(tz_offset("EDT"), -14400, "tz_offset EDT = -14400");
24 is(tz_offset("CDT"), -18000, "tz_offset CDT = -18000");
25 is(tz_offset("MDT"), -21600, "tz_offset MDT = -21600");
26 is(tz_offset("PDT"), -25200, "tz_offset PDT = -25200");
27 is(tz_offset("BST"), 3600, "tz_offset BST = 3600");
28 is(tz_offset("CEST"), 7200, "tz_offset CEST = 7200");
29
30 # tz_offset: numeric offsets
31 is(tz_offset("+0000"), 0, "tz_offset +0000 = 0");
32 is(tz_offset("-0500"), -18000, "tz_offset -0500 = -18000");
33 is(tz_offset("+0530"), 19800, "tz_offset +0530 = 19800");
34 is(tz_offset("+0900"), 32400, "tz_offset +0900 = 32400");
35 is(tz_offset("-0800"), -28800, "tz_offset -0800 = -28800");
36
37 # tz_offset: Southeast Asian timezones (RT#123247)
38 is(tz_offset("ICT"), 25200, "tz_offset ICT = 25200 (Indochina Time, UTC+7)");
39 is(tz_offset("PHT"), 28800, "tz_offset PHT = 28800 (Philippine Time, UTC+8)");
40 is(tz_offset("ict"), 25200, "tz_offset ict case insensitive");
41 is(tz_offset("pht"), 28800, "tz_offset pht case insensitive");
42
43 # tz_offset: unknown zone returns undef
44 is(tz_offset("BOGUS"), undef, "tz_offset unknown zone returns undef");
45
46 # tz_offset: case insensitivity
47 is(tz_offset("gmt"), 0, "tz_offset case insensitive: gmt");
48 is(tz_offset("est"), -18000, "tz_offset case insensitive: est");
49
50 # tz_name: with explicit $dst parameter (deterministic, no system-time dependency)
51 # When $dst=0, offset -18000 (-5h) should resolve to a standard timezone (EST)
52 # When $dst=1, offset -18000 (-5h) should resolve to a DST timezone (CDT)
53 is(tz_name(-18000, 0), "est", "tz_name(-18000, dst=0) is est");
54 is(tz_name(-18000, 1), "cdt", "tz_name(-18000, dst=1) is cdt");
55
56 # tz_name: offset 0 is always GMT/UTC regardless of DST flag
57 like(tz_name(0, 0), qr/^(?:gmt|utc)$/i, "tz_name(0, dst=0) is GMT or UTC");
58 like(tz_name(0, 1), qr/^(?:gmt|utc)$/i, "tz_name(0, dst=1) is GMT or UTC");
59
60 # tz_name: offsets with only standard or only DST entries
61 is(tz_name(32400, 0), "jst", "tz_name(32400, dst=0) is jst (Japan Standard)");
62 is(tz_name(-25200, 1), "pdt", "tz_name(-25200, dst=1) is pdt (Pacific Daylight)");
63 is(tz_name(-28800, 0), "pst", "tz_name(-28800, dst=0) is pst (Pacific Standard)");
64
65 # tz_name: unknown offset returns correct +HHMM numeric string (RT#59298)
66 # 5400s = 90 minutes = UTC+1:30 → "+0130" (not "+9000" which the buggy code produced)
67 is(tz_name(5400, 0), "+0130", "tz_name(5400) returns +0130 (UTC+1:30)");
68 # Negative fractional-hour offset: -5400s = UTC-1:30 → "-0130"
69 # (Note: -9000 is now NDT/Newfoundland Daylight, so use -5400 instead)
70 is(tz_name(-5400, 0), "-0130", "tz_name(-5400) returns -0130 (UTC-1:30)");
71
72 # tz_local_offset: returns a sane value
73 {
74 my $offset = tz_local_offset();
75 ok(defined $offset, "tz_local_offset returns defined value");
76 cmp_ok($offset, '>=', -12 * 3600, "tz_local_offset >= -12 hours");
77 cmp_ok($offset, '<=', 14 * 3600, "tz_local_offset <= 14 hours");
78 }
79
80 done_testing;