def timesince(d, now=None, reversed=False): """ Takes two datetime objects and returns the time between d and now as a nicely formatted string, e.g. "10 minutes". If d occurs after now, then "0 minutes" is returned. Units used are years, months, weeks, days, hours, and minutes. Seconds and microseconds are ignored. Up to two adjacent units will be displayed. For example, "2 weeks, 3 days" and "1 year, 3 months" are possible outputs, but "2 weeks, 3 hours" and "1 year, 5 days" are not. Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since """ chunks = ((60 * 60 * 24 * 365, lambda n: _('year', 'years', n)), (60 * 60 * 24 * 30, lambda n: _('month', 'months', n)), (60 * 60 * 24 * 7, lambda n: _('week', 'weeks', n)), (60 * 60 * 24, lambda n: _('day', 'days', n)), (60 * 60, lambda n: _('hour', 'hours', n)), (60, lambda n: _('minute', 'minutes', n))) # Convert datetime.date to datetime.datetime for comparison. if not isinstance(d, datetime.datetime): d = datetime.datetime(d.year, d.month, d.day) if now and not isinstance(now, datetime.datetime): now = datetime.datetime(now.year, now.month, now.day) if not now: now = datetime.datetime.utcnow() delta = (d - now) if reversed else (now - d) # ignore microseconds since = delta.days * 24 * 60 * 60 + delta.seconds if since <= 0: # d is in the future compared to now, stop processing. return '0 ' + pass_to_ugettext('minutes') for i, (seconds, name) in enumerate(chunks): count = since // seconds if count != 0: break s = pass_to_ugettext('%(number)d %(type)s') % { 'number': count, 'type': name(count) } if i + 1 < len(chunks): # Now get the second item seconds2, name2 = chunks[i + 1] count2 = (since - (seconds * count)) // seconds2 if count2 != 0: s += pass_to_ugettext(', %(number)d %(type)s') % { 'number': count2, 'type': name2(count2) } return s
def timesince(d, now=None, reversed=False): """ Takes two datetime objects and returns the time between d and now as a nicely formatted string, e.g. "10 minutes". If d occurs after now, then "0 minutes" is returned. Units used are years, months, weeks, days, hours, and minutes. Seconds and microseconds are ignored. Up to two adjacent units will be displayed. For example, "2 weeks, 3 days" and "1 year, 3 months" are possible outputs, but "2 weeks, 3 hours" and "1 year, 5 days" are not. Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since """ chunks = ( (60 * 60 * 24 * 365, lambda n: _('year', 'years', n)), (60 * 60 * 24 * 30, lambda n: _('month', 'months', n)), (60 * 60 * 24 * 7, lambda n : _('week', 'weeks', n)), (60 * 60 * 24, lambda n : _('day', 'days', n)), (60 * 60, lambda n: _('hour', 'hours', n)), (60, lambda n: _('minute', 'minutes', n)) ) # Convert datetime.date to datetime.datetime for comparison. if not isinstance(d, datetime.datetime): d = datetime.datetime(d.year, d.month, d.day) if now and not isinstance(now, datetime.datetime): now = datetime.datetime(now.year, now.month, now.day) if not now: now = datetime.datetime.now(utc if is_aware(d) else None) delta = (d - now) if reversed else (now - d) # ignore microseconds since = delta.days * 24 * 60 * 60 + delta.seconds if since <= 0: # d is in the future compared to now, stop processing. return '0 ' + pass_to_ugettext('minutes') for i, (seconds, name) in enumerate(chunks): count = since // seconds if count != 0: break s = pass_to_ugettext('%(number)d %(type)s') % {'number': count, 'type': name(count)} if i + 1 < len(chunks): # Now get the second item seconds2, name2 = chunks[i + 1] count2 = (since - (seconds * count)) // seconds2 if count2 != 0: s += pass_to_ugettext(', %(number)d %(type)s') % {'number': count2, 'type': name2(count2)} return s
def test_gettext_lazy_proxy(): from mediagoblin.tools.translate import lazy_pass_to_ugettext as _ from mediagoblin.tools.translate import pass_to_ugettext, set_thread_locale proxy = _(u"Password") orig = u"Password" set_thread_locale("es") p1 = unicode(proxy) p1_should = pass_to_ugettext(orig) assert p1_should != orig, "Test useless, string not translated" assert p1 == p1_should set_thread_locale("sv") p2 = unicode(proxy) p2_should = pass_to_ugettext(orig) assert p2_should != orig, "Test broken, string not translated" assert p2 == p2_should assert p1_should != p2_should, "Test broken, same translated string" assert p1 != p2