Skip to content

Commit 362d11e

Browse files
[3.14] gh-70647: Better promote how to safely parse yearless dates in datetime. (GH-116179) (#143037)
gh-70647: Better promote how to safely parse yearless dates in datetime. (GH-116179) * gh-70647: Better promote how to safely parse yearless dates in datetime. Every four years people encounter this because it just isn't obvious. This moves the footnote up to a note with a code example. We'd love to change the default year value for datetime but doing that could have other consequences for existing code. This documented workaround *always* works. * doctest code within note is bad, dedent. * Update to match the error message. * remove no longer referenced footnote * ignore the warning in the doctest * use Petr's suggestion for the docs to hide the warning processing * cover date.strptime (3.14) as well (cherry picked from commit b8d3fdd) Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
1 parent 27648a1 commit 362d11e

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

Doc/library/datetime.rst

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,9 +2634,42 @@ Broadly speaking, ``d.strftime(fmt)`` acts like the :mod:`time` module's
26342634
``time.strftime(fmt, d.timetuple())`` although not all objects support a
26352635
:meth:`~date.timetuple` method.
26362636

2637-
For the :meth:`.datetime.strptime` class method, the default value is
2638-
``1900-01-01T00:00:00.000``: any components not specified in the format string
2639-
will be pulled from the default value. [#]_
2637+
For the :meth:`.datetime.strptime` and :meth:`.date.strptime` class methods,
2638+
the default value is ``1900-01-01T00:00:00.000``: any components not specified
2639+
in the format string will be pulled from the default value.
2640+
2641+
.. note::
2642+
When used to parse partial dates lacking a year, :meth:`.datetime.strptime`
2643+
and :meth:`.date.strptime` will raise when encountering February 29 because
2644+
the default year of 1900 is *not* a leap year. Always add a default leap
2645+
year to partial date strings before parsing.
2646+
2647+
2648+
.. testsetup::
2649+
2650+
# doctest seems to turn the warning into an error which makes it
2651+
# show up and require matching and prevents the actual interesting
2652+
# exception from being raised.
2653+
# Manually apply the catch_warnings context manager
2654+
import warnings
2655+
catch_warnings = warnings.catch_warnings()
2656+
catch_warnings.__enter__()
2657+
warnings.simplefilter("ignore")
2658+
2659+
.. testcleanup::
2660+
2661+
catch_warnings.__exit__()
2662+
2663+
.. doctest::
2664+
2665+
>>> from datetime import datetime
2666+
>>> value = "2/29"
2667+
>>> datetime.strptime(value, "%m/%d")
2668+
Traceback (most recent call last):
2669+
...
2670+
ValueError: day 29 must be in range 1..28 for month 2 in year 1900
2671+
>>> datetime.strptime(f"1904 {value}", "%Y %m/%d")
2672+
datetime.datetime(1904, 2, 29, 0, 0)
26402673

26412674
Using ``datetime.strptime(date_string, format)`` is equivalent to::
26422675

@@ -2767,7 +2800,7 @@ Notes:
27672800
include a year in the format. If the value you need to parse lacks a year,
27682801
append an explicit dummy leap year. Otherwise your code will raise an
27692802
exception when it encounters leap day because the default year used by the
2770-
parser is not a leap year. Users run into this bug every four years...
2803+
parser (1900) is not a leap year. Users run into that bug every leap year.
27712804

27722805
.. doctest::
27732806

@@ -2794,5 +2827,3 @@ Notes:
27942827
.. [#] See R. H. van Gent's `guide to the mathematics of the ISO 8601 calendar
27952828
<https://web.archive.org/web/20220531051136/https://webspace.science.uu.nl/~gent0113/calendar/isocalendar.htm>`_
27962829
for a good explanation.
2797-
2798-
.. [#] Passing ``datetime.strptime('Feb 29', '%b %d')`` will fail since 1900 is not a leap year.

0 commit comments

Comments
 (0)