Hi I need some help to understand why this is happening.
I have a method to track 'time remaining' in an event program:
def get_program_time_budget(self):
return self.estimated_duration-self.get_program_duration()
All fine when the estimated_duration > self.get_program_duration() but when this goes the other way things get funny.
Results are displayed to the user:
Estimated 11 hours Allocated 10 hours 55 minutes Remaining 5 minutes
When the result goes negative it does this:
Estimated 11 hours Allocated 11 hours 5 minutes Remaining -1 day 23 hours 55 minutes
Any ideas how to get the result -5 minutes?
EDIT:
Here is the timedelta formatter (Note this is a Django filter, so receives the timedelta value as a str - but it is stored as a timedelta):
def format_duration(value):
try:
delim = ':'
toks = value.split(',')
hour = minute = ''
d_string = value.count('day') and toks[0] or ''
h, m, s = d_string and toks[-1].strip().split(delim) or value.split(delim)
try:
hour = int(h)
except:
pass
try:
minute = int(m)
except:
pass
h_string = "%s%s%s" % (hour and hour or '', (hour and ' hour' or ''),(hour and hour > 1 and 's' or '') )
m_string = "%s%s%s" % (minute and minute or '', (minute and ' minute' or ''),(minute and minute > 1 and 's' or ''))
return "%s %s %s" % (d_string, h_string, m_string)
except Exception, e:
logging.error("Error in format_duration -> %s. Duration value=%s" % (e, value))
return ''v
question from:
https://stackoverflow.com/questions/8408397/python-timedelta-issue-with-negative-values 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…