Python datetime无法解释的秒与微秒之间的差异

Pas*_*ten 2 python datetime timedelta

这里发生了什么?

>>> a = datetime.datetime.now()
# waiting....
>>> b = datetime.datetime.now()
>>> c = b - a
>>> c.seconds
4
>>> c.microseconds
884704
Run Code Online (Sandbox Code Playgroud)

微秒怎么能比秒数多2倍?我想要微秒的精度(然后自己将其转换为秒),但这似乎是错误的.

fal*_*tru 6

884704微秒意味着0.884704秒.

>>> c = datetime.timedelta(seconds=4, microseconds=884704)
>>> c.seconds
4
>>> c.microseconds
884704
>>> print(c)
0:00:04.884704
Run Code Online (Sandbox Code Playgroud)

要获得总秒数,您可以使用total_seconds():

>>> c.total_seconds()
4.884704
Run Code Online (Sandbox Code Playgroud)