changeset 4757:2982360d2547

utils: better display of ages >1 year when in short form * 1..2 years: display as months instead * 2+ years: round
author Aras Pranckevicius <aras@unity3d.com>
date Sun, 04 Jan 2015 14:03:23 +0200
parents e7812c9f6062
children 5f0e40fad7da
files kallithea/lib/utils2.py kallithea/tests/other/test_libs.py
diffstat 2 files changed, 16 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/lib/utils2.py	Sun Jan 04 13:44:51 2015 +0200
+++ b/kallithea/lib/utils2.py	Sun Jan 04 14:03:23 2015 +0200
@@ -408,6 +408,18 @@
         deltas['month'] += 12
         deltas['year'] -= 1
 
+    # In short version, we want nicer handling of ages of more than a year
+    if show_short_version:
+        if deltas['year'] == 1:
+            # ages between 1 and 2 years: show as months
+            deltas['month'] += 12
+            deltas['year'] = 0
+        if deltas['year'] >= 2:
+            # ages 2+ years: round
+            if deltas['month'] > 6:
+                deltas['year'] += 1
+                deltas['month'] = 0
+
     # Format the result
     fmt_funcs = {
         'year': lambda d: ungettext(u'%d year', '%d years', d) % d,
--- a/kallithea/tests/other/test_libs.py	Sun Jan 04 13:44:51 2015 +0200
+++ b/kallithea/tests/other/test_libs.py	Sun Jan 04 14:03:23 2015 +0200
@@ -150,11 +150,12 @@
         (dict(months= -1), u'1 month ago'),
         (dict(months= -1, days= -2), u'1 month ago'),
         (dict(months= -1, days= -20), u'1 month ago'),
-        (dict(years= -1, months= -1), u'1 year ago'),
-        (dict(years= -1, months= -10), u'1 year ago'),
+        (dict(years= -1, months= -1), u'13 months ago'),
+        (dict(years= -1, months= -10), u'22 months ago'),
         (dict(years= -2, months= -4), u'2 years ago'),
-        (dict(years= -2, months= -11), u'2 years ago'),
+        (dict(years= -2, months= -11), u'3 years ago'),
         (dict(years= -3, months= -2), u'3 years ago'),
+        (dict(years= -4, months= -8), u'5 years ago'),
     ])
     def test_age_short(self, age_args, expected):
         from kallithea.lib.utils2 import age