Skip to content

Commit fc9d403

Browse files
committed
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
1 parent a696ba8 commit fc9d403

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
@@ -2587,9 +2587,42 @@ Broadly speaking, ``d.strftime(fmt)`` acts like the :mod:`time` module's
25872587
``time.strftime(fmt, d.timetuple())`` although not all objects support a
25882588
:meth:`~date.timetuple` method.
25892589

2590-
For the :meth:`.datetime.strptime` class method, the default value is
2591-
``1900-01-01T00:00:00.000``: any components not specified in the format string
2592-
will be pulled from the default value. [#]_
2590+
For the :meth:`.datetime.strptime` and :meth:`.date.strptime` class methods,
2591+
the default value is ``1900-01-01T00:00:00.000``: any components not specified
2592+
in the format string will be pulled from the default value.
2593+
2594+
.. note::
2595+
When used to parse partial dates lacking a year, :meth:`.datetime.strptime`
2596+
and :meth:`.date.strptime` will raise when encountering February 29 because
2597+
the default year of 1900 is *not* a leap year. Always add a default leap
2598+
year to partial date strings before parsing.
2599+
2600+
2601+
.. testsetup::
2602+
2603+
# doctest seems to turn the warning into an error which makes it
2604+
# show up and require matching and prevents the actual interesting
2605+
# exception from being raised.
2606+
# Manually apply the catch_warnings context manager
2607+
import warnings
2608+
catch_warnings = warnings.catch_warnings()
2609+
catch_warnings.__enter__()
2610+
warnings.simplefilter("ignore")
2611+
2612+
.. testcleanup::
2613+
2614+
catch_warnings.__exit__()
2615+
2616+
.. doctest::
2617+
2618+
>>> from datetime import datetime
2619+
>>> value = "2/29"
2620+
>>> datetime.strptime(value, "%m/%d")
2621+
Traceback (most recent call last):
2622+
...
2623+
ValueError: day 29 must be in range 1..28 for month 2 in year 1900
2624+
>>> datetime.strptime(f"1904 {value}", "%Y %m/%d")
2625+
datetime.datetime(1904, 2, 29, 0, 0)
25932626

25942627
Using ``datetime.strptime(date_string, format)`` is equivalent to::
25952628

@@ -2720,7 +2753,7 @@ Notes:
27202753
include a year in the format. If the value you need to parse lacks a year,
27212754
append an explicit dummy leap year. Otherwise your code will raise an
27222755
exception when it encounters leap day because the default year used by the
2723-
parser is not a leap year. Users run into this bug every four years...
2756+
parser (1900) is not a leap year. Users run into that bug every leap year.
27242757

27252758
.. doctest::
27262759

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

0 commit comments

Comments
 (0)