gpt4 book ai didi

python - `datetime.now(pytz_timezone)` 什么时候失败?

转载 作者:太空狗 更新时间:2023-10-29 18:04:30 26 4
gpt4 key购买 nike

delorean docs以这种方式显示以获取给定时区的当前时间 using datetime :

from datetime import datetime
from pytz import timezone

EST = "US/Eastern"
UTC = "UTC"

d = datetime.utcnow()
utc = timezone(UTC)
est = timezone(EST)
d = utc.localize(d)
d = est.normalize(EST)

并将其与基于 delorian 的代码进行比较:

from delorean import Delorean

EST = "US/Eastern"

d = Delorean(timezone=EST)

I believe datetime 示例应写为:

from datetime import datetime
import pytz

eastern_timezone = pytz.timezone("US/Eastern")
d = datetime.now(eastern_timezone)

这样更简洁。

是否存在最后一个代码示例失败而第一个代码示例继续工作的情况?


更新: the current example:

from datetime import datetime
import pytz

d = datetime.utcnow()
d = pytz.utc.localize(d)

est = pytz.timezone('US/Eastern')
d = est.normalize(d)
return d

还是太啰嗦了。

问题剧照:do you need the explicit round-trip via utc and tz.normalize() or can you use datetime.now(tz) instead?

最佳答案

When does datetime.now(pytz_timezone) fail?

据我所知,在任何情况下它都不会失败。 datetime.now 在参数中传递的 tzinfo 实例上调用 fromutc 函数。所有从 UTC 到本地时间的转换都是明确的,因此没有失败的机会。

此外,原始代码甚至不起作用。

d = est.normalize(EST)

这似乎将字符串作为唯一参数传递给 normalize,它旨在采用 datetime。这给出:

AttributeError: 'str' object has no attribute 'tzinfo'

我相信他们打算写:

d = est.normalize(d.astimezone(est))

也就是说,我不认为他们代码的冗长会增加多少值(value)。如您所述,只需一步即可轻松完成此操作:

d = datetime.now(est)

查看 cpython source code for datetime.now ,我可以看到,当提供 tzinfo 对象时,它会调用该对象的 fromutc 方法。

if (self != NULL && tz != Py_None) {
/* Convert UTC to tzinfo's zone. */
PyObject *temp = self;

self = _PyObject_CallMethodId(tz, &PyId_fromutc, "O", self);
Py_DECREF(temp);
}

然后,在 pytz 源代码中,我看到 fromutc 方法的实现方式不同,具体取决于区域是 pytz.UTC 还是 的实例StaticTzInfoDstTzInfo。在所有这三种情况下,从输入 UTC 值到目标时区的转换都是明确的。下面是 DstTzInfo 实现,它是三者中更复杂的一个:

def fromutc(self, dt):
'''See datetime.tzinfo.fromutc'''
if (dt.tzinfo is not None
and getattr(dt.tzinfo, '_tzinfos', None) is not self._tzinfos):
raise ValueError('fromutc: dt.tzinfo is not self')
dt = dt.replace(tzinfo=None)
idx = max(0, bisect_right(self._utc_transition_times, dt) - 1)
inf = self._transition_info[idx]
return (dt + inf[0]).replace(tzinfo=self._tzinfos[inf])

这似乎是从时区的 _utc_transition_times 中找到转换,然后将其应用于返回的 datetime。这个方向没有歧义,所以结果是等价的。

同样值得注意的是,在 the datetime docs 中它说 datetime.now 相当于调用:

tz.fromutc(datetime.utcnow().replace(tzinfo=tz))

考虑到我之前展示的 pytz 中 fromutc 的来源,我不确定这与以下内容有什么不同:

tz.fromutc(datetime.utcnow())

但无论哪种情况,我都不认为 localizenormalize 是必要的。

关于python - `datetime.now(pytz_timezone)` 什么时候失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31886808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com