From 196f391a1df5b1ba12e6c257f886d54112f19b4d Mon Sep 17 00:00:00 2001 From: mariatta Date: Sat, 5 Oct 2024 11:15:49 -0700 Subject: [PATCH 1/3] Add a note that when over-specifying datetime format, only the last directive gets used. --- Doc/library/datetime.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst index f3c4ef9199075c9..94630399b910331 100644 --- a/Doc/library/datetime.rst +++ b/Doc/library/datetime.rst @@ -2924,6 +2924,14 @@ Notes: :meth:`~.datetime.strptime` calls using a format string containing ``%e`` without a year now emit a :exc:`DeprecationWarning`. +(11) + When parsing a string using :meth:`~.datetime.strptime`, if more than one + directives of the same time unit were used, only the last directive gets parsed + and applied to the datetime object. For example, if the format string contains + both ``%Y`` and ``%y``, like ``%Y%y``, only the last directive on the string (``%y``) + gets used on the resulting datetime object. + + .. rubric:: Footnotes .. [#] If, that is, we ignore the effects of relativity. From f8937f18b481f43e518a77b5ec870fd3ad145240 Mon Sep 17 00:00:00 2001 From: mariatta Date: Mon, 19 Oct 2026 10:58:02 -0700 Subject: [PATCH 2/3] adjust and reorganize the notes location, fast forward to latest. --- Doc/library/datetime.rst | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst index 94630399b910331..ce1fa47c654262c 100644 --- a/Doc/library/datetime.rst +++ b/Doc/library/datetime.rst @@ -2745,6 +2745,14 @@ in the format string will be pulled from the default value. the default year of 1900 is *not* a leap year. Always add a default leap year to partial date strings before parsing. +.. note:: + When parsing a string using :meth:`.datetime.strptime` or + :meth:`.date.strptime`, if more than one directive for the same time unit + is used, only the last directive is parsed and applied to the resulting + object. For example, if the format string contains both ``%Y`` and ``%y``, + like ``%Y%y``, only the last directive in the string (``%y``) determines + the year of the result. + .. testsetup:: # doctest seems to turn the warning into an error which makes it @@ -2924,14 +2932,6 @@ Notes: :meth:`~.datetime.strptime` calls using a format string containing ``%e`` without a year now emit a :exc:`DeprecationWarning`. -(11) - When parsing a string using :meth:`~.datetime.strptime`, if more than one - directives of the same time unit were used, only the last directive gets parsed - and applied to the datetime object. For example, if the format string contains - both ``%Y`` and ``%y``, like ``%Y%y``, only the last directive on the string (``%y``) - gets used on the resulting datetime object. - - .. rubric:: Footnotes .. [#] If, that is, we ignore the effects of relativity. From a1f91dea608bf3950d0cc9029387498b36b8423f Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 20 Oct 2026 10:45:39 -0700 Subject: [PATCH 3/3] Update Doc/library/datetime.rst Co-authored-by: Stan Ulbrych --- Doc/library/datetime.rst | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Doc/library/datetime.rst b/Doc/library/datetime.rst index ce1fa47c654262c..dd25200f73d4748 100644 --- a/Doc/library/datetime.rst +++ b/Doc/library/datetime.rst @@ -2746,12 +2746,11 @@ in the format string will be pulled from the default value. year to partial date strings before parsing. .. note:: - When parsing a string using :meth:`.datetime.strptime` or - :meth:`.date.strptime`, if more than one directive for the same time unit - is used, only the last directive is parsed and applied to the resulting - object. For example, if the format string contains both ``%Y`` and ``%y``, - like ``%Y%y``, only the last directive in the string (``%y``) determines - the year of the result. + If a :meth:`~.datetime.strptime` format string contains more than + one directive for the same field, for example both ``%Y`` and ``%y``, + every directive must still match the input, but only the value of the + last one is used. For example, parsing ``200099`` with ``%Y%y`` gives + the year 1999. Repeating the same directive, like ``%Y%Y``, raises an error. .. testsetup::