datetime.astimezone 在 Python 3.9.5 和 Python 3.9.6 上的不同行为
Different behaviour of datetime.astimezone on Python 3.9.5 and Python 3.9.6
在 Python 3.9.6 上,这按预期工作:
import datetime
A = datetime.datetime(2021,1,1)
A.astimezone(datetime.timezone.utc)
>> datetime.datetime(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)
但是在 Python 3.9.5,我得到:
import datetime
A = datetime.datetime(2021,1,1)
A.astimezone(datetime.timezone.utc)
>> datetime.datetime(2020, 12, 31, 23, 0, tzinfo=datetime.timezone.utc)
为什么会出现这种行为?我只想通过保留原始日期时间数据向原始日期时间添加一个 utc 感知 tzone,如 3.9.6 中所示。
我已经在 Windows 上测试过 Python 版本。我机器的时区设置是 'Europe/Berlin',所以天真的日期时间 datetime(2021,1,1)
比 UTC 早一小时。因此转换应为 2020-12-31T23:00:00,如您在 the docs:
中所读
If provided, tz must be an instance of a tzinfo subclass, and its utcoffset() and dst() methods must not return None. If self is naive, it is presumed to represent time in the system timezone.
输出:
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May 3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime, timezone
>>> A = datetime(2021,1,1)
>>> A.astimezone(timezone.utc)
datetime.datetime(2020, 12, 31, 23, 0, tzinfo=datetime.timezone.utc)
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime, timezone
>>> A = datetime(2021,1,1)
>>> A.astimezone(timezone.utc)
datetime.datetime(2020, 12, 31, 23, 0, tzinfo=datetime.timezone.utc)
因此,在我 运行 这两个 Python 版本具有相同时区设置的同一台机器上的情况下,我无法重现所描述的行为。
在 Python 3.9.6 上,这按预期工作:
import datetime
A = datetime.datetime(2021,1,1)
A.astimezone(datetime.timezone.utc)
>> datetime.datetime(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc)
但是在 Python 3.9.5,我得到:
import datetime
A = datetime.datetime(2021,1,1)
A.astimezone(datetime.timezone.utc)
>> datetime.datetime(2020, 12, 31, 23, 0, tzinfo=datetime.timezone.utc)
为什么会出现这种行为?我只想通过保留原始日期时间数据向原始日期时间添加一个 utc 感知 tzone,如 3.9.6 中所示。
我已经在 Windows 上测试过 Python 版本。我机器的时区设置是 'Europe/Berlin',所以天真的日期时间 datetime(2021,1,1)
比 UTC 早一小时。因此转换应为 2020-12-31T23:00:00,如您在 the docs:
If provided, tz must be an instance of a tzinfo subclass, and its utcoffset() and dst() methods must not return None. If self is naive, it is presumed to represent time in the system timezone.
输出:
Python 3.9.5 (tags/v3.9.5:0a7dcbd, May 3 2021, 17:27:52) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime, timezone
>>> A = datetime(2021,1,1)
>>> A.astimezone(timezone.utc)
datetime.datetime(2020, 12, 31, 23, 0, tzinfo=datetime.timezone.utc)
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime, timezone
>>> A = datetime(2021,1,1)
>>> A.astimezone(timezone.utc)
datetime.datetime(2020, 12, 31, 23, 0, tzinfo=datetime.timezone.utc)
因此,在我 运行 这两个 Python 版本具有相同时区设置的同一台机器上的情况下,我无法重现所描述的行为。