gh-102450: Add ISO-8601 alternative for midnight to `fromisoformat()` calls. by TizzySaurus · Pull Request #105856 · python/cpython
I've also just noticed that the datetime and _pydatetime modules give different errors when specifying an invalid day:
>>> from datetime import datetime as c_datetime >>> c_datetime.fromisoformat("2023-01-32T24:00:00") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: day is out of range for month
vs
>>> from _pydatetime import datetime as py_datetime >>> py_datetime.fromisoformat("2023-01-32T24:00:00") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/xxx/Documents/personal/cpython/Lib/_pydatetime.py", line 1906, in fromisoformat return cls(*(date_components + time_components)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/xxx/Documents/personal/cpython/Lib/_pydatetime.py", line 1717, in __new__ year, month, day = _check_date_fields(year, month, day) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/xxx/Documents/personal/cpython/Lib/_pydatetime.py", line 545, in _check_date_fields raise ValueError('day must be in 1..%d' % dim, day) ValueError: ('day must be in 1..31', 32)
Note how _pydatetime's error message specifies the actual day range for that month. Is this something I should add to the C implementation whilst I'm here? Should just be case of swapping PyErr_SetString() with PyErr_Format() -- although not sure how to replicate it being a tuple.