You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

29 lines
793 B

class Unit(object):
def __init__(self, name, nsecs):
self.name = name
self.plural = name + "s"
self.nsecs = nsecs
MINUTE = Unit("minute", 60)
HOUR = Unit("hour", MINUTE.nsecs * 60)
DAY = Unit("day", HOUR.nsecs * 24)
WEEK = Unit("week", DAY.nsecs * 7)
def format_duration(td):
remaining_seconds = int(td.total_seconds())
result = []
for unit in (WEEK, DAY, HOUR, MINUTE):
if unit == WEEK and remaining_seconds % unit.nsecs != 0:
# Say "8 days" instead of "1 week 1 day"
continue
v, remaining_seconds = divmod(remaining_seconds, unit.nsecs)
if v == 1:
result.append("1 %s" % unit.name)
elif v > 1:
result.append("%d %s" % (v, unit.plural))
return " ".join(result)