Commits

Matthew Schinckel committed d9b4f29

Added support for an SQL format.

  • Participants
  • Parent commits 02be996

Comments (0)

Files changed (1)

File timedelta/helpers.py

 import datetime
 from decimal import Decimal
 
-def nice_repr(timedelta, display="long"):
+def nice_repr(timedelta, display="long", sep=" "):
     """
     Turns a datetime.timedelta object into a nice string repr.
     
     
     assert isinstance(timedelta, datetime.timedelta), "First argument must be a timedelta."
     
-    result = ""
+    result = []
     
     weeks = timedelta.days / 7
     days = timedelta.days % 7
     minutes = (timedelta.seconds % 3600) / 60
     seconds = timedelta.seconds % 60
     
-    if display == 'minimal':
+    if display == "sql":
+        days += weeks * 7
+        return "%i %02i:%02i:%02i" % (days, hours, minutes, seconds)
+    elif display == 'minimal':
         words = ["w", "d", "h", "m", "s"]
     elif display == 'short':
         words = [" wks", " days", " hrs", " min", " sec"]
     for i in range(len(values)):
         if values[i]:
             if values[i] == 1 and len(words[i]) > 1:
-                result += "%i%s, " % (values[i], words[i].rstrip('s'))
+                result.append("%i%s" % (values[i], words[i].rstrip('s')))
             else:
-                result += "%i%s, " % (values[i], words[i])
+                result.append("%i%s" % (values[i], words[i]))
     
-    return result[:-2]
+    return sep.join(result)
 
 def parse(string):
     """