mirror of https://github.com/snachodog/mybuddy.git
Move duration methods in to utils for use elsewhere in the project.
This commit is contained in:
parent
1dbdc582dd
commit
9d0572ba92
|
@ -1,54 +1,56 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from datetime import timedelta
|
|
||||||
|
|
||||||
from django import template
|
from django import template
|
||||||
|
|
||||||
|
from core.utils import duration_parts, duration_string as d_string
|
||||||
|
|
||||||
|
|
||||||
register = template.Library()
|
register = template.Library()
|
||||||
|
|
||||||
|
|
||||||
@register.filter
|
@register.filter
|
||||||
def duration_string(duration):
|
def duration_string(duration):
|
||||||
h, m, s = _get_hms(duration)
|
"""Format a duration (e.g. "2 hours, 3 minutes, 35 seconds")."""
|
||||||
|
if not duration:
|
||||||
duration = ''
|
return ''
|
||||||
if h > 0:
|
try:
|
||||||
duration = '{} hour{}'.format(h, 's' if h > 1 else '')
|
return d_string(duration)
|
||||||
if m > 0:
|
except (ValueError, TypeError):
|
||||||
duration += '{}{} minute{}'.format(
|
return ''
|
||||||
'' 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
|
|
||||||
|
|
||||||
|
|
||||||
@register.filter
|
@register.filter
|
||||||
def hours(duration):
|
def hours(duration):
|
||||||
h, m, s = _get_hms(duration)
|
"""Return "hours" portion of a duration."""
|
||||||
return h
|
if not duration:
|
||||||
|
return 0
|
||||||
|
try:
|
||||||
|
h, m, s = duration_parts(duration)
|
||||||
|
return h
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
@register.filter
|
@register.filter
|
||||||
def minutes(duration):
|
def minutes(duration):
|
||||||
h, m, s = _get_hms(duration)
|
"""Return "minutes" portion of a duration."""
|
||||||
return m
|
if not duration:
|
||||||
|
return 0
|
||||||
|
try:
|
||||||
|
h, m, s = duration_parts(duration)
|
||||||
|
return m
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
@register.filter
|
@register.filter
|
||||||
def seconds(duration):
|
def seconds(duration):
|
||||||
h, m, s = _get_hms(duration)
|
"""Return "seconds" portion of a duration."""
|
||||||
return s
|
if not duration:
|
||||||
|
return 0
|
||||||
|
try:
|
||||||
def _get_hms(duration):
|
h, m, s = duration_parts(duration)
|
||||||
"""Get hours, minutes and seconds from a timedelta."""
|
return s
|
||||||
if not isinstance(duration, timedelta):
|
except (ValueError, TypeError):
|
||||||
return 0, 0, 0
|
return 0
|
||||||
h, remainder = divmod(duration.seconds, 3600)
|
|
||||||
h += duration.days * 24
|
|
||||||
m, s = divmod(remainder, 60)
|
|
||||||
return h, m, s
|
|
||||||
|
|
|
@ -23,3 +23,31 @@ def timer_stop(timer_id, end=None):
|
||||||
timer_instance = Timer.objects.get(id=timer_id)
|
timer_instance = Timer.objects.get(id=timer_id)
|
||||||
timer_instance.end = end
|
timer_instance.end = end
|
||||||
timer_instance.save()
|
timer_instance.save()
|
||||||
|
|
||||||
|
|
||||||
|
def duration_string(duration):
|
||||||
|
"""Format hours, minutes and seconds in a human-friendly way (e.g. "2
|
||||||
|
hours, 25 minutes, 31 seconds")"""
|
||||||
|
h, m, s = duration_parts(duration)
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
def duration_parts(duration):
|
||||||
|
"""Get hours, minutes and seconds from a timedelta."""
|
||||||
|
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)
|
||||||
|
return h, m, s
|
Loading…
Reference in New Issue