Time delta compared to ``t``. You can override ``now`` to specify what time
    to compare to.

    :param t: timestamp, :class:`datetime.date` or :class:`datetime.datetime`
              object
    :param now: default ``None``, optionally a :class:`datetime.datetime`
                object
    :param precision: default ``1``, number of fragments to return
    :param words: default ``None``, allow words like "yesterday", if set to
                  ``None`` this will be enabled if ``precision`` is set to
                  ``1``
    :param justnow: default ``datetime.timedelta(seconds=10)``,
                    :class:`datetime.timedelta` object passed to :func:`delta`
                    representing tolerance for considering argument ``t`` as
                    meaning 'just now'

    >>> import time
    >>> from datetime import datetime
    >>> print(duration(time.time() + 1))
    just now
    >>> print(duration(time.time() + 11))
    11 seconds from now
    >>> print(duration(time.time() - 1))
    just now
    >>> print(duration(time.time() - 11))
    11 seconds ago
    >>> print(duration(time.time() - 3601))
    an hour ago
    >>> print(duration(time.time() - 7201))
    2 hours ago
    >>> print(duration(time.time() - 1234567))
    2 weeks ago
    >>> print(duration(time.time() + 7200, precision=1))
    2 hours from now
    >>> print(duration(time.time() - 1234567, precision=3))
    2 weeks, 6 hours, 56 minutes ago
    >>> print(duration(datetime(2014, 9, 8), now=datetime(2014, 9, 9)))
    yesterday
    >>> print(duration(datetime(2014, 9, 7, 23), now=datetime(2014, 9, 9)))
    1 day ago
    >>> print(duration(datetime(2014, 9, 10), now=datetime(2014, 9, 9)))
    tomorrow
    >>> print(duration(datetime(2014, 9, 11, 1), now=datetime(2014, 9, 9, 23)))
    1 day from now

def duration(t, now=None, precision=1, pad=', ', words=None,
             justnow=datetime.timedelta(seconds=10)):
    '''
    Time delta compared to ``t``. You can override ``now`` to specify what time
    to compare to.

    :param t: timestamp, :class:`datetime.date` or :class:`datetime.datetime`
              object
    :param now: default ``None``, optionally a :class:`datetime.datetime`
                object
    :param precision: default ``1``, number of fragments to return
    :param words: default ``None``, allow words like "yesterday", if set to
                  ``None`` this will be enabled if ``precision`` is set to
                  ``1``
    :param justnow: default ``datetime.timedelta(seconds=10)``,
                    :class:`datetime.timedelta` object passed to :func:`delta`
                    representing tolerance for considering argument ``t`` as
                    meaning 'just now'

    >>> import time
    >>> from datetime import datetime
    >>> print(duration(time.time() + 1))
    just now
    >>> print(duration(time.time() + 11))
    11 seconds from now
    >>> print(duration(time.time() - 1))
    just now
    >>> print(duration(time.time() - 11))
    11 seconds ago
    >>> print(duration(time.time() - 3601))
    an hour ago
    >>> print(duration(time.time() - 7201))
    2 hours ago
    >>> print(duration(time.time() - 1234567))
    2 weeks ago
    >>> print(duration(time.time() + 7200, precision=1))
    2 hours from now
    >>> print(duration(time.time() - 1234567, precision=3))
    2 weeks, 6 hours, 56 minutes ago
    >>> print(duration(datetime(2014, 9, 8), now=datetime(2014, 9, 9)))
    yesterday
    >>> print(duration(datetime(2014, 9, 7, 23), now=datetime(2014, 9, 9)))
    1 day ago
    >>> print(duration(datetime(2014, 9, 10), now=datetime(2014, 9, 9)))
    tomorrow
    >>> print(duration(datetime(2014, 9, 11, 1), now=datetime(2014, 9, 9, 23)))
    1 day from now
    '''

    if words is None:
        words = precision == 1

    t1 = _to_datetime(t)
    t2 = _to_datetime(now or datetime.datetime.now())

    if t1 < t2:
        format = _('%s ago')
    else:
        format = _('%s from now')

    result, remains = delta(t1, t2, words=words, justnow=justnow)
    if result in (
            _('just now'),
            _('yesterday'),
            _('tomorrow'),
            _('last week'),
            _('next week'),
    ):
        return result

    elif precision > 1 and remains:
        t3 = t2 - datetime.timedelta(seconds=remains)
        return pad.join([
            result,
            duration(t2, t3, precision - 1, pad, words=False),
        ])

    else:
        return format % (result,)