Skip to content

gh-73267: Fix tm_yday in time.strptime() when a week rolls over a year - #161828

Open
Pixeless001 wants to merge 1 commit into
python:mainfrom
Pixeless001:gh-73267-strptime-yday-rollover
Open

Pixeless001 wants to merge 1 commit into
python:mainfrom
Pixeless001:gh-73267-strptime-yday-rollover

Conversation

@Pixeless001

@Pixeless001 Pixeless001 commented Nov 11, 2026 •

Copy link
Copy Markdown

time.strptime() accepts a week number (%U or %W) and a weekday that name a day after December 31. It resolves the date to the next year, but tm_yday keeps the count from the year in the input:

>>> time.strptime('2016 52 0', '%Y %W %w')[:3]
(2017, 1, 1)
>>> time.strptime('2016 52 0', '%Y %W %w').tm_yday
367
>>> time.strptime('2015 52 5', '%Y %U %w').tm_yday  # 2016-01-01
366

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 return tm_yday=1.

Values given with %j are not changed: time.strptime('366', '%j').tm_yday is 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_datetime passes. Without the change to Lib/_strptime.py, the new test_last_week fails with (2019, 1, 1, 0, 0, 0, 1, 366) != (2019, 1, 1, 0, 0, 0, 1, 1). ./python -m test -j8 passes except test_ssl, which fails the same way on unmodified main in my build (no zlib module).

The change is the elif block at Lib/_strptime.py:780, next to the existing week 0 branch.

… 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.
@python-cla-bot

python-cla-bot Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

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.

1 participant