2017-08-13 15:59:14 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-08-18 05:53:48 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
|
2017-08-13 15:59:14 +00:00
|
|
|
|
2017-10-30 18:26:49 +00:00
|
|
|
def duration_string(duration, precision='s'):
|
2017-09-13 18:07:51 +00:00
|
|
|
"""Format hours, minutes and seconds as a human-friendly string (e.g. "2
|
2017-10-30 18:26:49 +00:00
|
|
|
hours, 25 minutes, 31 seconds") with precision to h = hours, m = minutes or
|
|
|
|
s = seconds.
|
|
|
|
"""
|
2017-08-25 20:33:14 +00:00
|
|
|
h, m, s = duration_parts(duration)
|
|
|
|
|
|
|
|
duration = ''
|
|
|
|
if h > 0:
|
|
|
|
duration = '{} hour{}'.format(h, 's' if h > 1 else '')
|
2017-10-30 18:26:49 +00:00
|
|
|
if m > 0 and precision != 'h':
|
2017-08-25 20:33:14 +00:00
|
|
|
duration += '{}{} minute{}'.format(
|
2019-02-03 03:17:25 +00:00
|
|
|
'' if duration == '' else ', ', m, 's' if m > 1 else '')
|
2017-10-30 18:26:49 +00:00
|
|
|
if s > 0 and precision != 'h' and precision != 'm':
|
2017-08-25 20:33:14 +00:00
|
|
|
duration += '{}{} second{}'.format(
|
2019-02-03 03:17:25 +00:00
|
|
|
'' if duration == '' else ', ', s, 's' if s > 1 else '')
|
2017-08-25 20:33:14 +00:00
|
|
|
|
|
|
|
return duration
|
|
|
|
|
|
|
|
|
|
|
|
def duration_parts(duration):
|
2017-10-30 18:26:49 +00:00
|
|
|
"""Get hours, minutes and seconds from a timedelta.
|
|
|
|
"""
|
2017-08-25 20:33:14 +00:00
|
|
|
if not isinstance(duration, timezone.timedelta):
|
|
|
|
raise TypeError('Duration provided must be a timedetla')
|
|
|
|
h, remainder = divmod(duration.seconds, 3600)
|
|
|
|
h += duration.days * 24
|
|
|
|
m, s = divmod(remainder, 60)
|
2017-09-25 15:39:08 +00:00
|
|
|
return h, m, s
|