datetime与pytz时区。不同的偏移取决于如何设置tzinfo [英] datetime with pytz timezone. Different offset depending on how tzinfo is set
问题描述
>>>来自pytz import timezone
>>>> EST =时区('America / New_York')
>>> ts1 = datetime.datetime.now(tz = EST)
>>> ts2 = datetime.datetime.now()
>>> ts2 = ts2.replace(tzinfo = EST)
>>>打印ts1
2014-05-16 11:25:16.749748-04:00
>>>打印ts2
2014-05-16 11:25:19.581710-05:00
当您调用 ts2.replace(tzinfo = EST)
时, tzinfo
你得到的不符合你所得到的 ts1
:
>>> ts1
datetime.datetime(2014,5,16,11,51,7,916090,tzinfo =< DstTzInfo'America / New_York'EDT-1天,20:00:00 DST>)
>>> ts2
datetime.datetime(2014,5,16,11,51,30,922692,tzinfo =< DstTzInfo'America / New_York'LMT-1天,19:04:00 STD>)
您最终使用LMT而不是EDT。
pytz
文档实际上注意到使用标准datetime对象的
根本不适用于许多时区: tzinfo
参数的pytz
不幸的是,使用标准datetime
构造函数的tzinfo参数对于许多时区来说并不适用于pytz。
>>> datetime(2002,10,27,12,0,0,tzinfo = amsterdam).strftime(fmt)'2002-10-27 12:00:00 LMT + 0020'
对于没有夏令时转换的时区,如UTC:
>>> datetime(2002,10,27,12,0,0,tzinfo = pytz.utc).strftime(fmt)'2002-10-27 12:00:00 UTC + 0000'
我不知道为什么第一个工作;也许是因为当对象最初使用 tzinfo
对象构造时,实际上不需要转换任何东西。
编辑:
啊,Python 文档注意到使用 datetime.datetime.now()
与 tz
arg相当于:
EST.fromutc(datetime.utcnow()。replace(tzinfo = EST))
这意味着你正在从UTC转换,这是安全的, pytz
。所以这就是为什么第一个工作。
I ran across an interesting situation today. Can anyone explain why the offsets for ts1 and ts2 are different? ts1 is a datetime object that is timezone-aware right off the bat. ts2 is a datetime object that starts off timezone-naive and has its tzinfo replaced. However, they end up with different offsets.
>>> from pytz import timezone
>>> EST = timezone('America/New_York')
>>> ts1 = datetime.datetime.now(tz=EST)
>>> ts2 = datetime.datetime.now()
>>> ts2 = ts2.replace(tzinfo=EST)
>>> print ts1
2014-05-16 11:25:16.749748-04:00
>>> print ts2
2014-05-16 11:25:19.581710-05:00
When you call ts2.replace(tzinfo=EST)
, the tzinfo
object you're getting doesn't match the one you get with ts1
:
>>> ts1
datetime.datetime(2014, 5, 16, 11, 51, 7, 916090, tzinfo=<DstTzInfo 'America/New_York' EDT-1 day, 20:00:00 DST>)
>>> ts2
datetime.datetime(2014, 5, 16, 11, 51, 30, 922692, tzinfo=<DstTzInfo 'America/New_York' LMT-1 day, 19:04:00 STD>)
You end up with LMT instead of EDT.
The pytz
documentation actually notes that using pytz
with the tzinfo
argument of standard datetime objects simply doesn't work for many timezones:
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:
>>> datetime(2002, 10, 27, 12, 0, 0, tzinfo=pytz.utc).strftime(fmt) '2002-10-27 12:00:00 UTC+0000'
I'm not exactly sure why the first one works; perhaps because it doesn't actually have to convert anything when the object is initially constructed with the tzinfo
object.
Edit:
Ah, the Python documentation notes that using datetime.datetime.now()
with the tz
arg is equivalent to:
EST.fromutc(datetime.utcnow().replace(tzinfo=EST))
Which means you're converting from UTC, which is safe with pytz
. So that's why the first one works.
这篇关于datetime与pytz时区。不同的偏移取决于如何设置tzinfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!