Кра*_*йст 20 python datetime multiplying timedelta
我有两个日期,可以照常计算timedelta.
但是我想用结果timedelta计算一些百分比:
full_time = (100/percentage) * timdelta
Run Code Online (Sandbox Code Playgroud)
但它似乎只能与互联网相乘.
我如何使用float而不是int乘数?
例:
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
Run Code Online (Sandbox Code Playgroud)
如果使用int(multiplier)- 准确性受损.
eca*_*mur 27
您可以转换为总秒数,然后再转回:
full_time = timedelta(seconds=multiplier * passed_time.total_seconds())
Run Code Online (Sandbox Code Playgroud)
timedelta.total_seconds可以从Python 2.7获得; 在早期版本中使用
def timedelta_total_seconds(td):
return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / float(10**6)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7627 次 |
| 最近记录: |