def humanize(self, other=None, locale='en'): if other is None: utc = datetime.utcnow().replace(tzinfo=dateutil_tz.tzutc()) dt = utc.astimezone(self._datetime.tzinfo) elif isinstance(other, Arrow): dt = other._datetime elif isinstance(other, datetime): if other.tzinfo is None: dt = other.replace(tzinfo=self._datetime.tzinfo) else: dt = other.astimezone(self._datetime.tzinfo) else: raise TypeError() act_locale = locales.get_locale_by_name(locale) delta = int(get_total_seconds(self._datetime - dt)) past = delta < 0 delta = abs(delta) if delta < 10: return act_locale.format_humanize(0, 'now', past) if delta < 45: return act_locale.format_humanize(0, 'seconds', past) elif delta < 90: return act_locale.format_humanize(0, 'minute', past) elif delta < 2700: minutes = int(max(delta / 60, 2)) return act_locale.format_humanize(minutes, 'minutes', past) elif delta < 5400: return act_locale.format_humanize(0, 'hour', past) elif delta < 79200: hours = int(max(delta / 3600, 2)) return act_locale.format_humanize(hours, 'hours', past) elif delta < 129600: return act_locale.format_humanize(0, 'day', past) elif delta < 2160000: days = int(max(delta / 86400, 2)) return act_locale.format_humanize(days, 'days', past) elif delta < 3888000: return act_locale.format_humanize(0, 'month', past) elif delta < 29808000: months = max(abs(dt.month - self._datetime.month), 2) return act_locale.format_humanize(months, 'months', past) elif delta < 47260800: return act_locale.format_humanize(0, 'year', past) else: years = int(max(delta / 31536000, 2)) return act_locale.format_humanize(years, 'years', past)
def assertDtEqual(dt1, dt2, within=10): assertEqual(dt1.tzinfo, dt2.tzinfo) assertTrue(abs(get_total_seconds(dt1 - dt2)) < within)
def test_result_is_correct(self): dt1 = arrow.get(1369158067) dt2 = arrow.get(1369158078) td = dt2 - dt1 self.assert_equals(isinstance(td, timedelta), True) self.assert_equals(get_total_seconds(td), 11)