mcragun
mcragun

Reputation: 1218

Python time objects with more than 24 hours

I have a time out of Linux that is in hh:mm:sec, but the hh can be greater than 24 hours. So if the time is 1 day 12 hours, it would be 36:00:00. Is there a way to take this format and easily make a time object?

What I would really like to do is take the the required time i.e. 36:00:00, and the time that it has been running 4:46:23, and subtract the two to get the time remaining. I figured time delta might be the most convenient way to do this in Python, but I'd also be open to other suggestions.

Thanks.

Upvotes: 13

Views: 16216

Answers (2)

FogleBird
FogleBird

Reputation: 76792

timedelta is indeed what you want. Here is a more complete example that does what you asked.

>>> import datetime
>>> a = datetime.timedelta(hours=36)
>>> b = datetime.timedelta(hours=4, minutes=46, seconds=23)
>>> c = a - b
>>> print c
1 day, 7:13:37

Upvotes: 17

number5
number5

Reputation: 16443

What you need is the timedelta object: http://docs.python.org/library/datetime.html#timedelta-objects

36 hours:

d = timedelta(hours=36)

Upvotes: 10

Related Questions