Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar/util.py
diff options
context:
space:
mode:
authorTomeu Vizoso <tomeu@tomeuvizoso.net>2008-05-19 16:03:04 (GMT)
committer Tomeu Vizoso <tomeu@tomeuvizoso.net>2008-05-24 16:07:51 (GMT)
commit708a62c06f5212773aee46d4263b9e3a96d8cf32 (patch)
tree3ff9a1ae83afae4597c0e33f771afb32befe9c68 /src/sugar/util.py
parent6c024ed52949b758c12fe92033e3fbf07b91fc25 (diff)
Move timestamp_to_elapsed_string to sugar.util and add ActivityBundle.installation_time
Diffstat (limited to 'src/sugar/util.py')
-rw-r--r--src/sugar/util.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/sugar/util.py b/src/sugar/util.py
index 12f6824..f885523 100644
--- a/src/sugar/util.py
+++ b/src/sugar/util.py
@@ -21,6 +21,8 @@ import sha
import random
import binascii
import string
+from gettext import gettext as _
+import gettext
def printable_hash(in_hash):
"""Convert binary hash data into printable characters."""
@@ -168,3 +170,70 @@ class LRU:
yield j
def keys(self):
return self.d.keys()
+
+units = [['%d year', '%d years', 356 * 24 * 60 * 60],
+ ['%d month', '%d months', 30 * 24 * 60 * 60],
+ ['%d week', '%d weeks', 7 * 24 * 60 * 60],
+ ['%d day', '%d days', 24 * 60 * 60],
+ ['%d hour', '%d hours', 60 * 60],
+ ['%d minute', '%d minutes', 60]]
+
+AND = _(' and ')
+COMMA = _(', ')
+
+# TRANS: Indicating something that just happened, eg. "just now", "moments ago"
+NOW = _('Seconds ago')
+
+# TRANS: Indicating time passed, eg. "[10 day, 5 hours] ago",
+# "[2 minutes] in the past", or "[3 years, 1 month] earlier"
+ELAPSED = _('%s ago')
+
+# Explanation of the following hack:
+# The xgettext utility extracts plural forms by reading the strings included as
+# parameters of ngettext(). As our plurals are not passed to ngettext()
+# straight away because there needs to be a calculation before we know which
+# strings need to be used, then we need to call ngettext() in a fake way so
+# xgettext will pick them up as plurals.
+
+def ngettext(singular, plural, n): pass
+
+# TRANS: Relative dates (eg. 1 month and 5 days).
+ngettext('%d year', '%d years', 1)
+ngettext('%d month', '%d months', 1)
+ngettext('%d week', '%d weeks', 1)
+ngettext('%d day', '%d days', 1)
+ngettext('%d hour', '%d hours', 1)
+ngettext('%d minute', '%d minutes', 1)
+
+del ngettext
+
+# End of plurals hack
+
+def timestamp_to_elapsed_string(timestamp, max_levels=2):
+ levels = 0
+ time_period = ''
+ elapsed_seconds = int(time.time() - timestamp)
+
+ for name_singular, name_plural, factor in units:
+ elapsed_units = elapsed_seconds / factor
+ if elapsed_units > 0:
+
+ if levels > 0:
+ time_period += COMMA
+
+ time_period += gettext.ngettext(name_singular, name_plural,
+ elapsed_units) % elapsed_units
+
+ elapsed_seconds -= elapsed_units * factor
+
+ if time_period != '':
+ levels += 1
+
+ if levels == max_levels:
+ break
+
+ if levels == 0:
+ return NOW
+
+ return ELAPSED % time_period
+