Repository navigation
gh-73267: Fix tm_yday in time.strptime() when a week rolls over a year - #161828
Open
Pixeless001 wants to merge 1 commit into
Open
Pixeless001 wants to merge 1 commit into
Pixeless001 wants to merge 1 commit into
Conversation
… a year A week number (%U or %W) and a weekday can name a day after December 31, for example "2016 52 0" with "%Y %W %w", which is 2017-01-01. The date fields were resolved to the next year, but tm_yday kept the day count from the year in the input: 367 here, and 366 for 2016-01-01 parsed from "2015 52 5" with "%Y %U %w". A day in week 0 before January 1 is already moved to the previous year. Do the same in the other direction, so that tm_yday matches the date. Day numbers given with %j are not changed.
Pixeless001
requested review from
StanFromIreland and
pganssle
as code owners
November 11, 2026 20:42
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
time.strptime()accepts a week number (%Uor%W) and a weekday that name a day after December 31. It resolves the date to the next year, buttm_ydaykeeps the count from the year in the input:367 is outside the documented range, and 366 is the value for December 31.
_strptime()already moves a week 0 day that falls before January 1 into the previous year. This change does the same at the other end of the year, so both calls returntm_yday=1.Values given with
%jare not changed:time.strptime('366', '%j').tm_ydayis 366 today, and code can depend on that. GH-158459, which its author closed, changed that case too. I can add it if you want it.Tested on Linux (WSL Ubuntu, gcc 15) with main at 0ada998.
./python -m test test_strptime test_time test_datetimepasses. Without the change toLib/_strptime.py, the newtest_last_weekfails with(2019, 1, 1, 0, 0, 0, 1, 366) != (2019, 1, 1, 0, 0, 0, 1, 1)../python -m test -j8passes excepttest_ssl, which fails the same way on unmodified main in my build (no zlib module).The change is the
elifblock atLib/_strptime.py:780, next to the existing week 0 branch.