在python中使用高精度时间戳 [英] working with high precision timestamps in python
问题描述
嘿,我正在使用带日期时间的python工作,我想知道解析此时间戳的最佳方法是什么.
Hey I am working in python with datetime and I am wondering what the best way to parse this timestamp is.
时间戳是ISO标准,下面是示例"2010-06-19T08:17:14.078685237Z"
The timestamps are ISO standard, here is an example "2010-06-19T08:17:14.078685237Z"
到目前为止,我已经使用过
Now so far I have used
time = datetime.datetime.strptime(timestamp.split(".")[0], "%Y-%m-%dT%H:%M:%S")
precisetime = time + datetime.timedelta(0,float("." + timestamp[:-1].split(".")[0]))
这种类型的作品,但是我觉得应该有一种更简化的方式(我对python还是很陌生,我敢肯定我会像个屁股那样做).另外,我的时间戳中有纳秒,但日期时间对象中只有微秒,是否有更好的模块可以使用?我需要能够对时间进行操作,例如减去时间并将其放入调度程序中.
This kind of works, but I feel like there should be a more streamlined way (I am very new to python, and I am sure I am doing this like an ass). Also, I have nanoseconds in my timestamp, but only microseconds in my datetime object, is there a better module to work with? I need to be able to do operations on the time such as subtracting times and putting them in the scheduler.
有更好的方法吗?
推荐答案
您的方法天生就没有驴子般的味道,但是您可能想尝试 dateutil
There's nothing inherently ass-like with your approach, but you may like to try pyiso8601 or dateutil
这篇关于在python中使用高精度时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!