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