##// END OF EJS Templates
fixed AGE funtion for future dates, testa are now cleaner with dateutil usage
marcink -
r3261:54a439ed beta
parent child Browse files
Show More
@@ -357,23 +357,27 b' def age(prevdate):'
357 357 :rtype: unicode
358 358 :returns: unicode words describing age
359 359 """
360
360 now = datetime.datetime.now()
361 now = now.replace(microsecond=0)
361 362 order = ['year', 'month', 'day', 'hour', 'minute', 'second']
362 363 deltas = {}
363 364 future = False
364 365
365 # Get date parts deltas
366 now = datetime.datetime.now()
367 366 if prevdate > now:
368 367 now, prevdate = prevdate, now
369 368 future = True
370 369
370 # Get date parts deltas
371 371 for part in order:
372 deltas[part] = getattr(now, part) - getattr(prevdate, part)
372 if future:
373 import dateutil
374 d = dateutil.relativedelta.relativedelta(now, prevdate)
375 deltas[part] = getattr(d, part + 's')
376 else:
377 deltas[part] = getattr(now, part) - getattr(prevdate, part)
373 378
374 379 # Fix negative offsets (there is 1 second between 10:59:59 and 11:00:00,
375 380 # not 1 hour, -59 minutes and -59 seconds)
376
377 381 for num, length in [(5, 60), (4, 60), (3, 24)]: # seconds, minutes, hours
378 382 part = order[num]
379 383 carry_part = order[num - 1]
@@ -119,39 +119,36 b' class TestLibs(unittest.TestCase):'
119 119 self.assertEqual(s, extract_mentioned_users(sample))
120 120
121 121 def test_age(self):
122 import calendar
123 122 from rhodecode.lib.utils2 import age
123 from dateutil import relativedelta
124 124 n = datetime.datetime.now()
125 delt = lambda *args, **kwargs: datetime.timedelta(*args, **kwargs)
126 prev_month = n.month - 1 if n.month != 1 else n.month - 2
125 delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
126
127 127 self.assertEqual(age(n), u'just now')
128 self.assertEqual(age(n - delt(seconds=1)), u'1 second ago')
129 self.assertEqual(age(n - delt(seconds=60 * 2)), u'2 minutes ago')
130 self.assertEqual(age(n - delt(hours=1)), u'1 hour ago')
131 self.assertEqual(age(n - delt(hours=24)), u'1 day ago')
132 self.assertEqual(age(n - delt(hours=24 * 5)), u'5 days ago')
133 self.assertEqual(age(n - delt(hours=24 * (calendar.mdays[prev_month]))),
134 u'1 month ago')
135 self.assertEqual(age(n - delt(hours=24 * (calendar.mdays[prev_month] + 2))),
136 u'1 month and 2 days ago')
137 self.assertEqual(age(n - delt(hours=24 * 400)), u'1 year and 1 month ago')
128 self.assertEqual(age(n + delt(seconds=-1)), u'1 second ago')
129 self.assertEqual(age(n + delt(seconds=-60 * 2)), u'2 minutes ago')
130 self.assertEqual(age(n + delt(hours=-1)), u'1 hour ago')
131 self.assertEqual(age(n + delt(hours=-24)), u'1 day ago')
132 self.assertEqual(age(n + delt(hours=-24 * 5)), u'5 days ago')
133 self.assertEqual(age(n + delt(months=-1)), u'1 month ago')
134 self.assertEqual(age(n + delt(months=-1, days=-2)), u'1 month and 2 days ago')
135 self.assertEqual(age(n + delt(years=-1, months=-1)), u'1 year and 1 month ago')
138 136
139 137 def test_age_in_future(self):
140 import calendar
141 138 from rhodecode.lib.utils2 import age
139 from dateutil import relativedelta
142 140 n = datetime.datetime.now()
143 delt = lambda *args, **kwargs: datetime.timedelta(*args, **kwargs)
141 delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs)
142
144 143 self.assertEqual(age(n), u'just now')
145 144 self.assertEqual(age(n + delt(seconds=1)), u'in 1 second')
146 145 self.assertEqual(age(n + delt(seconds=60 * 2)), u'in 2 minutes')
147 146 self.assertEqual(age(n + delt(hours=1)), u'in 1 hour')
148 147 self.assertEqual(age(n + delt(hours=24)), u'in 1 day')
149 148 self.assertEqual(age(n + delt(hours=24 * 5)), u'in 5 days')
150 self.assertEqual(age(n + delt(hours=24 * (calendar.mdays[n.month]))),
151 u'in 1 month')
152 self.assertEqual(age(n + delt(hours=24 * (calendar.mdays[n.month] + 1))),
153 u'in 1 month and 1 day')
154 self.assertEqual(age(n + delt(hours=24 * 400)), u'in 1 year and 1 month')
149 self.assertEqual(age(n + delt(months=1)), u'in 1 month')
150 self.assertEqual(age(n + delt(months=1, days=1)), u'in 1 month and 1 day')
151 self.assertEqual(age(n + delt(years=1, months=1)), u'in 1 year and 1 month')
155 152
156 153 def test_tag_exctrator(self):
157 154 sample = (
General Comments 0
You need to be logged in to leave comments. Login now