Title: datetime.time.strftime('%Y') raises AttributeError in the pure-Python implementation · Issue #152305 · python/cpython · GitHub
Open Graph Title: datetime.time.strftime('%Y') raises AttributeError in the pure-Python implementation · Issue #152305 · python/cpython
X Title: datetime.time.strftime('%Y') raises AttributeError in the pure-Python implementation · Issue #152305 · python/cpython
Description: Bug report Bug description In the pure-Python datetime implementation (Lib/_pydatetime.py), datetime.time.strftime() raises AttributeError for the directives %Y, %G, %C and %F, while the C accelerator returns the correct values. A time h...
Open Graph Description: Bug report Bug description In the pure-Python datetime implementation (Lib/_pydatetime.py), datetime.time.strftime() raises AttributeError for the directives %Y, %G, %C and %F, while the C accelera...
X Description: Bug report Bug description In the pure-Python datetime implementation (Lib/_pydatetime.py), datetime.time.strftime() raises AttributeError for the directives %Y, %G, %C and %F, while the C accelera...
Opengraph URL: https://github.com/python/cpython/issues/152305
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"datetime.time.strftime('%Y') raises AttributeError in the pure-Python implementation","articleBody":"# Bug report\n\n## Bug description\n\nIn the pure-Python `datetime` implementation (`Lib/_pydatetime.py`),\n`datetime.time.strftime()` raises `AttributeError` for the directives `%Y`, `%G`,\n`%C` and `%F`, while the C accelerator returns the correct values. A `time` has no\ndate, so `strftime` fills the 1900-01-01 placeholder, and these directives should\nformat that placeholder.\n\n```python\n>>> import _pydatetime # pure implementation\n>>> _pydatetime.time(12, 30).strftime('%Y')\nTraceback (most recent call last):\n ...\n File \".../Lib/_pydatetime.py\", line 276, in _wrap_strftime\n object.year \u003c 1000 and _need_normalize_century()):\nAttributeError: 'time' object has no attribute 'year'\n```\n\nThe C accelerator is correct:\n\n```python\n>>> import datetime # C accelerator\n>>> datetime.time(12, 30).strftime('%Y')\n'1900'\n>>> datetime.time(12, 30).strftime('%C')\n'19'\n>>> datetime.time(12, 30).strftime('%F')\n'1900-01-01'\n>>> datetime.time(12, 30).strftime('%G')\n'1900'\n```\n\nDate-only directives such as `%j` already agree between the two implementations\n(both give `'001'`).\n\n## Root cause\n\n`_wrap_strftime(object, format, timetuple)` reads `object.year` in the\nyear-normalization branch:\n\n```python\nelif ((ch in 'YG' or ch in 'FC') and\n object.year \u003c 1000 and _need_normalize_century()):\n ...\n year = object.year\n```\n\n`time.strftime` passes the `time` object as `object` (it has no `.year`) together\nwith the fixed placeholder `timetuple` whose year is `1900`. Because\n`object.year \u003c 1000` is evaluated first in the `and` chain (before\n`_need_normalize_century()`), the `AttributeError` is raised on every platform,\nincluding ones where `_need_normalize_century()` is `False`.\n\nThe year actually being formatted is `timetuple[0]`, which equals `1900` for a\n`time` and equals `object.year` for `date`/`datetime`. Using `timetuple[0]` fixes\n`time` and leaves `date`/`datetime` unchanged.\n\n## Regression\n\nIntroduced by gh-120713 (the `%Y`/`%G` normalization branch) and widened to `%F`\nand `%C` by gh-122272.\n\n## Affected versions\n\n3.13+ (pure-Python implementation). The C accelerator is not affected.\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-152306\n* gh-152425\n* gh-152426\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/tonghuaroot","@type":"Person","name":"tonghuaroot"},"datePublished":"2026-06-26T15:40:40Z","url":"https://github.com/python/cpython/issues/152305"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:1a0b56b9-1534-83b1-4525-68e284ec546a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AA12:EAD6D:CB7EA0:FEA6AC:6B023A96 |
| html-safe-nonce | 86d5b49c7cfdc26bb06bd307012fc50718a7751cdfb3a1f55838f2f6bcc4234f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQTEyOkVBRDZEOkNCN0VBMDpGRUE2QUM6NkIwMjNBOTYiLCJ2aXNpdG9yX2lkIjoiMjYxNTQ4MTkwNTA1MDY5NjM0MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | bc4c29ba3216a5d3f8c4ef056a861bd75c439e9614a1735d47fb85e84bebfd26 |
| hovercard-subject-tag | issue:4753227438 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/152305/issue_layout |
| twitter:image | https://opengraph.githubassets.com/755c6f6c14e1961c4868a8b5a37145b5cb5a066802df1f00c1886f5a9a760985/python/cpython/issues/152305 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/755c6f6c14e1961c4868a8b5a37145b5cb5a066802df1f00c1886f5a9a760985/python/cpython/issues/152305 |
| og:image:alt | Bug report Bug description In the pure-Python datetime implementation (Lib/_pydatetime.py), datetime.time.strftime() raises AttributeError for the directives %Y, %G, %C and %F, while the C accelera... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tonghuaroot |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width