Content-Length: 289470 | pFad | http://github.com/python/cpython/pull/152306

A3 gh-152305: Fix pure-Python time.strftime AttributeError on %Y/%G/%C/%F by tonghuaroot · Pull Request #152306 · python/cpython · GitHub
Skip to content

gh-152305: Fix pure-Python time.strftime AttributeError on %Y/%G/%C/%F#152306

Open
tonghuaroot wants to merge 1 commit into
python:mainfrom
tonghuaroot:fix-gh-152305-strftime-time-year
Open

gh-152305: Fix pure-Python time.strftime AttributeError on %Y/%G/%C/%F#152306
tonghuaroot wants to merge 1 commit into
python:mainfrom
tonghuaroot:fix-gh-152305-strftime-time-year

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

_wrap_strftime in the pure-Python datetime implementation reads
object.year in its year-normalization branch. When strftime is called on a
datetime.time (which has no .year), this raises
AttributeError: 'time' object has no attribute 'year' for the %Y, %G, %C
and %F directives. The crash happens in the branch guard itself, before the
_need_normalize_century() platform check, so it occurs on every platform.

The C accelerator is correct: a time has no date, so strftime fills the
1900-01-01 placeholder and returns '1900', '19', '1900-01-01' and '1900'
respectively.

The year being formatted is timetuple[0], which is 1900 for a time and
object.year for a date/datetime. This replaces object.year with
timetuple[0] at both occurrences, fixing time and leaving date/datetime
behaviour unchanged: the new pure-Python output equals the long-standing C
accelerator output.

Tests in TestTime.test_strftime assert the four directives, and run under both
the C and pure implementations via the test_datetime parametrization.

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









ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/python/cpython/pull/152306

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy