Debian Codegraph @

debian/patches/0004-assume-current-year-for-formats-without-it.patch

raw
1 From: Stuart Prescott <[email protected]>
2 Date: Tue, 15 Sep 2026 19:33:40 +1000
3 Subject: assume current year for formats without it
4
5 Provides compatibility for Python 3.15, with thanks to Maximiliano Curia
6 for forwarding the patch.
7
8 Cherry-picked from https://github.com/scrapinghub/dateparser/pull/1288
9
10 Closes: #1146805
11 ---
12 dateparser/date.py | 3 +-
13 dateparser/utils/strptime.py | 29 +++++++++++++++++++-
14 tests/test_parser.py | 25 +++++++++++++++++
15 tests/test_utils_strptime.py | 65 ++++++++++++++++++++++++++++++++++++++++++++
16 4 files changed, 120 insertions(+), 2 deletions(-)
17
18 diff --git a/dateparser/date.py b/dateparser/date.py
19 index 5d8f9a6..e234447 100644
20 --- a/dateparser/date.py
21 +++ b/dateparser/date.py
22 @@ -19,6 +19,7 @@ from dateparser.utils import (
23 set_correct_day_from_settings,
24 set_correct_month_from_settings,
25 )
26 +from dateparser.utils.strptime import strptime as patched_strptime
27
28 APOSTROPHE_LOOK_ALIKE_CHARS = [
29 "\N{RIGHT SINGLE QUOTATION MARK}", # '\u2019'
30 @@ -182,7 +183,7 @@ def parse_with_formats(date_string, date_formats, settings):
31 period = "day"
32 for date_format in date_formats:
33 try:
34 - date_obj = datetime.strptime(date_string, date_format)
35 + date_obj = patched_strptime(date_string, date_format)
36 except ValueError:
37 continue
38 else:
39 diff --git a/dateparser/utils/strptime.py b/dateparser/utils/strptime.py
40 index 226716c..b223567 100644
41 --- a/dateparser/utils/strptime.py
42 +++ b/dateparser/utils/strptime.py
43 @@ -90,7 +90,34 @@ def patch_strptime():
44 __strptime = patch_strptime()
45
46
47 -def strptime(date_string, format):
48 +def _prepare_format(date_string: str, og_format: str) -> tuple[str, str]:
49 + # Adapted from std lib: https://github.com/python/cpython/blob/e34a5e33049ce845de646cf24a498766a2da3586/Lib/_strptime.py#L448
50 + format = re.sub(r"([\\.^$*+?\(\){}\[\]|])", r"\\\1", og_format)
51 + format = re.sub(r"\s+", r"\\s+", format)
52 + format = re.sub(r"'", "['\u02bc]", format)
53 + year_in_format = False
54 + day_of_month_in_format = False
55 +
56 + def repl(m: re.Match[str]) -> str:
57 + format_char = m[1]
58 + if format_char in ("Y", "y", "G"):
59 + nonlocal year_in_format
60 + year_in_format = True
61 + elif format_char in ("d",):
62 + nonlocal day_of_month_in_format
63 + day_of_month_in_format = True
64 +
65 + return ""
66 +
67 + _ = re.sub(r"%[-_0^#]*[0-9]*([OE]?\\?.?)", repl, format)
68 + if day_of_month_in_format and not year_in_format:
69 + current_year = datetime.today().year
70 + return f"{current_year} {date_string}", f"%Y {og_format}"
71 + return date_string, og_format
72 +
73 +
74 +def strptime(date_string: str, format: str) -> datetime:
75 + date_string, format = _prepare_format(date_string, format)
76 obj = datetime(*__strptime(date_string, format)[:-3])
77
78 if "%f" in format:
79 diff --git a/tests/test_parser.py b/tests/test_parser.py
80 index fbeeb88..c1c5f6d 100644
81 --- a/tests/test_parser.py
82 +++ b/tests/test_parser.py
83 @@ -1,4 +1,5 @@
84 from datetime import datetime, time
85 +import warnings
86
87 from parameterized import param, parameterized
88
89 @@ -507,6 +508,30 @@ class TestParser(BaseTestCase):
90 with self.assertRaises(ValueError):
91 self.parser.parse(date_string, self.settings)
92
93 + @parameterized.expand(
94 + [
95 + param(date_string="oct 14"),
96 + param(date_string="14-October-2025"),
97 + param(date_string="2024-11-27"),
98 + param(date_string="tomorrow"),
99 + param(date_string="1484823450"),
100 + param(date_string="In two months"),
101 + ]
102 + )
103 + def test_parser_does_not_raise_ambiguious_date_deprecation_warning(
104 + self, date_string
105 + ):
106 + with warnings.catch_warnings(record=True) as w:
107 + warnings.simplefilter("always")
108 + self.when_date_is_parsed(date_string)
109 + year_warnings = [
110 + warn
111 + for warn in w
112 + if "day of month without a year specified is ambiguious"
113 + in str(warn.message)
114 + ]
115 + self.assertEqual(len(year_warnings), 0)
116 +
117
118 class TestTimeParser(BaseTestCase):
119 @parameterized.expand(
120 diff --git a/tests/test_utils_strptime.py b/tests/test_utils_strptime.py
121 index 690e55b..529158a 100644
122 --- a/tests/test_utils_strptime.py
123 +++ b/tests/test_utils_strptime.py
124 @@ -1,6 +1,7 @@
125 import locale
126 from datetime import datetime
127 from unittest import SkipTest
128 +import warnings
129
130 from parameterized import param, parameterized
131
132 @@ -173,3 +174,67 @@ class TestStrptime(BaseTestCase):
133 def test_microseconds_are_parsed_correctly(self, date_string, fmt, expected):
134 self.when_date_string_is_parsed(date_string, fmt)
135 self.then_date_object_is(expected)
136 +
137 + @parameterized.expand(
138 + [
139 + param(date_string="oct 14", fmt=r"%m %d"),
140 + param(date_string="10-14", fmt=r"%b %d"),
141 + param(date_string="12 Dec 10:30:55.000111", fmt="%d %b %H:%M:%S.%f"),
142 + param(date_string="Wed 12 December 22:41", fmt="%a %d %B %H:%M"),
143 + ]
144 + )
145 + def test_dates_with_no_year_do_not_raise_a_deprecation_warning(
146 + self, date_string, fmt
147 + ):
148 + with warnings.catch_warnings(record=True) as w:
149 + warnings.simplefilter("always")
150 + self.when_date_string_is_parsed(date_string, fmt)
151 + year_warnings = [
152 + warn
153 + for warn in w
154 + if "day of month without a year specified is ambiguious"
155 + in str(warn.message)
156 + ]
157 + self.assertEqual(len(year_warnings), 0)
158 +
159 + @parameterized.expand(
160 + [
161 + param(
162 + date_string="oct 14",
163 + fmt=r"%b %d",
164 + expected=datetime(2010, 10, 14, 0, 0),
165 + ),
166 + param(
167 + date_string="10 14",
168 + fmt=r"%m %d",
169 + expected=datetime(2010, 10, 14, 0, 0),
170 + ),
171 + param(
172 + date_string="14 Oct",
173 + fmt=r"%d %b",
174 + expected=datetime(2010, 10, 14, 0, 0),
175 + ),
176 + param(
177 + "Monday 21 January",
178 + "%A %d %B",
179 + expected=datetime(2010, 1, 21, 0, 0),
180 + ),
181 + param(
182 + "Tue 2 Mar",
183 + "%a %d %b",
184 + expected=datetime(2010, 3, 2, 0, 0),
185 + ),
186 + param(
187 + "Friday 12 December 10:30",
188 + "%A %d %B %H:%M",
189 + expected=datetime(2010, 12, 12, 10, 30),
190 + ),
191 + ]
192 + )
193 + def test_dates_with_no_year_use_the_current_year(
194 + self, date_string: str, fmt: str, expected: datetime
195 + ):
196 + self.when_date_string_is_parsed(date_string, fmt)
197 + current_year = datetime.today().year
198 + expected = expected.replace(year=current_year)
199 + self.assertEqual(self.result, expected)