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.
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.
You can use greater than operator > to check if one datetime object is greater than other.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With