Skip to content

gh-124549: Document that strptime uses the last directive when a time unit is over-specified - #125009

Open
Mariatta wants to merge 2 commits into
python:mainfrom
Mariatta:123449-docs-about-dtformat
Open

Mariatta wants to merge 2 commits into
python:mainfrom
Mariatta:123449-docs-about-dtformat

Conversation

@Mariatta

@Mariatta Mariatta commented Oct 5, 2024 •

Copy link
Copy Markdown
Member

@bedevere-app bedevere-app Bot added docs Documentation in the Doc dir skip news awaiting core review labels Oct 5, 2024
@Mariatta Mariatta changed the title gh-123549: Add a note that when over-specifying datetime format, only the last directive gets used. GH-124549: Add a note that when over-specifying datetime format, only the last directive gets used. Oct 5, 2024
@Mariatta Mariatta added 3.12 only security fixes 3.13 only security fixes labels Oct 5, 2024
@alliesw

This comment was marked as off-topic.

Comment thread Doc/library/datetime.rst Outdated
: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`.

(11)

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.

Is a note here the best place for this?

This comment was marked as resolved.

@python python deleted a comment from wiglo232-ops Aug 25, 2025
@python python deleted a comment from wiglo232-ops Aug 25, 2025
@StanFromIreland StanFromIreland removed 3.12 only security fixes 3.13 only security fixes labels Aug 29, 2025
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 18, 2026
@Mariatta
Mariatta force-pushed the 123449-docs-about-dtformat branch from 4988071 to f8937f1 Compare October 19, 2026 19:02
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35250757 | 📁 Comparing f8937f1 against main (2d81eae)

  🔍 Preview build  

1 file changed
± library/datetime.html

Comment thread Doc/library/datetime.rst
Comment on lines +2748 to +2754
.. 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.

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.

@Mariatta Mariatta changed the title GH-124549: Add a note that when over-specifying datetime format, only the last directive gets used. gh-124549: Document that strptime uses the last directive when a time unit is over-specified Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review docs Documentation in the Doc dir skip news stale Stale PR or inactive for long period of time.

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

3 participants