Use mark_safe as decorator where possible
[mygpo.git] / mygpo / web / templatetags / time.py
blob760f09ef9dd80123798711eb9e119329eedfa999
1 from datetime import time
3 from django.utils.safestring import mark_safe
4 from django.utils.translation import ugettext as _
5 from django import template
8 register = template.Library()
10 @register.filter
11 def sec_to_time(sec):
12 """ Converts seconds to a time object
14 >>> t = sec_to_time(1000)
15 >>> (t.hour, t.minute, t.second)
16 (0, 16, 40)
17 """
19 s = int(sec)
20 hour = int(s / 60 / 60)
21 minute = int((s / 60) % 60)
22 sec = int(s % 60 )
23 return time(hour, minute, sec)
26 @register.filter
27 @mark_safe
28 def format_duration(sec):
29 """ Converts seconds into a duration string
31 >>> format_duration(1000)
32 '0h 16m 40s'
34 """
35 hours = int(sec / 60 / 60)
36 minutes = int((sec / 60) % 60)
37 seconds = int(sec % 60)
38 return _('{h}h {m}m {s}s').format(h=hours, m=minutes, s=seconds)