-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
gh-140715: Add %t and %n format codes support to strptime() #144896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jyalim
wants to merge
20
commits into
python:main
Choose a base branch
from
jyalim:fix-issue-140715--t
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
43c8345
add `%t` C99 token to strptime
jyalim d838236
init passing tests
jyalim a31ad12
drop `(0)` footnote from `%t` docs, reflecting support; add support note
jyalim 669870a
📜🤖 Added by blurb_it.
blurb-it[bot] d77ebf7
add support note
jyalim fcc03cf
modify %t -> more general r'\s+' from '\t' to meet C99
jyalim 5193157
use theclass() and test '\s+' functionality, as requested
jyalim edfed51
test '\s+' functionality, as requested
jyalim 16195db
alphabetize 3.15 patch notes
jyalim 7301962
update docs to reflect powerful %t strptime functionality
jyalim b753ea6
simplify docs, as requested
jyalim 9b29dbb
Update Doc/library/datetime.rst
jyalim d199d0b
Update Lib/test/datetimetester.py
jyalim dac5692
correctly capture '' with '\s*' instead of '\s+'
jyalim e9927fb
update tests with subtests for all whitespace chars covered by '\s'
jyalim c674612
init `%n` support for `strptime`
jyalim 3a26205
init tests for `%n` support in `strptime`
jyalim f709b80
update docs for `%n` support; drop (0) in footnote
jyalim 924b7cf
update NEWS to include `%n`
jyalim ed87c7d
%n before %t to match general style
jyalim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2207,6 +2207,32 @@ def test_strptime_D_format(self): | |
| self.theclass.strptime(test_date, "%m/%d/%y") | ||
| ) | ||
|
|
||
| def test_strptime_t_format(self): | ||
| test_year,test_month,test_day = 2026,2,20 | ||
| whitespaces = ('',' ','\t','\r','\v','\n','\f') | ||
| for ws in (*whitespaces,''.join(whitespaces)): | ||
| with self.subTest(whitespace=ws): | ||
| self.assertEqual( | ||
| self.theclass.strptime( | ||
| f'{test_year:04d}{ws}{test_month:02d}{ws}{test_day:02d}', | ||
| "%Y%t%m%t%d" | ||
| ), | ||
| self.theclass(test_year,test_month,test_day) | ||
| ) | ||
|
|
||
| def test_strptime_n_format(self): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The two tests can be merged with e.g. |
||
| test_year,test_month,test_day = 2026,2,25 | ||
| whitespaces = ('',' ','\t','\r','\v','\n','\f') | ||
| for ws in (*whitespaces,''.join(whitespaces)): | ||
| with self.subTest(whitespace=ws): | ||
| self.assertEqual( | ||
| self.theclass.strptime( | ||
| f'{test_year:04d}{ws}{test_month:02d}{ws}{test_day:02d}', | ||
| "%Y%n%m%n%d" | ||
| ), | ||
| self.theclass(test_year,test_month,test_day) | ||
| ) | ||
|
|
||
|
|
||
| ############################################################################# | ||
| # datetime tests | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -670,6 +670,38 @@ def test_strptime_D_format(self): | |
| time.strptime(test_date, "%m/%d/%y") | ||
| ) | ||
|
|
||
| def test_strptime_t_format(self): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same notes as above. |
||
| test_year,test_month,test_day = 2026,2,20 | ||
| whitespaces = ('',' ','\t','\r','\v','\n','\f') | ||
| for ws in (*whitespaces,''.join(whitespaces)): | ||
| with self.subTest(whitespace=ws): | ||
| self.assertEqual( | ||
| time.strptime( | ||
| f'{test_year:04d}{ws}{test_month:02d}{ws}{test_day:02d}', | ||
| "%Y%t%m%t%d" | ||
| ), | ||
| time.strptime( | ||
| f'{test_year:04d}-{test_month:02d}-{test_day:02d}', | ||
| "%Y-%m-%d" | ||
| ) | ||
| ) | ||
|
|
||
| def test_strptime_n_format(self): | ||
| test_year,test_month,test_day = 2026,2,20 | ||
| whitespaces = ('',' ','\t','\r','\v','\n','\f') | ||
| for ws in (*whitespaces,''.join(whitespaces)): | ||
| with self.subTest(whitespace=ws): | ||
| self.assertEqual( | ||
| time.strptime( | ||
| f'{test_year:04d}{ws}{test_month:02d}{ws}{test_day:02d}', | ||
| "%Y%n%m%n%d" | ||
| ), | ||
| time.strptime( | ||
| f'{test_year:04d}-{test_month:02d}-{test_day:02d}', | ||
| "%Y-%m-%d" | ||
| ) | ||
| ) | ||
|
|
||
| class Strptime12AMPMTests(unittest.TestCase): | ||
| """Test a _strptime regression in '%I %p' at 12 noon (12 PM)""" | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2026-02-17-03-43-07.gh-issue-140715.twmcM_.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add ``'%n'`` and ``'%t'`` support to :meth:`~datetime.datetime.strptime`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shorter names are clearer, and please follow PEP 8's whitespace guide.