Ensure totals are shown in hours:minutes:seconds when they are over 24 hours default tip
authorBorja Lopez <borja@codigo23.net>
Mon, 24 Feb 2020 08:27:01 +0100
changeset 75 957d4b9c06eb
parent 74 2290b564b51e
Ensure totals are shown in hours:minutes:seconds when they are over 24 hours
stamper/stamper.py
--- a/stamper/stamper.py	Thu Sep 26 16:23:31 2019 +0200
+++ b/stamper/stamper.py	Mon Feb 24 08:27:01 2020 +0100
@@ -12,6 +12,15 @@
 from .filters import DateFilter
 
 
+def timedelta_to_hms(value):
+    """
+    Return hours, minutes, seconds from a timedelta object
+    """
+    hours, remainder = divmod(int(value.total_seconds()), 3600)
+    minutes, seconds = divmod(remainder, 60)
+    return hours, minutes, seconds
+
+
 class Stamper(object):
 
     def __init__(self, config_file=None):
@@ -298,6 +307,9 @@
         if customer:
             seconds=totals.get(customer, 0)
             total = timedelta(seconds=totals.get(customer, 0))
+            h, m, s = timedelta_to_hms(total)
+            total = ':'.join(
+                [str(h).zfill(2), str(m).zfill(2), str(s).zfill(2)])
             if not self.wants_seconds or self.wants_seconds == 'false':
                 # remove the seconds part from the string representation
                 total = ':'.join(str(total).split(':')[:-1])
@@ -307,6 +319,9 @@
             for c in totals:
                 seconds=totals[c]
                 total = timedelta(seconds=totals[c])
+                h, m, s = timedelta_to_hms(total)
+                total = ':'.join(
+                    [str(h).zfill(2), str(m).zfill(2), str(s).zfill(2)])
                 if not self.wants_seconds or self.wants_seconds == 'false':
                     # remove the seconds part from the string representation
                     total = ':'.join(str(total).split(':')[:-1])