From dc99188e3d9746adb9006950acbf19302df6bf0c Mon Sep 17 00:00:00 2001 From: A-312 Date: Sat, 2 Feb 2019 19:44:06 +0100 Subject: [PATCH] =?UTF-8?q?Formatte=20l'heure=20avec=20unit=C3=A9=20de=20t?= =?UTF-8?q?emps=2000h00m00s=20(#5261)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Formatte l'heure avec unité de temps 00h00m00s * Pas d'heure si on ne dépasse pas la limite --- zds/utils/templatetags/seconds_to_duration.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/zds/utils/templatetags/seconds_to_duration.py b/zds/utils/templatetags/seconds_to_duration.py index 58ca7eabc8..dd0ec71e4a 100644 --- a/zds/utils/templatetags/seconds_to_duration.py +++ b/zds/utils/templatetags/seconds_to_duration.py @@ -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): @@ -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')