Debian Codegraph @

metomi/isodatetime/tests/test_timezone.py

raw symbols SCIP
1 # Copyright (C) British Crown (Met Office) & Contributors.
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU Lesser General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 """This tests the timezone module."""
17
18 from typing import Callable, Tuple
19 import pytest
20
21 from metomi.isodatetime import timezone
22
23
24 @pytest.mark.parametrize('dst', [True, False])
25 @pytest.mark.parametrize(
26 'tz_seconds, expected_no_dst, tz_dst_seconds, expected_with_dst',
27 [
28 pytest.param(
29 45900, (12, 45),
30 49500, (13, 45),
31 id="pacific/chatham, +12:45 and +13:45"
32 ),
33 pytest.param(
34 20700, (5, 45),
35 20700, (5, 45),
36 id="asia/kathmandu, +05:45"
37 ),
38 pytest.param(
39 3600, (1, 0),
40 7200, (2, 0),
41 id="europe/oslo, +01:00 and +02:00"
42 ),
43 pytest.param(
44 0, (0, 0),
45 0, (0, 0),
46 id="UTC"
47 ),
48 pytest.param(
49 0, (0, 0),
50 3600, (1, 0),
51 id="europe/london, +00:00 and +01:00"
52 ),
53 pytest.param(
54 -12600, (-3, -30),
55 -9000, (-2, -30),
56 id="america/st_johns, -03:30 and -02:30"
57 ),
58 pytest.param(
59 -5*3600, (-5, 0),
60 -4*3600, (-4, 0),
61 id="america/new_york, -05:00 and -04:00,"
62 ),
63 pytest.param(
64 -8100, (-2, -15),
65 -8100, (-2, -15),
66 id="hypothetical -02:15"
67 ),
68 pytest.param(
69 -1800, (0, -30),
70 -2400, (0, -40),
71 id="hypothetical -0:30 and -0:40"
72 ),
73 ]
74 )
75 def test_get_local_time_zone(
76 dst: bool,
77 tz_seconds: int,
78 expected_no_dst: Tuple[int, int],
79 tz_dst_seconds: int,
80 expected_with_dst: Tuple[int, int],
81 mock_local_time_zone: Callable
82 ):
83 """Test that the hour/minute returned is correct.
84
85 Params:
86 dst: Whether to simulate daylight savings time.
87 tz_seconds: Time zone UTC offset when not in DST.
88 expected_no_dst: Expected return value when in DST.
89 tz_dst_seconds: Time zone UTC offset when in DST.
90 expected_with_dst: Expected return value when not in DST.
91 """
92 mock_local_time_zone(
93 tz_seconds,
94 tz_dst_seconds if dst else 0
95 )
96 expected = expected_with_dst if dst else expected_no_dst
97 assert timezone.get_local_time_zone() == expected
98
99
100 @pytest.mark.parametrize(
101 'tz_seconds, expected_formats',
102 [
103 pytest.param(
104 0, ('Z', 'Z', 'Z'), # flags are never used for UTC
105 id="UTC"
106 ),
107 # Positive values, some with minutes != 0
108 pytest.param(
109 28800, ('+0800', '+08:00', '+08'),
110 id="asia/macao"
111 ),
112 pytest.param(
113 45900, ('+1245', '+12:45', '+1245'),
114 id="pacific/chatham"
115 ),
116 # Negative values, some with minutes != 0
117 pytest.param(
118 -3*3600, ('-0300', '-03:00', '-03'),
119 id="america/buenos_aires"
120 ),
121 pytest.param(
122 -12600, ('-0330', '-03:30', '-0330'),
123 id="america/st_johns"
124 ),
125 pytest.param(
126 -8100, ('-0215', '-02:15', '-0215'),
127 id="hypothetical -02:15"
128 ),
129 pytest.param(
130 -1800, ('-0030', '-00:30', '-0030'),
131 id="hypothetical -00:30"
132 ),
133 ]
134 )
135 def test_get_local_time_zone_format(
136 tz_seconds: int,
137 expected_formats: Tuple[int, int, int],
138 mock_local_time_zone: Callable
139 ):
140 """Test that the UTC offset string format is correct.
141
142 Params:
143 tz_seconds: The time zone's UTC offset.
144 expected_formats: The expected return values for normal, extended
145 and reduced formats, respectively.
146 """
147 # DST is not really important for this test
148 mock_local_time_zone(tz_seconds)
149 for i, tz_format_mode in enumerate([
150 timezone.TimeZoneFormatMode.normal,
151 timezone.TimeZoneFormatMode.extended,
152 timezone.TimeZoneFormatMode.reduced
153 ]):
154 expected = expected_formats[i]
155 assert timezone.get_local_time_zone_format(tz_format_mode) == expected