为什么我得到欧洲/柏林时区的偏移量0:53? [英] Why do I get the offset 0:53 for timezone Europe/Berlin?

查看:49
本文介绍了为什么我得到欧洲/柏林时区的偏移量0:53?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

问题

有人可以向我解释一下吗?

Question

Can somebody please explain that to me?

  • Why doesn't pytz localize() produce a datetime object with tzinfo matching the tz object that localized it? only asks for an explanation to "where in the code does it come from". My question is more in the direction: "Why is it that off?" - so likely a bit of history will be in an answer that I accept.

推荐答案

我不想说我可以这样解释,但是它记录了 无效.从 pytz主页:

I wouldn't like to say I can explain it as such, but it is documented to not work. From the pytz home page:

此库仅支持构建本地化时间的两种方式.第一种是使用pytz库提供的 localize()方法.这用于本地化原始日期时间(没有时区信息的日期时间)

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)

(示例)

构建本地化时间的第二种方法是使用标准的 astimezone()方法转换现有的本地化时间.

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

(示例)

不幸的是,在许多时区中,使用标准日期时间构造函数的tzinfo参数对pytz均不起作用.

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

它对于没有夏令时转换的时区是安全的,例如UTC

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

我怀疑以pytz表示的时区与datetime构造函数使用的格式不兼容.

I suspect the representation of time zones in pytz is just incompatible with what the datetime constructor uses.

我想追逐确切的细节,而不是追逐确切的细节,只是接受它不起作用并使用建议的替代方法.

Rather than chase the exact details, I suspect it's more practical just to accept it doesn't work and use the alternatives suggested.

这篇关于为什么我得到欧洲/柏林时区的偏移量0:53?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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