Debian Codegraph @

tests/test_utils_strptime.py

raw
1 import locale
2 from datetime import datetime
3 from unittest import SkipTest
4 import warnings
5
6 from parameterized import param, parameterized
7
8 from dateparser.utils.strptime import strptime
9 from tests import BaseTestCase
10
11
12 class TestStrptime(BaseTestCase):
13 def setUp(self):
14 super().setUp()
15
16 def given_system_locale_is(self, locale_str):
17 try:
18 locale.setlocale(locale.LC_ALL, locale_str)
19 except locale.Error:
20 raise SkipTest("Locale {} is not installed".format(locale_str))
21
22 def when_date_string_is_parsed(self, date_string, fmt):
23 try:
24 self.result = strptime(date_string, fmt)
25 except ValueError as e:
26 self.result = e
27
28 def when_date_string_is_parsed_using_datetime_strptime(self, date_string, fmt):
29 try:
30 self.result = datetime.strptime(date_string, fmt)
31 except ValueError as e:
32 self.result = e
33
34 def then_date_object_is(self, expected):
35 assert self.result == expected
36
37 def then_date_object_is_instance_of(self, expected):
38 assert isinstance(self.result, expected)
39
40 @parameterized.expand(
41 [
42 param("21 January 2010", "%d %B %Y", expected=datetime(2010, 1, 21, 0, 0)),
43 param("2 Mar 2010", "%d %b %Y", expected=datetime(2010, 3, 2, 0, 0)),
44 param(
45 "12 December 10 10:30",
46 "%d %B %y %H:%M",
47 expected=datetime(2010, 12, 12, 10, 30),
48 ),
49 param(
50 "12 December 10 22:41",
51 "%d %B %y %H:%M",
52 expected=datetime(2010, 12, 12, 22, 41),
53 ),
54 param(
55 "12 February 2016 11:41",
56 "%d %B %Y %I:%M",
57 expected=datetime(2016, 2, 12, 11, 41),
58 ),
59 param("21 Jan 2010", "%d %b %Y", expected=datetime(2010, 1, 21, 0, 0)),
60 param(
61 "12 Dec 10 10:30",
62 "%d %b %y %H:%M",
63 expected=datetime(2010, 12, 12, 10, 30),
64 ),
65 param(
66 "12 Feb 2016 11:41",
67 "%d %b %Y %I:%M",
68 expected=datetime(2016, 2, 12, 11, 41),
69 ),
70 ]
71 )
72 def test_dates_with_months_are_parsed_if_locale_is_non_english(
73 self, date_string, fmt, expected
74 ):
75 self.given_system_locale_is("fr_FR.UTF-8")
76 self.when_date_string_is_parsed(date_string, fmt)
77 self.then_date_object_is(expected)
78
79 @parameterized.expand(
80 [
81 param(
82 "Monday 21 January 2010",
83 "%A %d %B %Y",
84 expected=datetime(2010, 1, 21, 0, 0),
85 ),
86 param("Tue 2 Mar 2010", "%a %d %b %Y", expected=datetime(2010, 3, 2, 0, 0)),
87 param(
88 "Friday 12 December 10 10:30",
89 "%A %d %B %y %H:%M",
90 expected=datetime(2010, 12, 12, 10, 30),
91 ),
92 param(
93 "Wed 12 December 10 22:41",
94 "%a %d %B %y %H:%M",
95 expected=datetime(2010, 12, 12, 22, 41),
96 ),
97 param(
98 "Thu 12 February 2016 11:41",
99 "%a %d %B %Y %I:%M",
100 expected=datetime(2016, 2, 12, 11, 41),
101 ),
102 ]
103 )
104 def test_dates_with_days_are_parsed_if_locale_is_non_english(
105 self, date_string, fmt, expected
106 ):
107 self.given_system_locale_is("fr_FR.UTF-8")
108 self.when_date_string_is_parsed(date_string, fmt)
109 self.then_date_object_is(expected)
110
111 def test_parsing_date_should_fail_using_datetime_strptime_if_locale_is_non_english(
112 self,
113 ):
114 self.given_system_locale_is("fr_FR.UTF-8")
115 self.when_date_string_is_parsed_using_datetime_strptime(
116 "21 february 2010", "%d %B %Y"
117 )
118 self.then_date_object_is_instance_of(ValueError)
119
120 @parameterized.expand(
121 [
122 param(
123 "12 Dec 10 10:30:55.1",
124 "%d %b %y %H:%M:%S.%f",
125 expected=datetime(2010, 12, 12, 10, 30, 55, 100000),
126 ),
127 param(
128 "12 Dec 10 10:30:55.10",
129 "%d %b %y %H:%M:%S.%f",
130 expected=datetime(2010, 12, 12, 10, 30, 55, 100000),
131 ),
132 param(
133 "12 Dec 10 10:30:55.100",
134 "%d %b %y %H:%M:%S.%f",
135 expected=datetime(2010, 12, 12, 10, 30, 55, 100000),
136 ),
137 param(
138 "12 Dec 10 10:30:55.1000",
139 "%d %b %y %H:%M:%S.%f",
140 expected=datetime(2010, 12, 12, 10, 30, 55, 100000),
141 ),
142 param(
143 "12 Dec 10 10:30:55.100000",
144 "%d %b %y %H:%M:%S.%f",
145 expected=datetime(2010, 12, 12, 10, 30, 55, 100000),
146 ),
147 param(
148 "12 Dec 10 10:30:55.000001",
149 "%d %b %y %H:%M:%S.%f",
150 expected=datetime(2010, 12, 12, 10, 30, 55, 1),
151 ),
152 param(
153 "12 Dec 10 10:30:55.000011",
154 "%d %b %y %H:%M:%S.%f",
155 expected=datetime(2010, 12, 12, 10, 30, 55, 11),
156 ),
157 param(
158 "12 Dec 10 10:30:55.000111",
159 "%d %b %y %H:%M:%S.%f",
160 expected=datetime(2010, 12, 12, 10, 30, 55, 111),
161 ),
162 param(
163 "12 Feb 2016 11:41:23",
164 "%d %b %Y %I:%M:%S",
165 expected=datetime(2016, 2, 12, 11, 41, 23),
166 ),
167 param(
168 "11 Dec 10 10:30:2011.999999",
169 "%y %b %S %H:%M:%Y.%f",
170 expected=datetime(2011, 12, 1, 10, 30, 10, 999999),
171 ),
172 ]
173 )
174 def test_microseconds_are_parsed_correctly(self, date_string, fmt, expected):
175 self.when_date_string_is_parsed(date_string, fmt)
176 self.then_date_object_is(expected)
177
178 @parameterized.expand(
179 [
180 param(date_string="oct 14", fmt=r"%m %d"),
181 param(date_string="10-14", fmt=r"%b %d"),
182 param(date_string="12 Dec 10:30:55.000111", fmt="%d %b %H:%M:%S.%f"),
183 param(date_string="Wed 12 December 22:41", fmt="%a %d %B %H:%M"),
184 ]
185 )
186 def test_dates_with_no_year_do_not_raise_a_deprecation_warning(
187 self, date_string, fmt
188 ):
189 with warnings.catch_warnings(record=True) as w:
190 warnings.simplefilter("always")
191 self.when_date_string_is_parsed(date_string, fmt)
192 year_warnings = [
193 warn
194 for warn in w
195 if "day of month without a year specified is ambiguious"
196 in str(warn.message)
197 ]
198 self.assertEqual(len(year_warnings), 0)
199
200 @parameterized.expand(
201 [
202 param(
203 date_string="oct 14",
204 fmt=r"%b %d",
205 expected=datetime(2010, 10, 14, 0, 0),
206 ),
207 param(
208 date_string="10 14",
209 fmt=r"%m %d",
210 expected=datetime(2010, 10, 14, 0, 0),
211 ),
212 param(
213 date_string="14 Oct",
214 fmt=r"%d %b",
215 expected=datetime(2010, 10, 14, 0, 0),
216 ),
217 param(
218 "Monday 21 January",
219 "%A %d %B",
220 expected=datetime(2010, 1, 21, 0, 0),
221 ),
222 param(
223 "Tue 2 Mar",
224 "%a %d %b",
225 expected=datetime(2010, 3, 2, 0, 0),
226 ),
227 param(
228 "Friday 12 December 10:30",
229 "%A %d %B %H:%M",
230 expected=datetime(2010, 12, 12, 10, 30),
231 ),
232 ]
233 )
234 def test_dates_with_no_year_use_the_current_year(
235 self, date_string: str, fmt: str, expected: datetime
236 ):
237 self.when_date_string_is_parsed(date_string, fmt)
238 current_year = datetime.today().year
239 expected = expected.replace(year=current_year)
240 self.assertEqual(self.result, expected)
241
242 @parameterized.expand(
243 [
244 param("1999001", "%Y%j", expected=datetime(1999, 1, 1)),
245 param("1999032", "%Y%j", expected=datetime(1999, 2, 1)),
246 param("1999050", "%Y%j", expected=datetime(1999, 2, 19)),
247 param("1999059", "%Y%j", expected=datetime(1999, 2, 28)),
248 param("1999060", "%Y%j", expected=datetime(1999, 3, 1)),
249 param("1999365", "%Y%j", expected=datetime(1999, 12, 31)),
250 param("2000060", "%Y%j", expected=datetime(2000, 2, 29)),
251 param("2000061", "%Y%j", expected=datetime(2000, 3, 1)),
252 param("2000366", "%Y%j", expected=datetime(2000, 12, 31)),
253 param("2400366", "%Y%j", expected=datetime(2400, 12, 31)),
254 param("2023-100", "%Y-%j", expected=datetime(2023, 4, 10)),
255 param("2024 366", "%Y %j", expected=datetime(2024, 12, 31)),
256 param("1999001 12:30", "%Y%j %H:%M", expected=datetime(1999, 1, 1, 12, 30)),
257 ]
258 )
259 def test_day_of_year_is_parsed(self, date_string, fmt, expected):
260 self.when_date_string_is_parsed(date_string, fmt)
261 self.then_date_object_is(expected)
262
263 @parameterized.expand(
264 [
265 # 366 is only a valid day of year in leap years.
266 param("1999366", "%Y%j"),
267 param("2023-366", "%Y-%j"),
268 param("1900366", "%Y%j"), # divisible by 100 but not by 400
269 param("2100366", "%Y%j"),
270 param("2025366 12:30", "%Y%j %H:%M"),
271 # Values that strptime itself rejects.
272 param("1999000", "%Y%j"),
273 param("1999367", "%Y%j"),
274 param("1999777", "%Y%j"),
275 ]
276 )
277 def test_day_of_year_out_of_range_is_not_parsed(self, date_string, fmt):
278 self.when_date_string_is_parsed(date_string, fmt)
279 self.then_date_object_is_instance_of(ValueError)
280
281 @parameterized.expand(
282 [
283 param("%j", "%%j", expected=datetime(1900, 1, 1)),
284 param("1999%j", "%Y%%j", expected=datetime(1999, 1, 1)),
285 # A literal "%j" must not be taken for a day of year directive, not
286 # even when another directive rolls the year over.
287 param("2023 53 1 %j", "%Y %U %w %%j", expected=datetime(2024, 1, 1)),
288 ]
289 )
290 def test_escaped_day_of_year_directive_is_a_literal(
291 self, date_string, fmt, expected
292 ):
293 self.when_date_string_is_parsed(date_string, fmt)
294 self.then_date_object_is(expected)