示例#1
0
def format(date, now=None, locale='en'):
    '''
    the entry method
    '''
    if not isinstance(date, timedelta):
        if now is None:
            now = datetime.now()
        date = parser.parse(date)
        now = parser.parse(now)

        if date is None:
            raise ParameterUnvalid('the parameter `date` should be datetime '
                                   '/ timedelta, or datetime formated string.')
        if now is None:
            raise ParameterUnvalid('the parameter `now` should be datetime, '
                                   'or datetime formated string.')
        date = now - date
    # the gap sec
    diff_seconds = int(total_seconds(date))

    # is ago or in
    ago_in = 0
    if diff_seconds < 0:
        ago_in = 1  # date is later then now, is the time in future
        diff_seconds *= -1  # chango to positive

    tmp = 0
    i = 0
    while i < SEC_ARRAY_LEN:
        tmp = SEC_ARRAY[i]
        if diff_seconds >= tmp:
            i += 1
            diff_seconds /= tmp
        else:
            break
    diff_seconds = int(diff_seconds)
    i *= 2

    if diff_seconds > (i == 0 and 9 or 1):
        i += 1

    if locale is None:
        locale = DEFAULT_LOCALE

    # Parsing function for languages with specific grammar.
    # They are called with i, ago_in and diff_seconds arguments.
    parsing_func = custom_grammar.SPECIFIC_GRAMMAR_LANGUAGES.get(
        locale, lambda i, ago_in, diff_seconds: ago_in)
    ago_in = parsing_func(i, ago_in, diff_seconds)

    tmp = timeago_template(locale, i, ago_in)
    return '%s' in tmp and tmp % diff_seconds or tmp
示例#2
0
def format(date, now=None, locale='en'):
    '''
    the entry method
    '''
    if not isinstance(date, timedelta):
        if now is None:
            now = datetime.now()
        date = parser.parse(date)
        now = parser.parse(now)

        if date is None:
            raise ParameterUnvalid('the parameter `date` should be datetime '
                                   '/ timedelta, or datetime formated string.')
        if now is None:
            raise ParameterUnvalid('the parameter `now` should be datetime, '
                                   'or datetime formated string.')
        date = now - date
    # the gap sec
    diff_seconds = int(total_seconds(date))

    # is ago or in
    ago_in = 0
    if diff_seconds < 0:
        ago_in = 1  # date is later then now, is the time in future
        diff_seconds *= -1  # chango to positive

    tmp = 0
    i = 0
    while i < SEC_ARRAY_LEN:
        tmp = SEC_ARRAY[i]
        if diff_seconds >= tmp:
            i += 1
            diff_seconds /= tmp
        else:
            break
    diff_seconds = int(diff_seconds)
    i *= 2

    if diff_seconds > (i == 0 and 9 or 1):
        i += 1

    if locale is None:
        locale = DEFAULT_LOCALE

    tmp = timeago_template(locale, i, ago_in)
    if hasattr(tmp, '__call__'):
        tmp = tmp(diff_seconds)
    return '%s' in tmp and tmp % diff_seconds or tmp
示例#3
0
def format(date, now=None, locale='en'):
    '''
    the entry method
    '''
    if isinstance(date, timedelta):
        diff_seconds = date.total_seconds()
    else:
        if now is None:
            now = datetime.now()
        date = parser.parse(date)
        now = parser.parse(now)

        if date is None:
            raise ParameterUnvalid('the parameter `date` should be datetime / timedelta, or datetime formated string.')
        if now is None:
            raise ParameterUnvalid('the parameter `now` should be datetime, or datetime formated string.')
        # the gap sec
        diff_seconds = (now - date).total_seconds()

    # is ago or in
    ago_in = 0
    if diff_seconds < 0:
        ago_in = 1  # date is later then now, is the time in future
        diff_seconds *= -1  # chango to positive

    # less then SECONDS
    if(diff_seconds < SECONDS):
        return timeago_template('JUST_NOW', locale, ago_in)

    # seconds ago
    if(diff_seconds < MINUTE_SECONDS):
        return timeago_template('SECOND_AGO', locale, ago_in) % (int(diff_seconds))

    # a minute ago
    if(diff_seconds < MINUTE_SECONDS * 2):
        return timeago_template('A_MINUTE_AGO', locale, ago_in)

    # minutes ago
    if(diff_seconds < HOUR_SECONDS):
        return timeago_template('MINUTES_AGO', locale, ago_in) % (int(diff_seconds / MINUTE_SECONDS))

    # an hour ago
    if(diff_seconds < HOUR_SECONDS * 2):
        return timeago_template('AN_HOUR_AGO', locale, ago_in)

    # hours ago
    if(diff_seconds < HOUR_SECONDS * 24):
        return timeago_template('HOURS_AGO', locale, ago_in) % (int(diff_seconds / HOUR_SECONDS))

    # a day ago
    if(diff_seconds < DAY_SECONDS * 2):
        return timeago_template('A_DAY_AGO', locale, ago_in)

    # days ago
    if(diff_seconds < DAY_SECONDS * 30):
        return timeago_template('DAYS_AGO', locale, ago_in) % (int(diff_seconds / DAY_SECONDS))

    # a month ago
    if(diff_seconds < MONTH_SECONDS * 2):
        return timeago_template('A_MONTH_AGO', locale, ago_in)

    # months ago
    if(diff_seconds < MONTH_SECONDS * 12):
        return timeago_template('MONTHS_AGO', locale, ago_in) % (int(diff_seconds / MONTH_SECONDS))

    # a year ago
    if(diff_seconds < YEAR_SECONDS * 2):
        return timeago_template('A_YEAR_AGO', locale, ago_in)

    # years ago
    return timeago_template('YEARS_AGO', locale, ago_in) % (int(diff_seconds / YEAR_SECONDS))