python / python/cpython

unexpected behavior of `datetime.astimezone` method

未關閉
#130,718 2 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

3.12 3.13 3.14 extension-modules stdlib type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

Bug report

Bug description:

my system time zone is UTC+04:00

>>> import datetime
>>> datetime.datetime(9999, 12, 31).astimezone() # same for .astimezone(datetime.UTC)
Traceback (most recent call last):
  File "<python-input-46>", line 1, in <module>
    datetime.datetime(9999, 12, 31).astimezone()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
ValueError: year must be in 1..9999, not 10000
>>> datetime.datetime(9999, 12, 31, tzinfo=datetime.UTC).astimezone()
datetime.datetime(9999, 12, 31, 4, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=14400), '+04'))

same behavior for _pydatetime module, but I found that in version 3.11 the behavior is as next:

>>> datetime.datetime(9999, 12, 31).astimezone()
datetime.datetime(9999, 12, 31, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=14400), '+04'))

EDIT: same behavior for datetime(1, 1, 1):

>>> datetime.datetime(1, 1, 1).astimezone()
Traceback (most recent call last):
  File "<python-input-23>", line 1, in <module>
    datetime.datetime(1, 1, 1).astimezone()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
ValueError: year must be in 1..9999, not 0
CPython versions tested on:

CPython main branch, 3.13, 3.12

Operating systems tested on:

Linux

Linked PRs
  • gh-130752

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 datetime.astimezone 入口點開始,在 CPython main 上重現回報的第 1 年和第 9999 年案例,並將其與 Python 3.11 的行為進行比較。繼續之前請檢視關聯的 PR gh-130752;當邊界案例不再意外引發錯誤,且預期行為已由相關測試涵蓋時,工作即告完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
backend
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
20/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。