为什么用pytz中的tzinfo创建日期时间会显示奇怪的时间偏移?

Kwn*_*sos 5 python timezone datetime pytz

有人可以解释一下为什么我不能得到相同的结果吗?

import datetime,pytz
var1 = datetime.datetime(2017,10,25,20,10,50,tzinfo=pytz.timezone("Europe/Athens")))
print(var1)
Run Code Online (Sandbox Code Playgroud)

此代码的输出是: 2017-10-25 20:10:50+01:35

import datetime,pytz
var1 = datetime.datetime(2017,10,25,20,10,50)
var1 = pytz.timezone("Europe/Athens").localize(var1)
print(var1)
Run Code Online (Sandbox Code Playgroud)

此代码的输出是: 2017-10-25 20:10:50+03:00

我的问题是为什么他们有不同的时区(1:35和3:00).我知道第二个代码是真的,因为我的UTC是3:00.但是你能告诉我为什么我要进入1:35第一个吗?

MSe*_*ert 5

没有问题,datetime只是高兴地报告在任何参考系中的偏移量tzinfo

默认情况下,pytz.timezone不提供 UTC 偏移量,而是提供LMT(本地平均时间)偏移量:

>>> pytz.timezone("Europe/Athens")
<DstTzInfo 'Europe/Athens' LMT+1:35:00 STD>
#                          ^^^-------------------- local mean time
Run Code Online (Sandbox Code Playgroud)

然而,当你本地化它时:

>>> var1 = datetime.datetime(2017,10,25,20,10,50)
>>> var1 = pytz.timezone("Europe/Athens").localize(var1)
>>> var1.tzinfo
<DstTzInfo 'Europe/Athens' EEST+3:00:00 DST>
#                          ^^^^-------------------- eastern european summer time
Run Code Online (Sandbox Code Playgroud)

现在报告了不同的偏移量,这次是基于 EEST。