Browse Source

Cleanup to use gettext.

master
Julio Biason 15 years ago
parent
commit
d578e228ff
  1. 81
      mitterlib/ui/helpers/timesince.py

81
mitterlib/ui/helpers/timesince.py

@ -19,67 +19,58 @@
import datetime import datetime
import time import time
import gettext
# ----------------------------------------------------------------------
# I18n bits
# ----------------------------------------------------------------------
t = gettext.translation('timesince', fallback=True)
_ = t.gettext
N_ = t.ngettext
# Adapted from # Adapted (but modified to use ngettext) from
# http://code.djangoproject.com/browser/django/trunk/django/utils/timesince.py # http://code.djangoproject.com/browser/django/trunk/django/utils/timesince.py
# My version expects time to be given in UTC & returns timedelta from UTC. # My version expects time to be given in UTC & returns timedelta from UTC.
def pluralize(singular, plural, count): def timesince(timestamp):
if count == 1:
return singular
else:
return plural
def timesince(d, now=None):
""" """
Takes two datetime objects and returns the time between then and now Takes two datetime objects and returns the time between then and now
as a nicely formatted string, e.g "10 minutes" as a nicely formatted string, e.g "10 minutes"
Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since
""" """
chunks = ( chunks = (
(60 * 60 * 24 * 365, lambda n: pluralize('year', 'years', n)), (60 * 60 * 24 * 365, lambda n: N_('year', 'years', n)),
(60 * 60 * 24 * 30, lambda n: pluralize('month', 'months', n)), (60 * 60 * 24 * 30, lambda n: N_('month', 'months', n)),
(60 * 60 * 24 * 7, lambda n: pluralize('week', 'weeks', n)), (60 * 60 * 24 * 7, lambda n: N_('week', 'weeks', n)),
(60 * 60 * 24, lambda n: pluralize('day', 'days', n)), (60 * 60 * 24, lambda n: N_('day', 'days', n)),
(60 * 60, lambda n: pluralize('hour', 'hours', n)), (60 * 60, lambda n: N_('hour', 'hours', n)),
(60, lambda n: pluralize('minute', 'minutes', n))) (60, lambda n: N_('minute', 'minutes', n)))
# Convert datetime.date to datetime.datetime for comparison # Convert datetime.date to datetime.datetime for comparison
if d.__class__ is not datetime.datetime: if not isinstance(timestamp, datetime.datetime):
d = datetime.datetime(d.year, d.month, d.day) d = datetime.datetime(d.year, d.month, d.day)
if now:
t = now.timetuple() now = datetime.datetime.utcnow()
else:
t = time.gmtime()
now = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5])
# ignore microsecond part of 'd' since we removed it from 'now' # ignore microsecond part of 'd' since we removed it from 'now'
delta = now - (d - datetime.timedelta(0, 0, d.microsecond)) delta = now - timestamp
since = delta.days * 24 * 60 * 60 + delta.seconds since = (delta.days * 24 * 60 * 60) + delta.seconds
if int(since) <= 0: if since <= 0:
return 'moments' return _('just now')
for i, (seconds, name) in enumerate(chunks): result = []
count = since / seconds for (seconds, name) in chunks:
if count != 0: part = since / seconds
break if part < 1:
continue
s = '%d %s' % (count, name(count)) # all divs in Python 2.x are integers; we'll have to check this again
if i + 1 < len(chunks): # when we change to Python 3.
# Now get the second item
seconds2, name2 = chunks[i + 1]
count2 = (since - (seconds * count)) / seconds2
if count2 != 0:
s += ', %d %s' % (count2, name2(count2))
return s
result.append('%s %s' % (part, name))
since -= (seconds * part)
def timeuntil(d, now=None): # since now only have seconds (which are out of our "chunks")
""" result.append(N_('%s second', '%s seconds', since))
Like timesince, but returns a string measuring the time until
the given time. whole_message = ', '.join(result)
""" return _('%s ago') % (whole_message)
if now == None:
now = datetime.datetime.now()
return timesince(now, d)

Loading…
Cancel
Save