def _format_date()

in python/django/python-guestbook/src/message/views.py [0:0]


def _format_date(date_obj):
    """Format the datetime object in a human readable way."""
    now = timezone.now()
    rd = dateutil.relativedelta.relativedelta(now, date_obj)

    for n, unit in [(rd.years, 'year'), (rd.days, 'day'), (rd.hours, 'hour'),
                    (rd.minutes, 'minute')]:
        if n == 1:
            return '{} {} ago'.format(n, unit)
        elif n > 1:
            return '{} {}s ago'.format(n, unit)
    return 'just now'