嗨,我需要一些帮助来了解为什么会这样。我有一种方法可以在事件程序中跟踪“剩余时间”:
def get_program_time_budget(self):
return self.estimated_duration-self.get_program_duration()
一切都很好,estimated_duration > self.get_program_duration()
但是当事情发生相反的情况时,事情就变得有趣了。
结果显示给用户:
Estimated 11 hours Allocated 10 hours 55 minutes Remaining 5 minutes
当结果为负时,它会这样做:
Estimated 11 hours Allocated 11 hours 5 minutes Remaining -1 day 23 hours 55 minutes
任何想法如何获得结果 -5 分钟?
这是timedelta
格式化程序(注意这是一个 Django 过滤器,因此接收timedelta
值作为 a str
- 但它存储为 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