有人能帮我理解这里发生了什么吗?
import pytz
from datetime import datetime
tz = pytz.timezone('Europe/Berlin')
print repr(tz)
# <DstTzInfo 'Europe/Berlin' LMT+0:53:00 STD>
dt = datetime(2011, 1, 3, 18, 40)
result = tz.localize(dt)
print repr(result.tzinfo)
# <DstTzInfo 'Europe/Berlin' CET+1:00:00 STD>
assert result.tzinfo == tz, "Why aren't these the same timezone?"我的理解是,pytz时区对象上的localize()方法将接受一个朴素的datetime对象,并添加一个与执行本地化的时区对象匹配的tzinfo属性。在这种情况下,这种情况似乎没有发生。
显然,对于时区,或者pytz处理时区的方式,我有些误解。有人能解释吗?
发布于 2014-06-23 16:14:47
他们是同一时区- "Europe/Berlin"。
在打印它们时,输出包括在特定时间点应用的缩写和偏移量。
如果您检查tz数据源,您将看到:
# Zone NAME GMTOFF RULES FORMAT [UNTIL]
Zone Europe/Berlin 0:53:28 - LMT 1893 Apr
1:00 C-Eur CE%sT 1945 May 24 2:00
1:00 SovietZone CE%sT 1946
1:00 Germany CE%sT 1980
1:00 EU CE%sT因此,当时区没有本地化日期时间时,它只使用第一个条目。
这似乎也表明,pytz不保留额外的28秒,从最初的当地平均时间偏差-但这并不重要,除非你在柏林的日期在1893年4月之前。
https://stackoverflow.com/questions/24359540
复制相似问题