Simplify duration string function.

This commit is contained in:
Christopher Charbonneau Wells 2017-08-13 15:27:32 -04:00
parent 9422d73421
commit c5c3c4b030
2 changed files with 14 additions and 21 deletions

View File

@ -6,25 +6,18 @@ from math import floor
def duration_string(start, end):
diff = end - start
if diff.seconds < 60:
duration = '{} second{}'.format(
diff.seconds,
's' if diff.seconds > 1 else ''
)
elif diff.seconds < 3600:
duration = '{} minute{}, {} second{}'.format(
floor(diff.seconds / 60),
's' if floor(diff.seconds / 60) > 1 else '',
diff.seconds % 60,
's' if diff.seconds % 60 > 1 else ''
)
else:
duration = '{} hour{}, {} minute{}, {} second{}'.format(
floor(diff.seconds / 3600),
's' if floor(diff.seconds / 3600) > 1 else '',
floor((diff.seconds - 3600) / 60),
's' if floor((diff.seconds - 3600) / 60) > 1 else '',
diff.seconds % 60,
's' if diff.seconds % 60 > 1 else ''
)
h = floor(diff.seconds / 3600)
m = floor((diff.seconds - h * 3600) / 60)
s = diff.seconds % 60
duration = ''
if h > 0:
duration = '{} hour{}'.format(h, 's' if h > 1 else '')
if m > 0:
duration += '{}{} minute{}'.format(
'' if duration is '' else ', ', m, 's' if m > 1 else '')
if s > 0:
duration += '{}{} second{}'.format(
'' if duration is '' else ', ', s, 's' if s > 1 else '')
return duration

Binary file not shown.