# HG changeset patch # User Aras Pranckevicius # Date 2015-01-04 13:03:23 # Node ID 2982360d25476a9d6a9b6781d2fd6a4c6dc84e38 # Parent e7812c9f606240d29b6e349e27cba04105b0b570 utils: better display of ages >1 year when in short form * 1..2 years: display as months instead * 2+ years: round diff --git a/kallithea/lib/utils2.py b/kallithea/lib/utils2.py --- a/kallithea/lib/utils2.py +++ b/kallithea/lib/utils2.py @@ -408,6 +408,18 @@ def age(prevdate, show_short_version=Fal 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, diff --git a/kallithea/tests/other/test_libs.py b/kallithea/tests/other/test_libs.py --- a/kallithea/tests/other/test_libs.py +++ b/kallithea/tests/other/test_libs.py @@ -150,11 +150,12 @@ class TestLibs(BaseTestCase): (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