Smart i18n date diff (twitter like)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from django import template
from django.utils.translation import ungettext, ugettext as _
import datetime

@register.filter
def date_diff(d):

    now = datetime.datetime.now()
    today = datetime.datetime(now.year, now.month, now.day)
    delta = now - d
    delta_midnight = today - d
    days = delta.days
    hours = round(delta.seconds / 3600., 0)
    minutes = round(delta.seconds / 60., 0)
    chunks = (
        (365.0, lambda n: ungettext('year', 'years', n)),
        (30.0, lambda n: ungettext('month', 'months', n)),
        (7.0, lambda n : ungettext('week', 'weeks', n)),
        (1.0, lambda n : ungettext('day', 'days', n)),
    )
    
    if days == 0:
        if hours == 0:
            if minutes > 0:
                return ungettext('1 minute ago', \
                    '%(minutes)d minutes ago', minutes) % \
                    {'minutes': minutes}
            else:
                return _("less than 1 minute ago")
        else:
            return ungettext('1 hour ago', '%(hours)d hours ago', hours) \
            % {'hours':hours}

    if delta_midnight.days == 0:
        return _("yesterday at %s") % d.strftime("%H:%M")

    count = 0
    for i, (chunk, name) in enumerate(chunks):
        if days >= chunk:
            count = round((delta_midnight.days + 1)/chunk, 0)
            break

    return _('%(number)d %(type)s ago') % \
        {'number': count, 'type': name(count)}

More like this

  1. Fuzzy Date Diff Template Filter by zain 4 years, 2 months ago
  2. Fuzzy Time of Day by waylan 5 years, 11 months ago
  3. DateTimeFluxCapacitor by jbcurtin 2 years, 3 months ago
  4. FuzzyDateTimeField by japerk 4 years, 1 month ago
  5. Human format Date representation by sachingupta006 1 year, 2 months ago

Comments

benjaoming (on August 27, 2009):

Nice function! Just a quick note for people using different default languages, adding special unicode characters to the strings: remember to add 'u' - as in u"string". Somehow the translation engine likes to make empty strings without producing error messages. Can be tricky! For instance in Danish:

chunks = (
    (365.0, lambda n: ungettext(u'år', u'år', n)),
    (30.0, lambda n: ungettext(u'måned', u'måneder', n)),
    (7.0, lambda n : ungettext(u'uge', u'uger', n)),
    (1.0, lambda n : ungettext(u'dag', u'dage', n)),
)

#

(Forgotten your password?)