Skip to content

gh-140715: Add %t and %n format codes support to strptime() - #144896

Merged
encukou merged 33 commits into
python:mainfrom
jyalim:fix-issue-140715--t
Mar 13, 2026
Merged

gh-140715: Add %t and %n format codes support to strptime()#144896
encukou merged 33 commits into
python:mainfrom
jyalim:fix-issue-140715--t

Conversation

@jyalim

@jyalimjyalim commented Feb 17, 2026

Copy link
Copy Markdown
Contributor

Incrementally builds C99+ datetime support with addition of %t and %n with two lines of code. Also adds tests and updates documentation.

Continued effort from PR: #140647 , #144819.

Documentation drops (0) footnote.

Passed all tests.

Passed patchcheck.

Issue: #140715

Note, this PR originally only was for %t; %n was added 2026-02-25.


📚 Documentation preview 📚: https://cpython-previews--144896.org.readthedocs.build/

@StanFromIrelandStanFromIreland self-assigned this Feb 17, 2026

@StanFromIrelandStanFromIreland left a comment

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.

This is quite an odd case, as per the standard for strftime, it is a \t character, however, for strptime():

A series of conversion specifications composed of %n, %t, white-space bytes, or any combination is executed by scanning up to the first non-white-space byte (which remains unscanned), or until no more characters can be scanned.

And, more straightforward:

Arbitrary whitespace.

Considering that this is the wide-spread behaviour, it should instead be r'\s+' for %n/%t (with additional tests), and add a note to the doc.

Comment threadDoc/library/datetime.rst Outdated
Comment threadLib/test/datetimetester.py Outdated
@jyalim

Copy link
Copy Markdown
ContributorAuthor

Thanks, this was a great catch that adds some power to %t for strptime. I've implemented the changes!

Comment threadDoc/library/datetime.rst Outdated
@jyalim

Copy link
Copy Markdown
ContributorAuthor

Thank you, the documentation changes were implemented.

@StanFromIrelandStanFromIreland left a comment

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.

We could also do \n now, since it is equivalent. (re-use the tests please)

Comment threadDoc/library/datetime.rst Outdated
Comment threadLib/test/datetimetester.py Outdated
Comment threadLib/_strptime.py Outdated
Comment threadLib/test/datetimetester.py Outdated
jyalimand others added 2 commits February 25, 2026 22:25
typo fix
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
remove trailing whitespace
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
@jyalimjyalim changed the title gh-140715: Add %t format code support to strptime()gh-140715: Add %t and %n format codes support to strptime()Feb 26, 2026
@jyalim

Copy link
Copy Markdown
ContributorAuthor

Thanks! All the requested changes were implemented. The title and description of this PR were also updated to include %n.

Comment threadLib/test/datetimetester.py Outdated
Comment threadLib/test/datetimetester.py Outdated
Comment threadLib/test/test_strptime.py Outdated
@jyalim

Copy link
Copy Markdown
ContributorAuthor

Thank you, the tests have been merged and all requested changes have been implemented.

Comment threadDoc/library/datetime.rst Outdated
Comment threadDoc/library/datetime.rst Outdated
Comment threadMisc/NEWS.d/next/Library/2026-02-17-03-43-07.gh-issue-140715.twmcM_.rst Outdated
Comment threadLib/test/test_strptime.py Outdated
jyalimand others added 4 commits March 1, 2026 07:49
clarify "arbitrary" with "zero or more"
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
clarify "arbitrary" with "zero or more"
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
…wmcM_.rst
Drop quotes around format directives
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
@StanFromIreland

Copy link
Copy Markdown
Member

Something broke with the CI, I'll try updating the branch.

@jyalim

Copy link
Copy Markdown
ContributorAuthor

Thanks! Changes have been made.

Comment threadLib/test/datetimetester.py

@StanFromIrelandStanFromIreland left a comment

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.

LGTM

Comment threadLib/_strptime.py Outdated
Comment threadLib/test/datetimetester.py Outdated
Comment threadLib/test/datetimetester.py Outdated
Comment threadLib/test/datetimetester.py Outdated
Comment threadLib/test/datetimetester.py Outdated

def test_strptime_n_and_t_format(self):
whitespaces = ('', ' ', '\t', '\r', '\v', '\n', '\f')
for fd in ('n', 't'):

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.

Would it make sense to also test '%n%t' and the non-empty whitespaces as part of the pattern?

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.

Would it make sense to add whitespace to the table in the docs? (It already worked before, but AFAIK wasn't documented/tested.)

@jyalimjyalimMar 3, 2026

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Commit d6ca685 added '%n%t' and '%t%n'.

The final test for each whitespace format directive is ' \t\r\v\n\f', due to the ''.join(whitespaces) argument in the loop. Did you have another non-empty whitespace pattern in mind?

I'm all for improved documentation, could you clarify what you are envisioning? The whitespace format directives (%n and %t) are specified in the format directive table.

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.

(By pattern I meant fd; thanks for doing that change!)

In docs, I meant adding entries for space and tab, in addition to the percent directives, to document that strptime “collapses” runs of whitespace kand %t/%n.

jyalimand others added 5 commits March 3, 2026 06:44
PEP 8 fix on existing code
Co-authored-by: Petr Viktorin <encukou@gmail.com>
PEP 8 comma for exploded function arguments
Co-authored-by: Petr Viktorin <encukou@gmail.com>
…lso add PEP 8 trailing commas to all exploded function arguments
@encukou

Copy link
Copy Markdown
Member

The remaining doc update can be done in a separate PR.

@encukou
encukou merged commit f884dc6 into python:mainMar 13, 2026
47 checks passed
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
…thonGH-144896)
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@jyalim@StanFromIreland@encukou