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
派生
35.9k
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 摘要。