Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 339
mcragun Avatar asked May 27 '10 16:05

mcragun


People also ask

How to calculate the time difference between two dates in python?

Use the strptime(date_str, format) function to convert a date string into a datetime object as per the corresponding format . To get the difference between two dates, subtract date2 from date1. A result is a timedelta object.

How do you know if time is greater than in Python?

You can use greater than operator > to check if one datetime object is greater than other.


1 Answers

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
like image 136
FogleBird Avatar answered Sep 22 '22 01:09

FogleBird