mirror of https://github.com/snachodog/mybuddy.git
Add a short param to the duration_string for XhXmXs formatted durations.
This commit is contained in:
parent
463dd0c1d7
commit
1185addace
|
@ -126,7 +126,8 @@ class Timer(models.Model):
|
||||||
return 'Timer ({})'.format(self.name)
|
return 'Timer ({})'.format(self.name)
|
||||||
|
|
||||||
def duration(self):
|
def duration(self):
|
||||||
return duration_string(self.start, self.end or timezone.now())
|
return duration_string(self.start, self.end or timezone.now(),
|
||||||
|
short=True)
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
self.active = self.end is None
|
self.active = self.end is None
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
{% for timer in timers %}
|
{% for timer in timers %}
|
||||||
{{ timer.name }} ({{ timer.duration }})
|
{{ timer.name }} ({{ timer.duration }})<br/>
|
||||||
{% endfor %}
|
{% endfor %}
|
|
@ -4,21 +4,24 @@ from __future__ import unicode_literals
|
||||||
from math import floor
|
from math import floor
|
||||||
|
|
||||||
|
|
||||||
def duration_string(start, end):
|
def duration_string(start, end, short=False):
|
||||||
diff = end - start
|
diff = end - start
|
||||||
h = floor(diff.seconds / 3600)
|
h = floor(diff.seconds / 3600)
|
||||||
m = floor((diff.seconds - h * 3600) / 60)
|
m = floor((diff.seconds - h * 3600) / 60)
|
||||||
s = diff.seconds % 60
|
s = diff.seconds % 60
|
||||||
|
|
||||||
duration = ''
|
duration = ''
|
||||||
if h > 0:
|
if short:
|
||||||
duration = '{} hour{}'.format(h, 's' if h > 1 else '')
|
duration = '{}h {}m {}s'.format(h, m, s)
|
||||||
if m > 0:
|
else:
|
||||||
duration += '{}{} minute{}'.format(
|
if h > 0:
|
||||||
'' if duration is '' else ', ', m, 's' if m > 1 else '')
|
duration = '{} hour{}'.format(h, 's' if h > 1 else '')
|
||||||
if s > 0:
|
if m > 0:
|
||||||
duration += '{}{} second{}'.format(
|
duration += '{}{} minute{}'.format(
|
||||||
'' if duration is '' else ', ', s, 's' if s > 1 else '')
|
'' 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
|
return duration
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue