I’m trying to divide one timedelta object with another to calculate a server uptime:
>>> import datetime
>>> installation_date=datetime.datetime(2010,8,01)
>>> down_time=datetime.timedelta(seconds=1400)
>>> server_life_period=datetime.datetime.now()-installation_date
>>> down_time_percentage=down_time/server_life_period
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'datetime.timedelta'
and 'datetime.timedelta'
I know this has been solved in Python 3.2, but is there a convenient way to handle it in prior versions of Python, apart from calculating the number of microseconds, seconds and days and dividing?
Thanks,
Adam
In Python ≥2.7, there is a
.total_seconds()method to compute the total seconds contained in the timedelta:Otherwise, there is no way but to compute the total microseconds (for versions < 2.7)