Skip to content
Prev Previous commit
Next Next commit
Merge branch 'main' into docs/datetime/02-29
  • Loading branch information
gpshead committed Mar 21, 2025
commit 1f07bc60c43cf3a09af95c32da4734d370e88d6b
22 changes: 19 additions & 3 deletions Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2764,9 +2764,23 @@ Notes:
``%W``, and ``%V``. Format ``%y`` does require a leading zero.

(10)
Parsing dates without a year using :meth:`~.datetime.strptime` will fail on
representations of February 29 as that date does not exist in the default
year of 1900.
When parsing a month and day using :meth:`~.datetime.strptime`, always
include a year in the format. If the value you need to parse lacks a year,
append an explicit dummy leap year. Otherwise your code will raise an
exception when it encounters leap day because the default year used by the
parser (1900) is not a leap year. Users run into that bug every leap year.

.. doctest::

>>> month_day = "02/29"
>>> datetime.strptime(f"{month_day};1984", "%m/%d;%Y") # No leap year bug.
datetime.datetime(1984, 2, 29, 0, 0)

.. deprecated-removed:: 3.13 3.15
:meth:`~.datetime.strptime` calls using a format string containing
a day of month without a year now emit a
:exc:`DeprecationWarning`. In 3.15 or later we may change this into
an error or change the default year to a leap year. See :gh:`70647`.

.. rubric:: Footnotes

Expand All @@ -2781,3 +2795,5 @@ Notes:
.. [#] See R. H. van Gent's `guide to the mathematics of the ISO 8601 calendar
<https://web.archive.org/web/20220531051136/https://webspace.science.uu.nl/~gent0113/calendar/isocalendar.htm>`_
for a good explanation.

.. [#] Passing ``datetime.strptime('Feb 29', '%b %d')`` will fail since 1900 is not a leap year.
You are viewing a condensed version of this merge commit. You can view the full changes here.