PascalVKooten
PascalVKooten

Reputation: 21443

Python datetime unexplained difference between seconds and microseconds

What is going on here?

>>> a = datetime.datetime.now()
# waiting....
>>> b = datetime.datetime.now()
>>> c = b - a
>>> c.seconds
4
>>> c.microseconds
884704

How can the microseconds be 2 times more than the amount in seconds? I'd like the precision of microseconds (and then convert it to seconds myself), but this seems simply wrong.

Upvotes: 2

Views: 335

Answers (1)

falsetru
falsetru

Reputation: 369074

884704 microseconds means 0.884704 seconds.

>>> c = datetime.timedelta(seconds=4, microseconds=884704)
>>> c.seconds
4
>>> c.microseconds
884704
>>> print(c)
0:00:04.884704

To get total seconds, you can use total_seconds():

>>> c.total_seconds()
4.884704

Upvotes: 6

Related Questions