uilib.py
changeset 7913 d0c6a7993cec
parent 7879 9aae456abab5
child 7914 fb757a7d887e
--- a/uilib.py	Tue Oct 04 18:45:38 2011 +0200
+++ b/uilib.py	Thu Oct 06 16:14:08 2011 +0200
@@ -79,6 +79,39 @@
         return ustrftime(value, req.property_value('ui.datetime-format')) + u' UTC'
     return ustrftime(value, req.property_value('ui.date-format'))
 
+_('%d years')
+_('%d months')
+_('%d weeks')
+_('%d days')
+_('%d hours')
+_('%d minutes')
+_('%d seconds')
+
+def print_timedelta(value, req, props, displaytime=True):
+    if isinstance(value, (int, long)):
+        # `date - date`, unlike `datetime - datetime` gives an int
+        # (number of days), not a timedelta
+        # XXX should rql be fixed to return Int instead of Interval in
+        #     that case? that would be probably the proper fix but we
+        #     loose information on the way...
+        value = timedelta(days=value)
+    if value.days > 730 or value.days < -730: # 2 years
+        return req._('%d years') % (value.days // 365)
+    elif value.days > 60 or value.days < -60: # 2 months
+        return req._('%d months') % (value.days // 30)
+    elif value.days > 14 or value.days < -14: # 2 weeks
+        return req._('%d weeks') % (value.days // 7)
+    elif value.days > 2 or value.days < -2:
+        return req._('%d days') % int(value.days)
+    else:
+        minus = 1 if value.days > 0 else -1
+        if value.seconds > 3600:
+            return req._('%d hours') % (int(value.seconds // 3600) * minus)
+        elif value.seconds >= 120:
+            return req._('%d minutes') % (int(value.seconds // 60) * minus)
+        else:
+            return req._('%d seconds') % (int(value.seconds) * minus)
+
 def print_boolean(value, req, props, displaytime=True):
     if value:
         return req._('yes')
@@ -98,7 +131,7 @@
     'Boolean': print_boolean,
     'Float': print_float,
     'Decimal': print_float,
-    # XXX Interval
+    'Interval': print_timedelta,
     }
 
 def printable_value(req, attrtype, value, props=None, displaytime=True):