Skip to content

Commit bc181c7

Browse files
Filipe Rossetmathstuf
authored andcommitted
strptime: monkey patch for day-without-year behavior change
1 parent 653612e commit bc181c7

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

khal/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,23 @@
3939
__description__ = "A standards based terminal calendar"
4040
__license__ = "Expat/MIT, see COPYING"
4141
__homepage__ = "https://lostpackets.de/khal/"
42+
43+
44+
# Monkeypatch _strptime to support Python 3.15+ where parsing day of month (%d or %e)
45+
# without a year directive raises a ValueError.
46+
try:
47+
import _strptime
48+
except ImportError:
49+
pass
50+
else:
51+
_orig_strptime = _strptime._strptime
52+
53+
def _custom_strptime(data_string, format_string):
54+
has_year = "%y" in format_string or "%Y" in format_string
55+
has_day = "%d" in format_string or "%e" in format_string
56+
if has_day and not has_year:
57+
data_string = f"{data_string} 2024"
58+
format_string = f"{format_string} %Y"
59+
return _orig_strptime(data_string, format_string)
60+
61+
_strptime._strptime = _custom_strptime

0 commit comments

Comments
 (0)