moksha/lib/helpers.py | 24 +++++++++++++++++++++---
1 file changed, 21 insertions(+), 3 deletions(-)
New commits:
commit 92a0a6d70ba738defb1e57a2d51ad7ab02a2f42a
Merge: 1b2c9f1... c8c5b99...
Author: Luke Macken <lmacken(a)redhat.com>
Date: Fri May 22 00:04:44 2009 -0400
Merge branch 'master' of
git+ssh://git.fedorahosted.org/git/moksha
commit 1b2c9f12534506e3fc252674f20d5e455c82d53f
Author: Luke Macken <lmacken(a)redhat.com>
Date: Fri May 22 00:03:29 2009 -0400
Add a 'general' argument to the DateTimeDisplay.age method.
This will only return the most significant granularity of time.
This solves the problems mentioned in:
https://fedorahosted.org/fedoracommunity/ticket/181
diff --git a/moksha/lib/helpers.py b/moksha/lib/helpers.py
index c1dfe72..a67bc60 100644
--- a/moksha/lib/helpers.py
+++ b/moksha/lib/helpers.py
@@ -964,18 +964,34 @@ class DateTimeDisplay(object):
zone = timezone(tz)
return zone.normalize(timestamp.astimezone(zone))
- def age(self, end=None, tz=None, granularity='hour'):
+ def age(self, end=None, tz=None, granularity='hour', general=False):
"""
Return the distance of time in words from `self.datetime` to `end`.
+
+ >>> start = datetime(1984, 11, 02)
+ >>> now = datetime(2009, 5, 22, 12, 11, 10)
+ >>> DateTimeDisplay(start).age(now)
+ '2 decades, 4 years, 6 months, 20 days and 12 hours'
+ >>> DateTimeDisplay(start).age(now, general=True)
+ '2 decades'
+
"""
start = self.datetime
if not end:
end = datetime.utcnow()
if tz:
zone = timezone(tz)
- #end = zone.localize(end)
end = end.replace(tzinfo=utc)
end = zone.normalize(end.astimezone(zone))
start = self.astimezone(tz)
- return distance_of_time_in_words(start, end, granularity=granularity)
+ age = distance_of_time_in_words(start, end, granularity=granularity)
+
+ if general:
+ if not age.startswith('less than'):
+ age = age.split('and')[0].split(',')[0]
+
+ return age
+
+ def __str__(self):
+ return self.datetime.strftime('%Y-%m-%d %H:%M:%S %Z%z')
commit ff41731d379fc209e397a6e2781d23f1b3db6699
Author: Luke Macken <lmacken(a)redhat.com>
Date: Fri May 22 00:03:14 2009 -0400
Minor doctest improvement
diff --git a/moksha/lib/helpers.py b/moksha/lib/helpers.py
index 70954a4..c1dfe72 100644
--- a/moksha/lib/helpers.py
+++ b/moksha/lib/helpers.py
@@ -930,6 +930,8 @@ class DateTimeDisplay(object):
>> now = datetime(2009, 5, 12)
>> later = datetime(2009, 5, 13)
>> d = DateTimeDisplay(now)
+ >>> print d
+ 2009-05-12 00:00:00
>> d.age(later)
'1 day'
>> d.age(datetime(2010, 7, 10, 10, 10),
granularity='minute')