- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
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)
这样更简洁。
是否存在最后一个代码示例失败而第一个代码示例继续工作的情况?
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
还是太啰嗦了。
最佳答案
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
还是 的实例StaticTzInfo
或 DstTzInfo
。在所有这三种情况下,从输入 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())
但无论哪种情况,我都不认为 localize
和 normalize
是必要的。
关于python - `datetime.now(pytz_timezone)` 什么时候失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31886808/
我正在尝试使用以下方法对 datetime.datetime 对象列表求和: from datetime import datetime, timedelta d= [datetime.datetim
我正在尝试这个 (datetime.datetime.today()-datetime.datetime.today()).days 给出 -1 并期待值 0 而不是我得到 -1。在这种情况下,我将结
如果我列一个时间增量的列表,平均值比我对这些增量的微秒值求平均时要大。为什么会这样呢?。赠送。这是Linux上的Python3.8.10。
考虑以下片段: import datetime print(datetime.datetime.now() - datetime.datetime.now()) 在 x86_64 Linux 下的 P
如何在 SQLAlchemy 查询中比较 DateTime 字段和 datetime.datetime 对象? 例如,如果我这样做 candidates = session.query(User).f
我收到以下错误: type object 'datetime.datetime' has no attribute 'datetime' 在下面一行: date = datetime.datetime
尝试找出如何将当前日期锁定为变量,以从输入的 self.birthday 中减去。我已经查看了各种示例和链接,但无济于事......建议? from datetime import datetime
您好,我有一些 datetime.datetime 格式的日期,我用它们来过滤带有 Pandas 时间戳的 Pandas 数据框。我刚刚尝试了以下方法并获得了 2 小时的偏移量: from datet
如果您调用 datetime.datetime.now(datetime.timezone.utc) 您会得到类似 datetime.datetime(2021, 9, 8, 1, 33, 19, 6
我正在使用 pywin32 读取/写入 Excel 文件。我在 Excel 中有一些日期,以 yyyy-mm-dd hh:mm:ss 格式存储。我想将它们作为 datetime.datetime 对象
据我所知,自 Unix 纪元(1970-01-01 00:00:00 UTC)以来的秒数在全局各地应该是相同的,因为它固定为 UTC。 现在,如果您所在的时区有几个小时 +/- UTC,为什么这样做会
我正在尝试添加 datetime.datetime 和 datetime.time 以获得一列。我正在尝试结合: import datetime as dt dt.datetime.combine(m
我有一个脚本需要在脚本的不同行执行以下操作: today_date = datetime.date.today() date_time = datetime.strp(date_time_string
我在 AppEngine 上收到 type object 'datetime.datetime' has no attribute 'datetime' 错误,提示日期时间类型,但我的导入是 impo
所以我一直在使用 python 语言制作东西。我遇到了一些不太容易理解的错误: TypeError: 'datetime.datetime' object is not subscriptable (
当我运行时 from datetime import date, time, timedelta date(2012, 11, 1) + timedelta(0, 3600) 结果是 datetime
我的目标是转换 utc进入loc : use chrono::{Local, UTC, TimeZone}; let utc = chrono::UTC::now(); let loc = chron
假设您有一个 datetime.date 对象,例如 datetime.date.today() 返回的对象。 稍后您还会得到一个表示时间的字符串,它补充了日期对象。 在 datetime.datet
我试过了 In [16]: import datetime In [17]: now = datetime.datetime.utcnow() In [18]: isinstance(now, dat
我有以下代码并且收到上述错误。由于我是 python 新手,因此无法理解此处的语法以及如何修复错误: if not start or date < start: start = date 最佳答案 有
我是一名优秀的程序员,十分优秀!