Python 2.6.5:将 timedelta 除以 timedelta
我正在尝试将一个 timedelta
对象与另一个对象分开来计算服务器正常运行时间:
>>> 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'
我知道这个 在Python 3.2中已经解决了,但是在Python的早期版本中,除了计算微秒数、秒数和天数以及除法之外,是否有方便的方法来处理它?
谢谢,
亚当
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在Python ≥2.7中,有一个
.total_seconds() 方法
来计算 timedelta 中包含的总秒数:
否则,只能计算总微秒数(对于版本 <2.7)
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)