【发布时间】:2013-11-10 09:41:16
【问题描述】:
这是怎么回事?
>>> a = datetime.datetime.now()
# waiting....
>>> b = datetime.datetime.now()
>>> c = b - a
>>> c.seconds
4
>>> c.microseconds
884704
微秒怎么可能是秒数的 2 倍?我想要微秒的精度(然后自己将其转换为秒),但这似乎完全错误。
【问题讨论】:
这是怎么回事?
>>> a = datetime.datetime.now()
# waiting....
>>> b = datetime.datetime.now()
>>> c = b - a
>>> c.seconds
4
>>> c.microseconds
884704
微秒怎么可能是秒数的 2 倍?我想要微秒的精度(然后自己将其转换为秒),但这似乎完全错误。
【问题讨论】:
884704 微秒表示0.884704 秒。
>>> c = datetime.timedelta(seconds=4, microseconds=884704)
>>> c.seconds
4
>>> c.microseconds
884704
>>> print(c)
0:00:04.884704
要获取总秒数,您可以使用total_seconds():
>>> c.total_seconds()
4.884704
【讨论】:
total_seconds() 方法。我添加了那个。
total_seconds() 也考虑到天数。