Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Doc/library/datetime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment on lines +2748 to +2754

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. 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.
.. note::
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.

A few little suggestions merged (sorry, it was easier with wrapping this way, feel free to split as you see fit!):

  • De-duplicate strptime.
  • "only the last directive is parsed": I fear "parsed" is a little misleading, as they do still have to match.
  • "same time unit": technically it's not so simple, e.g. %j always wins over %d due to parsing implementation details.
  • Also suggest adding a concrete example.


.. testsetup::

# doctest seems to turn the warning into an error which makes it
Expand Down
Loading