Пример #1
0
def compute_time_difference(date1, date2, suffix='ago'):
    difference = relativedelta.relativedelta(date1, date2)

    minutes_d = difference.minutes
    hours_d = difference.hours
    days_d = difference.days
    years_d = difference.years

    if minutes_d == 0:
        return time_ago_in_words(date2, granularity='second',
                                 round=True) + ' ' + suffix
    elif hours_d == 1 or hours_d > 1:
        return time_ago_in_words(date2, granularity='hour',
                                 round=True) + ' ' + suffix

    elif minutes_d > 0 or minutes_d < 59:
        return time_ago_in_words(date2, granularity='minute') + ' ' + suffix

    elif days_d == 1 or days_d > 1 or days_d < 7:
        return time_ago_in_words(date2, granularity='day') + ' ' + suffix

    elif days_d > 7 or years_d == 0:
        return MONTH_MAPPING[str(date2.month)] + str(date2.day)

    else:
        return MONTH_MAPPING[str(date2.month)] + str(date2.day) + ", " + str(
            date2.year)
Пример #2
0
def time_ago_in_words(from_time):
    try:
        from datetime import datetime
        from webhelpers2 import date
        logger.debug("from_time: %s, type=%s", from_time, type(from_time))
        delta = datetime.now() - from_time
        granularity='minute'
        if delta.days > 365:
            granularity = 'year'
        elif delta.days > 30:
            granularity = 'month'
        elif delta.days > 0:
            granularity = 'day'
        elif delta.total_seconds() > 3600:
            granularity = 'hour'
        time_ago = date.time_ago_in_words(from_time, granularity=granularity)
        time_ago = time_ago + " ago"
    except:
        time_ago = '???'
    finally:
        return time_ago
Пример #3
0
 def created_in_words(self):
     return time_ago_in_words(self.created)
Пример #4
0
 def created_in_words(self):
     return time_ago_in_words(self.created)
Пример #5
0
def _jinja2_filter_dateinwords(date):
    return time_ago_in_words(date, round=True, granularity='day')
Пример #6
0
def test_time_ago_in_words():
    assert time_ago_in_words(-18 * 3600, granularity="day",
                             round=True) == '1 day'
Пример #7
0
def test_time_ago_in_words():
    assert time_ago_in_words(-18*3600, granularity="day", round=True) == '1 day'