unexpected behavior of `datetime.astimezone` method
Open
Nobody has claimed this yet.
3.12
3.13
3.14
extension-modules
stdlib
type-bug
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the datetime.astimezone entry point and reproduce the reported year-1 and year-9999 cases on CPython main, comparing them with Python 3.11 behavior. Review linked PR gh-130752 before proceeding; the work is done when the boundary cases no longer raise unexpectedly and the intended behavior is covered by the relevant tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 20/100