Thanks to visit codestin.com
Credit goes to github.com

Skip to content

unexpected behavior of datetime.astimezone method #130718

Open
@donBarbos

Description

@donBarbos

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13bugs and security fixes3.14bugs and security fixesextension-modulesC modules in the Modules dirstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions