Timedelta与python中的float相乘 [英] Timedelta multiply with float in python
本文介绍了Timedelta与python中的float相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个日期,可以像往常一样计算timedelta。
I have two dates and can calculate timedelta as usual.
但是我想计算得到的timedelta的百分比:
But I want to calculate some percent with resulting timedelta:
full_time = (100/percentage) * timdelta
但是看来它只能与interegs相乘。
But it seems that it can only multiplying with interegs.
如何使用 float
代替 int
作为乘数?
How can I use float
instead of int
as multiplier?
示例:
percentage = 43.27
passed_time = fromtimestamp(fileinfo.st_mtime) - fromtimestamp(fileinfo.st_ctime)
multiplier = 100 / percentage # 2.3110700254217702796394730760342
full_time = multiplier * passed_time # BUG: here comes exception
estimated_time = full_time - passed_time
如果使用了 int(乘数)
—准确性受到影响。
If is used int(multiplier)
— accuracy suffers.
推荐答案
您可以转换为总秒数,然后再次返回:
You can convert to total seconds and back again:
full_time = timedelta(seconds=multiplier * passed_time.total_seconds())
timedelta.total_seconds
可从Python 2.7获得;在早期版本上使用
timedelta.total_seconds
is available from Python 2.7; on earlier versions use
def timedelta_total_seconds(td):
return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / float(10**6)
这篇关于Timedelta与python中的float相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文