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): def duration_string(start, end):
diff = end - start diff = end - start
if diff.seconds < 60: h = floor(diff.seconds / 3600)
duration = '{} second{}'.format( m = floor((diff.seconds - h * 3600) / 60)
diff.seconds, s = diff.seconds % 60
's' if diff.seconds > 1 else ''
) duration = ''
elif diff.seconds < 3600: if h > 0:
duration = '{} minute{}, {} second{}'.format( duration = '{} hour{}'.format(h, 's' if h > 1 else '')
floor(diff.seconds / 60), if m > 0:
's' if floor(diff.seconds / 60) > 1 else '', duration += '{}{} minute{}'.format(
diff.seconds % 60, '' if duration is '' else ', ', m, 's' if m > 1 else '')
's' if diff.seconds % 60 > 1 else '' if s > 0:
) duration += '{}{} second{}'.format(
else: '' if duration is '' else ', ', s, 's' if s > 1 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 ''
)
return duration return duration

Binary file not shown.