Python:astimezone(None)提供已知的日期时间,不知道DST [英] Python: astimezone(None) gives aware datetime, unaware of DST

查看:77
本文介绍了Python:astimezone(None)提供已知的日期时间,不知道DST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

astimezone(None)是将日期时间对象本地化为本地时间的便捷方法,即您的OS设置(示例)

astimezone(None) is a convenient way to localize a datetime object to local time, i.e. your OS setting (docs, example).

如预期的那样, timedelta 显示墙时差 1 ,这是两个时间戳之间的3天:

As expected, the timedelta shows wall time difference1, which is 3 days between those timestamps:

from datetime import datetime, timezone
from zoneinfo import ZoneInfo

t_DSTactive = datetime(2020,10,23, tzinfo=ZoneInfo('Europe/Berlin'))
t_DSTinactive = datetime(2020,10,26, tzinfo=ZoneInfo('Europe/Berlin'))
print(t_DSTinactive - t_DSTactive)
# 3 days, 0:00:00

由于从DST启用到DST停用,UTC的持续时间为3天1小时:

Due to the change from DST active to DST inactive, the duration in UTC is 3 days and 1 hour:

t_DSTactive = t_DSTactive.astimezone(timezone.utc)
t_DSTinactive = t_DSTinactive.astimezone(timezone.utc)
print(t_DSTinactive - t_DSTactive)
# 3 days, 1:00:00

使用 asttimezone(None)进行本地化,一切似乎都很好(UTC + 2→UTC + 1):

Using asttimezone(None) to localize, everything seems to be fine (UTC+2 → UTC+1):

DSTactive = datetime(2020,10,23).astimezone(None)
DSTinactive = datetime(2020,10,26).astimezone(None)
print(DSTactive, DSTinactive)
# 2020-10-23 00:00:00+02:00 2020-10-26 00:00:00+01:00

...但是现在 timedelta 包含了DST转换后的+1小时:

...but the timedelta now includes the +1 hour from the DST transition:

print(DSTinactive - DSTactive)
# 3 days, 1:00:00

这是怎么回事?

  • 1 see also: Semantics of timezone-aware datetime arithmetic

推荐答案

查看通过 .astimezone(None)获得的日期时间对象的 repr(),我们看到 tzinfo 属性是"only"(仅);在两种情况下都是 timedelta :

Looking at the repr() of the datetime objects obtained with .astimezone(None), we see that the tzinfo attribute is "only" a timedelta in both cases:

print(repr(DSTactive))
print(repr(DSTinactive))
# datetime.datetime(2020, 10, 23, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=7200), 'Mitteleuropäische Sommerzeit'))
# datetime.datetime(2020, 10, 26, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=3600), 'Mitteleuropäische Zeit'))

换句话说,

固定的UTC偏移量 ,不是地理意义上的时区(包括规定的DST更改).当然,固定的偏移量并不知道DST的变化.考虑到偏移量,计算正确地给出了3天1小时的差异.

A fixed UTC offset in other words, not a time zone in a geographical sense (including prescribed DST changes). A fixed offset of course is not aware of DST changes. Taking into account the offsets, the calculation is correctly giving 3 days, 1 hour difference.

背景: 在datetime的

Background: In datetime's src, we see that astimezone(None) calls _local_timezone() to obtain the tzinfo, which returns that as a fixed offset.

虽然方便,但 astimezone(None)可能会在此处导致意外结果. 一种解决方法 ,此问题当然是将日期时间本地化为本地时区. tzlocal 可以在这里提供很大的帮助,尤其是在Windows上,它尤其不愿意放弃时区名称

While convenient, astimezone(None) can lead to unexpected results here. A way around this issue is of course to localize datetime to the local time zone. tzlocal can be of great assistance here, especially on Windows which is particularly reluctant to give away the time zone name.

这篇关于Python:astimezone(None)提供已知的日期时间,不知道DST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆