Refactor get_age in util/dt (#2067)
parent
0626a80186
commit
630b7377bd
|
@ -156,6 +156,7 @@ def parse_time(time_str):
|
|||
|
||||
# Found in this gist: https://gist.github.com/zhangsen/1199964
|
||||
def get_age(date):
|
||||
# pylint: disable=too-many-return-statements
|
||||
"""
|
||||
Take a datetime and return its "age" as a string.
|
||||
|
||||
|
@ -175,26 +176,28 @@ def get_age(date):
|
|||
"""Return quotient and remaining."""
|
||||
return first // second, first % second
|
||||
|
||||
# pylint: disable=too-few-public-methods
|
||||
class PrettyDelta:
|
||||
"""A class for relative times."""
|
||||
delta = now() - date
|
||||
day = delta.days
|
||||
second = delta.seconds
|
||||
|
||||
def __init__(self, subDt):
|
||||
delta = now() - subDt
|
||||
self.day = delta.days
|
||||
self.second = delta.seconds
|
||||
year, day = q_n_r(day, 365)
|
||||
if year > 0:
|
||||
return formatn(year, 'year')
|
||||
|
||||
self.year, self.day = q_n_r(self.day, 365)
|
||||
self.month, self.day = q_n_r(self.day, 30)
|
||||
self.hour, self.second = q_n_r(self.second, 3600)
|
||||
self.minute, self.second = q_n_r(self.second, 60)
|
||||
month, day = q_n_r(day, 30)
|
||||
if month > 0:
|
||||
return formatn(month, 'month')
|
||||
if day > 0:
|
||||
return formatn(day, 'day')
|
||||
|
||||
def format(self):
|
||||
"""Format a datetime to relative time string."""
|
||||
for period in ['year', 'month', 'day', 'hour', 'minute', 'second']:
|
||||
number = getattr(self, period)
|
||||
if number > 0:
|
||||
return formatn(number, period)
|
||||
return "0 second"
|
||||
hour, second = q_n_r(second, 3600)
|
||||
if hour > 0:
|
||||
return formatn(hour, 'hour')
|
||||
|
||||
return PrettyDelta(date).format()
|
||||
minute, second = q_n_r(second, 60)
|
||||
if minute > 0:
|
||||
return formatn(minute, 'minute')
|
||||
if second > 0:
|
||||
return formatn(second, 'second')
|
||||
|
||||
return "0 second"
|
||||
|
|
|
@ -135,6 +135,30 @@ class TestDateUtil(unittest.TestCase):
|
|||
self.assertIsNone(dt_util.parse_datetime("not a datetime string"))
|
||||
|
||||
def test_get_age(self):
|
||||
"""Test get_age returns 5 minutes."""
|
||||
fiveminago = dt_util.now() - timedelta(minutes=5)
|
||||
self.assertEqual(dt_util.get_age(fiveminago), "5 minutes")
|
||||
"""Test get_age."""
|
||||
diff = dt_util.now() - timedelta(seconds=0)
|
||||
self.assertEqual(dt_util.get_age(diff), "0 second")
|
||||
|
||||
diff = dt_util.now() - timedelta(seconds=30)
|
||||
self.assertEqual(dt_util.get_age(diff), "30 seconds")
|
||||
|
||||
diff = dt_util.now() - timedelta(minutes=5)
|
||||
self.assertEqual(dt_util.get_age(diff), "5 minutes")
|
||||
|
||||
diff = dt_util.now() - timedelta(minutes=1)
|
||||
self.assertEqual(dt_util.get_age(diff), "1 minute")
|
||||
|
||||
diff = dt_util.now() - timedelta(minutes=300)
|
||||
self.assertEqual(dt_util.get_age(diff), "5 hours")
|
||||
|
||||
diff = dt_util.now() - timedelta(minutes=320)
|
||||
self.assertEqual(dt_util.get_age(diff), "5 hours")
|
||||
|
||||
diff = dt_util.now() - timedelta(minutes=2*60*24)
|
||||
self.assertEqual(dt_util.get_age(diff), "2 days")
|
||||
|
||||
diff = dt_util.now() - timedelta(minutes=32*60*24)
|
||||
self.assertEqual(dt_util.get_age(diff), "1 month")
|
||||
|
||||
diff = dt_util.now() - timedelta(minutes=365*60*24)
|
||||
self.assertEqual(dt_util.get_age(diff), "1 year")
|
||||
|
|
Loading…
Reference in New Issue