为什么我在欧洲/柏林时区得到偏移量0:53?

7

示例代码

from datetime import datetime, timezone
import pytz

tzstring = 'Europe/Berlin'
t1 = datetime(2016, 6, 16, 2, 0, tzinfo=pytz.timezone(tzstring))
t2 = datetime(2016, 6, 16, 2, 0, tzinfo=timezone.utc).astimezone(pytz.timezone(tzstring))

观察到的

print(t1): 2016-06-16 02:00:00+00:53
print(t2): 2016-06-16 04:00:00+02:00

预期结果

print(t1): 2016-06-16 04:00:00+02:00  # does not match expectation
print(t2): 2016-06-16 04:00:00+02:00  # matches expectation

问题

有人能给我解释一下吗?

其他问题:


2
可能是[为什么pytz的localize()方法不能生成一个带有与其本地化对象匹配的tzinfo的datetime对象?]的重复问题。(链接:https://dev59.com/WGAf5IYBdhLWcg3wskiQ) - Sean Breckenridge
另一个答案也展示了为什么是那个值:LMT。我真的不认为在这样一个简单的问题中使用大标题有什么好处。 - jonrsharpe
原因并不是一个编程问题,而与德国时区历史有关。 - Sean Breckenridge
@SeanBreckenridge 哦,不错!这篇维基文章朝着正确的方向前进!但我仍然想知道当地时间是/如何计算的。 - Martin Thoma
1个回答

6

我不想说我可以完全解释它,但是记录表明它无法正常工作。来自pytz主页

This library only supports two ways of building a localized time. The first is to use the localize() method provided by the pytz library. This is used to localize a naive datetime (datetime with no timezone information)

(Example)

The second way of building a localized time is by converting an existing localized time using the standard astimezone() method.

(Example)

Unfortunately using the tzinfo argument of the standard datetime constructors 'does not work' with pytz for many timezones.

>>> datetime(2002, 10, 27, 12, 0, 0, tzinfo=amsterdam).strftime(fmt)
'2002-10-27 12:00:00 LMT+0020'

It is safe for timezones without daylight saving transitions though, such as UTC

我怀疑pytz中的时区表示与datetime构造函数使用的不兼容。与其追求精确细节,我认为更实际的做法是接受它无法工作并使用建议的替代方案。

(答案在我标记此问题为重复的那个问题中) - Sean Breckenridge
@SeanBreckenridge,这个答案可能对你很清晰,但对我来说肯定不是。是的,它在tz数据源中。但为什么会这样呢? - Martin Thoma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接