Example #1
0
def _format_timedelta(delta, granularity='second', threshold=.85, locale=None):
    """Return a time delta according to the rules of the given locale.

    function copied and slightly modified from babel.dates.format_timedelta
    see babel package information for details and
    http://babel.edgewall.org/wiki/License for the license

    """
    if isinstance(delta, timedelta):
        seconds = int((delta.days * 86400) + delta.seconds)
    else:
        seconds = delta

    if locale is None:
        locale = get_locale()

    for unit, secs_per_unit in TIMEDELTA_UNITS:
        value = abs(seconds) / secs_per_unit
        if value >= threshold or unit == granularity:
            if unit == granularity and value > 0:
                value = max(1, value)
            value = int(round(value))
            plural_form = locale.plural_form(value)
            pattern = locale._data['unit_patterns'][unit][plural_form]
            return pattern.replace(u'{0}', humanize_number(value))

    return u''
Example #2
0
def _format_timedelta(delta, granularity="second", threshold=0.85, locale=None):
    """Return a time delta according to the rules of the given locale.

    function copied and slightly modified from babel.dates.format_timedelta
    see babel package information for details and
    http://babel.edgewall.org/wiki/License for the license

    """
    if isinstance(delta, timedelta):
        seconds = int((delta.days * 86400) + delta.seconds)
    else:
        seconds = delta

    if locale is None:
        locale = get_locale()

    for unit, secs_per_unit in TIMEDELTA_UNITS:
        value = abs(seconds) / secs_per_unit
        if value >= threshold or unit == granularity:
            if unit == granularity and value > 0:
                value = max(1, value)
            value = int(round(value))
            plural_form = locale.plural_form(value)
            pattern = locale._data["unit_patterns"][unit][plural_form]
            return pattern.replace(u"{0}", humanize_number(value))

    return u""
Example #3
0
    def _inner(*args, **kwargs):
        if 'locale' not in kwargs or kwargs['locale'] is dates.LC_TIME:
            kwargs['locale'] = get_locale()
        # yet only a few methods can work with timezone information properly
        # we also check if the applied value is a basestring.  If it is
        # we convert it to a proper pytz timezone value.  If it's not we
        # assume to get a proper timezone value.
        tzinfo = kwargs.get('tzinfo', None)
        if isinstance(tzinfo, basestring) or tzinfo is None:
            tzinfo = get_timezone(tzinfo)
        if func.func_name in _timezone_aware:
            kwargs['tzinfo'] = tzinfo

        return func(*args, **kwargs)
Example #4
0
    def _inner(*args, **kwargs):
        if "locale" not in kwargs or kwargs["locale"] is dates.LC_TIME:
            kwargs["locale"] = get_locale()
        # yet only a few methods can work with timezone information properly
        # we also check if the applied value is a basestring.  If it is
        # we convert it to a proper pytz timezone value.  If it's not we
        # assume to get a proper timezone value.
        tzinfo = kwargs.get("tzinfo", None)
        if isinstance(tzinfo, basestring) or tzinfo is None:
            tzinfo = get_timezone(tzinfo)
        if func.func_name in _timezone_aware:
            kwargs["tzinfo"] = tzinfo

        return func(*args, **kwargs)