Debian Codegraph @

tests/test_timezone_parser.py

raw
1 import datetime as dt
2 from datetime import datetime, timedelta
3 from unittest import SkipTest
4 from unittest.mock import Mock, patch
5
6 from parameterized import param, parameterized
7 from pytz import timezone
8
9 import dateparser.timezone_parser
10 from dateparser import parse
11 from dateparser.timezone_parser import (
12 StaticTzInfo,
13 get_local_tz_offset,
14 pop_tz_offset_from_string,
15 )
16 from tests import BaseTestCase
17
18
19 class TestTZPopping(BaseTestCase):
20 def setUp(self):
21 super().setUp()
22 self.initial_string = self.datetime_string = self.timezone_offset = (
23 NotImplemented
24 )
25
26 @parameterized.expand(
27 [
28 param("Sep 03 2014 | 4:32 pm EDT", -4),
29 param("17th October, 2034 @ 01:08 am PDT", -7),
30 param("17th October, 2034 @ 01:08 am (PDT)", -7),
31 param("October 17, 2014 at 7:30 am PST", -8),
32 param("20 Oct 2014 13:08 CET", +1),
33 param("20 Oct 2014 13:08cet", +1),
34 param("Nov 25 2014 | 10:17 pm EST", -5),
35 param("Nov 25 2014 | 10:17 pm +0600", +6),
36 param("Nov 25 2014 | 10:17 pm -0930", -9.5),
37 param("20 Oct 2014 | 05:17 am -1200", -12),
38 param("20 Oct 2014 | 05:17 am +0000", 0),
39 param("20 Oct 2014 | 05:17 am -0000", 0),
40 param("15 May 2004", None),
41 param("Wed Aug 05 12:00:00 EDTERR 2015", None),
42 param("Wed Aug 05 12:00:00 EDT 2015", -4),
43 param("April 10, 2016 at 12:00:00 UTC", 0),
44 param("April 10, 2016 at 12:00:00 MEZ", 1),
45 param("April 10, 2016 at 12:00:00 MESZ", 2),
46 param("April 10, 2016 at 12:00:00 GMT+2", 2),
47 param("April 10, 2016 at 12:00:00 UTC+2:00", 2),
48 param("April 10, 2016 at 12:00:00 GMT+02:00", 2),
49 param("April 10, 2016 at 12:00:00 UTC+5:30", 5.5),
50 param("April 10, 2016 at 12:00:00 GMT+05:30", 5.5),
51 param("April 10, 2016 at 12:00:00 UTC-2", -2),
52 param("April 10, 2016 at 12:00:00 GMT-2:00", -2),
53 param("April 10, 2016 at 12:00:00 UTC-02:00", -2),
54 param("April 10, 2016 at 12:00:00 GMT-9:30", -9.5),
55 param("April 10, 2016 at 12:00:00 UTC-09:30", -9.5),
56 param("Thu, 24 Nov 2016 16:03:00 UT", 0),
57 param("Fri Sep 23 2016 10:34:51 GMT+0800 (CST)", 8),
58 param("Fri Sep 23 2016 10:34:51 GMT+12", 12),
59 param("Fri Sep 23 2016 10:34:51 UTC+13", 13),
60 param("Fri Sep 23 2016 10:34:51 GMT+1245 (CST)", 12.75),
61 param("Fri Sep 23 2016 10:34:51 UTC+1245", 12.75),
62 param("2019-07-17T12:30:00.000-03:30", -3.5),
63 param("2019-07-17T12:30:00.000-02:30", -2.5),
64 param("16. srpna 2021 9:59:44 SELČ", 2),
65 param("16. srpna 2021 9:59:44 SEČ", 1),
66 param("16. srpna 2021 9:59:44 ZEČ", 0),
67 param("16. srpna 2021 9:59:44 VEČ", 2),
68 ]
69 )
70 def test_extracting_valid_offset(self, initial_string, expected_offset):
71 self.given_string(initial_string)
72 self.when_offset_popped_from_string()
73 self.then_offset_is(expected_offset)
74
75 @parameterized.expand(
76 [
77 param("Sep 03 2014 | 4:32 pm EDT", "Sep 03 2014 | 4:32 pm "),
78 param(
79 "17th October, 2034 @ 01:08 am PDT", "17th October, 2034 @ 01:08 am "
80 ),
81 param("October 17, 2014 at 7:30 am PST", "October 17, 2014 at 7:30 am "),
82 param("20 Oct 2014 13:08 CET", "20 Oct 2014 13:08 "),
83 param("20 Oct 2014 13:08cet", "20 Oct 2014 13:08"),
84 param("Nov 25 2014 | 10:17 pm EST", "Nov 25 2014 | 10:17 pm "),
85 param(
86 "17th October, 2034 @ 01:08 am +0700", "17th October, 2034 @ 01:08 am "
87 ),
88 param("Sep 03 2014 4:32 pm +0630", "Sep 03 2014 4:32 pm "),
89 ]
90 )
91 def test_timezone_deleted_from_string(self, initial_string, result_string):
92 self.given_string(initial_string)
93 self.when_offset_popped_from_string()
94 self.then_string_modified_to(result_string)
95
96 def test_string_not_changed_if_no_timezone(self):
97 self.given_string("15 May 2004")
98 self.when_offset_popped_from_string()
99 self.then_string_modified_to("15 May 2004")
100
101 def given_string(self, string_):
102 self.initial_string = string_
103
104 def when_offset_popped_from_string(self):
105 self.datetime_string, timezone_offset = pop_tz_offset_from_string(
106 self.initial_string
107 )
108 if timezone_offset:
109 self.timezone_offset = timezone_offset.utcoffset("")
110 else:
111 self.timezone_offset = timezone_offset
112
113 def then_string_modified_to(self, expected_string):
114 self.assertEqual(expected_string, self.datetime_string)
115
116 def then_offset_is(self, expected_offset):
117 delta = (
118 timedelta(hours=expected_offset) if expected_offset is not None else None
119 )
120 self.assertEqual(delta, self.timezone_offset)
121
122
123 class TestLocalTZOffset(BaseTestCase):
124 def setUp(self):
125 super().setUp()
126 self.timezone_offset = NotImplemented
127
128 @parameterized.expand(
129 [
130 param(utc="2014-08-20 4:32", local="2014-08-20 8:32", offset=+4),
131 param(utc="2052-01-02 11:07", local="2052-01-02 10:07", offset=-1),
132 param(utc="2013-12-31 23:59", local="2014-01-01 00:29", offset=+0.5),
133 param(utc="2011-07-14 11:59", local="2011-07-13 23:59", offset=-12),
134 param(utc="2014-10-18 17:41", local="2014-10-18 17:41", offset=0),
135 ]
136 )
137 def test_timezone_offset_calculation(self, utc, local, offset):
138 try:
139 self.given_time(utc, local)
140 except OverflowError:
141 raise SkipTest("Unsupported with 32-bit time_t")
142 self.when_offset_popped_from_string()
143 self.then_offset_is(offset)
144
145 def when_offset_popped_from_string(self):
146 self.timezone_offset = get_local_tz_offset()
147
148 def then_offset_is(self, expected_offset):
149 delta = (
150 timedelta(seconds=3600 * expected_offset)
151 if expected_offset is not None
152 else None
153 )
154 self.assertEqual(delta, self.timezone_offset)
155
156 def given_time(self, utc_dt_string, local_dt_string):
157 datetime_cls = dateparser.timezone_parser.datetime
158 if not isinstance(datetime_cls, Mock):
159 datetime_cls = Mock(wraps=datetime)
160 utc_dt_obj = datetime.strptime(utc_dt_string, "%Y-%m-%d %H:%M").replace(
161 tzinfo=dt.timezone.utc
162 )
163 local_dt_obj = datetime.strptime(local_dt_string, "%Y-%m-%d %H:%M")
164
165 def _dt_now(tz=None):
166 if tz == dt.timezone.utc:
167 return utc_dt_obj
168 return local_dt_obj
169
170 datetime_cls.now = Mock(side_effect=_dt_now)
171 self.add_patch(patch("dateparser.timezone_parser.datetime", new=datetime_cls))
172
173
174 class TestTimeZoneConversion(BaseTestCase):
175 def setUp(self):
176 super().setUp()
177 self.settings = {}
178 self.parser = parse
179 self.result = NotImplemented
180
181 @parameterized.expand(
182 [
183 param(
184 "2015-12-31 10:04 AM",
185 "Asia/Karachi",
186 "UTC",
187 datetime(2015, 12, 31, 5, 4),
188 ),
189 param(
190 "2015-12-30 10:04 AM",
191 "Asia/Karachi",
192 "+0200",
193 datetime(2015, 12, 30, 7, 4),
194 ),
195 ]
196 )
197 def test_timezone_conversion(self, datestring, from_tz, to_tz, expected):
198 self.given_from_timezone(from_tz)
199 self.given_to_timezone(to_tz)
200 self.when_date_is_parsed(datestring)
201 self.then_date_is(expected)
202
203 def given_from_timezone(self, timezone):
204 self.settings["TIMEZONE"] = timezone
205
206 def given_to_timezone(self, timezone):
207 self.settings["TO_TIMEZONE"] = timezone
208
209 def when_date_is_parsed(self, datestring):
210 self.result = self.parser(datestring, settings=self.settings)
211
212 def then_date_is(self, date):
213 self.assertEqual(date, self.result)
214
215
216 class TestStaticTzInfo(BaseTestCase):
217 def setUp(self):
218 super().setUp()
219
220 @parameterized.expand(
221 [
222 param(given_date=datetime(2007, 1, 18, tzinfo=timezone("UTC"))),
223 param(given_date=datetime(2003, 3, 31, tzinfo=timezone("US/Arizona"))),
224 param(given_date=datetime(2000, 2, 20, tzinfo=timezone("Pacific/Samoa"))),
225 ]
226 )
227 def test_localize_raises_error_if_date_has_tzinfo(self, given_date):
228 self.timezone_info = StaticTzInfo("UTC\\+00:00", timedelta(0))
229 self.when_date_is_localized(given_date)
230 self.then_error_was_raised(
231 ValueError, ["Not naive datetime (tzinfo is already set)"]
232 )
233
234 def when_date_is_localized(self, given_date):
235 try:
236 self.localized_date = self.timezone_info.localize(given_date)
237 except Exception as error:
238 self.error = error
239
240 def then_localized_date_is(self, expected_date, expected_tzname):
241 self.assertEqual(self.localized_date.date(), expected_date.date())
242 self.assertEqual(self.localized_date.tzname(), expected_tzname)