datetime与pytz时区。不同的偏移取决于如何设置tzinfo

编程入门 行业动态 更新时间:2024-10-27 06:19:10
本文介绍了datetime与pytz时区。不同的偏移取决于如何设置tzinfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我今天遇到一个有趣的情况。任何人都可以解释为什么ts1和ts2的偏移量不同? ts1是一个datetime对象,它是蝙蝠的时区感知权。 ts2是一个datetime对象,从timezone-naive开始,并替换其tzinfo。但是,他们最终会得到不同的偏移量。

>>>来自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

本文发布于:2023-11-04 10:28:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557681.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时区   如何设置   datetime   pytz   tzinfo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!