为什么我得到时区 Europe/Berlin 的偏移量 0:53?
Why do I get the offset 0:53 for timezone Europe/Berlin?
示例代码
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
问题
有人可以给我解释一下吗?
其他问题:
- Why doesn't pytz localize() produce a datetime object with tzinfo matching the tz object that localized it?只求对"where in the code does it come from"的解释。我的问题更多的是方向:"Why is it that off?" - 我接受的答案很可能会包含一些历史。
我不想说我可以这样解释它,但它 被记录为无效。来自 pytz home page:
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 中时区的表示与日期时间构造函数使用的内容不兼容。
与其追逐确切的细节,我怀疑更实际的做法是接受它不起作用并使用建议的替代方案。
示例代码
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
问题
有人可以给我解释一下吗?
其他问题:
- Why doesn't pytz localize() produce a datetime object with tzinfo matching the tz object that localized it?只求对"where in the code does it come from"的解释。我的问题更多的是方向:"Why is it that off?" - 我接受的答案很可能会包含一些历史。
我不想说我可以这样解释它,但它 被记录为无效。来自 pytz home page:
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 中时区的表示与日期时间构造函数使用的内容不兼容。
与其追逐确切的细节,我怀疑更实际的做法是接受它不起作用并使用建议的替代方案。