python - 在 python 中使用高精度时间戳

标签 python datetime time

嘿,我在 python 中使用 datetime 工作,我想知道解析这个时间戳的最佳方法是什么。

时间戳是 ISO 标准,这里是一个例子“2010-06-19T08:17:14.078685237Z”

到目前为止我用过

time = datetime.datetime.strptime(timestamp.split(".")[0], "%Y-%m-%dT%H:%M:%S")
precisetime = time + datetime.timedelta(0,float("." + timestamp[:-1].split(".")[0]))

这种方法可行,但我觉得应该有一种更精简的方法(我是 python 的新手,我确信我这样做很蠢)。另外,我的时间戳中有纳秒,但我的日期时间对象中只有微秒,是否有更好的模块可以使用?我需要能够对时间进行操作,例如减去时间并将它们放入调度程序中。

有什么更好的办法吗?

最佳答案

您可以使用 Numpy 的 datetime64:http://docs.scipy.org/doc/numpy-dev/reference/arrays.datetime.html

它支持纳秒级和更高的精度。

>>> import numpy as np
>>> np.version.version
'1.7.1'
>>> np.datetime64("2010-06-19T08:17:14.078685237Z", dtype="datetime64[ns]")
numpy.datetime64('2010-06-19T08:17:14.078685237+0000')

关于python - 在 python 中使用高精度时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6207365/

相关文章:

python - 如何在pyqt5中对按钮点击使用react

python - 在Python中将UTC转换为另一个时区的简单方法

c# - 从 GMT 到本地的时区转换

c - Linux 中的 SystemTimeToFileTime 精确替换

c++ - 对象内的定时器

python : how to disable auto sort when creating dictionary

python根据文件名中的文本字符将多个文件从一个文件夹移动到另一个文件夹

python - 来自数据库的加权边列表的图形

powershell - 获取格式化的通用日期/时间

c++ - 在 C/C++ 中将月/日/年/时间转换为时间值