--- a/ztfy/utils/date.py Mon Nov 15 01:50:36 2010 +0100
+++ b/ztfy/utils/date.py Tue Dec 07 17:24:39 2010 +0100
@@ -23,10 +23,13 @@
# import Zope3 packages
from zope.datetime import parseDatetimetz
+from zope.i18n import translate
# import local packages
from timezone import gmtime
+from ztfy.utils import _
+
def unidate(value):
"""Get specified date converted to unicode ISO format
@@ -70,3 +73,33 @@
if type(value) is datetime:
return value
return datetime(value.year, value.month, value.day)
+
+
+def getAge(value):
+ """Get age of a given datetime (including timezone) compared to current datetime (in UTC)
+
+ @param value: a datetime value, including timezone
+ @type value: datetime
+ @return: string representing value age
+ @rtype: gettext translated string
+ """
+ now = gmtime(datetime.utcnow())
+ delta = now - value
+ if delta.days > 60:
+ return translate(_("%d months ago")) % int(round(delta.days * 1.0 / 30))
+ elif delta.days > 10:
+ return translate(_("%d weeks ago")) % int(round(delta.days * 1.0 / 7))
+ elif delta.days > 2:
+ return translate(_("%d days ago")) % delta.days
+ elif delta.days == 2:
+ return translate(_("the day before yesterday"))
+ elif delta.days == 1:
+ return translate(_("yesterday"))
+ else:
+ hours = int(round(delta.seconds * 1.0 / 3600))
+ if hours > 1:
+ return translate(_("%d hours ago")) % hours
+ elif delta.seconds > 300:
+ return translate(_("%d minutes ago")) % int(round(delta.seconds * 1.0 / 60))
+ else:
+ return translate(_("less than 5 minutes ago"))