|
1 | 1 | import locale |
2 | 2 | from datetime import datetime |
3 | 3 | from unittest import SkipTest |
| 4 | +import warnings |
4 | 5 |
|
5 | 6 | from parameterized import param, parameterized |
6 | 7 |
|
@@ -173,3 +174,67 @@ def test_parsing_date_should_fail_using_datetime_strptime_if_locale_is_non_engli |
173 | 174 | def test_microseconds_are_parsed_correctly(self, date_string, fmt, expected): |
174 | 175 | self.when_date_string_is_parsed(date_string, fmt) |
175 | 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) |
0 commit comments