Timedelta multiply with float in python

2020-04-02 01:31发布

问题:

I have two dates and can calculate timedelta as usual.

But I want to calculate some percent with resulting timedelta:

full_time = (100/percentage) * timdelta

But it seems that it can only multiplying with interegs.

How can I use float instead of int as multiplier?

Example:

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

If is used int(multiplier) — accuracy suffers.

回答1:

You can convert to total seconds and back again:

full_time = timedelta(seconds=multiplier * passed_time.total_seconds())

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)


回答2:

You could use total_seconds():

datetime.timedelta(seconds=datetime.timedelta(minutes=42).total_seconds() * 0.8)
# => datetime.timedelta(0, 2016)