views:

58

answers:

2

Hello

Assume I have these datatime variables:

start_time, end_time, current_time

I would like to know how much time left as percentage by checking current_time and the time delta between start_time and the end_time

IE: Assume the interval is a 24 hours betwen start_time and end_time yet between current_time and end_time, there are 6 hours left to finish, %25 should be left.

How can this be done ?

+2  A: 

Here's a hackish workaround: compute the total number of microseconds between the two values by using the days, seconds, and microseconds fields. Then divide by the total number of microseconds in the interval.

avpx
A: 

Possibly simplest:

import time

def t(dt):
  return time.mktime(dt.timetuple())

def percent(start_time, end_time, current_time):
  total = t(end_time) - t(start_time)
  current = t(current_time) - t(start_time)
  return (100.0 * current) / total
Alex Martelli