Skip to content

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

Open
jyalim wants to merge 20 commits intopython:mainfrom
jyalim:fix-issue-140715--t
Open

gh-140715: Add %t and %n format codes support to strptime()#144896
jyalim wants to merge 20 commits intopython:mainfrom
jyalim:fix-issue-140715--t

Conversation

@jyalim
Copy link
Contributor

@jyalim jyalim commented Feb 17, 2026

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/

@StanFromIreland StanFromIreland self-assigned this Feb 17, 2026
Copy link
Member

@StanFromIreland StanFromIreland left a comment

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.

@jyalim
Copy link
Contributor Author

jyalim commented Feb 21, 2026

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

@jyalim
Copy link
Contributor Author

jyalim commented Feb 22, 2026

Thank you, the documentation changes were implemented.

Copy link
Member

@StanFromIreland StanFromIreland left a comment

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)

jyalim and 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>
@jyalim jyalim 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
Contributor Author

jyalim commented Feb 26, 2026

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

self.theclass(test_year,test_month,test_day)
)

def test_strptime_n_format(self):
Copy link
Member

Choose a reason for hiding this comment

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

The two tests can be merged with e.g. for d in ('n', 't'): ...

)

def test_strptime_t_format(self):
test_year,test_month,test_day = 2026,2,20
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
test_year,test_month,test_day = 2026,2,20
year, month, day = 2026, 2, 20

Shorter names are clearer, and please follow PEP 8's whitespace guide.

time.strptime(test_date, "%m/%d/%y")
)

def test_strptime_t_format(self):
Copy link
Member

Choose a reason for hiding this comment

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

Same notes as above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants