Skip to content

Commit

Permalink
Formatte l'heure avec unité de temps 00h00m00s (#5261)
Browse files Browse the repository at this point in the history
* Formatte l'heure avec unité de temps 00h00m00s

* Pas d'heure si on ne dépasse pas la limite
  • Loading branch information
A-312 authored and artragis committed Feb 2, 2019
1 parent 65cfdb8 commit dc99188
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion zds/utils/templatetags/seconds_to_duration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
register = template.Library()


# https://stackoverflow.com/a/8907269/2226755
def strfdelta(tdelta, fmt):
d = {'days': tdelta.days}
d['hours'], rem = divmod(tdelta.seconds, 3600)
d['minutes'], d['seconds'] = divmod(rem, 60)
return fmt.format(**d)


# TODO add unit test
@register.filter('seconds_to_duration')
def seconds_to_duration(value):
Expand All @@ -15,4 +23,7 @@ def seconds_to_duration(value):
return ''

duration = datetime.timedelta(seconds=value)
return str(duration)
if duration < 3600
return strfdelta(duration, '{minutes}m{seconds}s')
else
return strfdelta(duration, '{hours}h{minutes}m{seconds}s')

0 comments on commit dc99188

Please sign in to comment.