Show More
@@ -353,7 +353,7 b' def engine_from_config(configuration, pr' | |||||
353 | return engine |
|
353 | return engine | |
354 |
|
354 | |||
355 |
|
355 | |||
356 | def age(prevdate, show_short_version=False): |
|
356 | def age(prevdate, show_short_version=False, now=None): | |
357 | """ |
|
357 | """ | |
358 | turns a datetime into an age string. |
|
358 | turns a datetime into an age string. | |
359 | If show_short_version is True, then it will generate a not so accurate but shorter string, |
|
359 | If show_short_version is True, then it will generate a not so accurate but shorter string, | |
@@ -364,8 +364,7 b' def age(prevdate, show_short_version=Fal' | |||||
364 | :rtype: unicode |
|
364 | :rtype: unicode | |
365 | :returns: unicode words describing age |
|
365 | :returns: unicode words describing age | |
366 | """ |
|
366 | """ | |
367 | now = datetime.datetime.now() |
|
367 | now = now or datetime.datetime.now() | |
368 | now = now.replace(microsecond=0) |
|
|||
369 | order = ['year', 'month', 'day', 'hour', 'minute', 'second'] |
|
368 | order = ['year', 'month', 'day', 'hour', 'minute', 'second'] | |
370 | deltas = {} |
|
369 | deltas = {} | |
371 | future = False |
|
370 | future = False | |
@@ -373,15 +372,13 b' def age(prevdate, show_short_version=Fal' | |||||
373 | if prevdate > now: |
|
372 | if prevdate > now: | |
374 | now, prevdate = prevdate, now |
|
373 | now, prevdate = prevdate, now | |
375 | future = True |
|
374 | future = True | |
376 |
|
375 | if future: | ||
|
376 | prevdate = prevdate.replace(microsecond=0) | |||
377 | # Get date parts deltas |
|
377 | # Get date parts deltas | |
|
378 | from dateutil import relativedelta | |||
378 | for part in order: |
|
379 | for part in order: | |
379 | if future: |
|
|||
380 | from dateutil import relativedelta |
|
|||
381 |
|
|
380 | d = relativedelta.relativedelta(now, prevdate) | |
382 |
|
|
381 | deltas[part] = getattr(d, part + 's') | |
383 | else: |
|
|||
384 | deltas[part] = getattr(now, part) - getattr(prevdate, part) |
|
|||
385 |
|
382 | |||
386 | # Fix negative offsets (there is 1 second between 10:59:59 and 11:00:00, |
|
383 | # Fix negative offsets (there is 1 second between 10:59:59 and 11:00:00, | |
387 | # not 1 hour, -59 minutes and -59 seconds) |
|
384 | # not 1 hour, -59 minutes and -59 seconds) |
@@ -114,37 +114,42 b' class TestLibs(unittest.TestCase):' | |||||
114 | ], key=lambda k: k.lower()) |
|
114 | ], key=lambda k: k.lower()) | |
115 | self.assertEqual(s, extract_mentioned_users(sample)) |
|
115 | self.assertEqual(s, extract_mentioned_users(sample)) | |
116 |
|
116 | |||
117 | def test_age(self): |
|
117 | @parameterized.expand([ | |
|
118 | (dict(), u'just now'), | |||
|
119 | (dict(seconds= -1), u'1 second ago'), | |||
|
120 | (dict(seconds= -60 * 2), u'2 minutes ago'), | |||
|
121 | (dict(hours= -1), u'1 hour ago'), | |||
|
122 | (dict(hours= -24), u'1 day ago'), | |||
|
123 | (dict(hours= -24 * 5), u'5 days ago'), | |||
|
124 | (dict(months= -1), u'1 month ago'), | |||
|
125 | (dict(months= -1, days= -2), u'1 month and 2 days ago'), | |||
|
126 | (dict(years= -1, months= -1), u'1 year and 1 month ago'), | |||
|
127 | ]) | |||
|
128 | def test_age(self, age_args, expected): | |||
118 | from rhodecode.lib.utils2 import age |
|
129 | from rhodecode.lib.utils2 import age | |
119 | from dateutil import relativedelta |
|
130 | from dateutil import relativedelta | |
120 |
n = datetime.datetime |
|
131 | n = datetime.datetime(year=2012, month=5, day=17) | |
121 | delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs) |
|
132 | delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs) | |
|
133 | self.assertEqual(age(n + delt(**age_args), now=n), expected) | |||
|
134 | ||||
|
135 | @parameterized.expand([ | |||
122 |
|
136 | |||
123 |
|
|
137 | (dict(), u'just now'), | |
124 |
|
|
138 | (dict(seconds=1), u'in 1 second'), | |
125 |
|
|
139 | (dict(seconds=60 * 2), u'in 2 minutes'), | |
126 |
|
|
140 | (dict(hours=1), u'in 1 hour'), | |
127 |
|
|
141 | (dict(hours=24), u'in 1 day'), | |
128 |
|
|
142 | (dict(hours=24 * 5), u'in 5 days'), | |
129 |
|
|
143 | (dict(months=1), u'in 1 month'), | |
130 |
|
|
144 | (dict(months=1, days=1), u'in 1 month and 1 day'), | |
131 |
|
|
145 | (dict(years=1, months=1), u'in 1 year and 1 month') | |
132 |
|
146 | ]) | ||
133 | def test_age_in_future(self): |
|
147 | def test_age_in_future(self, age_args, expected): | |
134 | from rhodecode.lib.utils2 import age |
|
148 | from rhodecode.lib.utils2 import age | |
135 | from dateutil import relativedelta |
|
149 | from dateutil import relativedelta | |
136 |
n = datetime.datetime |
|
150 | n = datetime.datetime(year=2012, month=5, day=17) | |
137 | delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs) |
|
151 | delt = lambda *args, **kwargs: relativedelta.relativedelta(*args, **kwargs) | |
138 |
|
152 | self.assertEqual(age(n + delt(**age_args), now=n), expected) | ||
139 | self.assertEqual(age(n), u'just now') |
|
|||
140 | self.assertEqual(age(n + delt(seconds=1)), u'in 1 second') |
|
|||
141 | self.assertEqual(age(n + delt(seconds=60 * 2)), u'in 2 minutes') |
|
|||
142 | self.assertEqual(age(n + delt(hours=1)), u'in 1 hour') |
|
|||
143 | self.assertEqual(age(n + delt(hours=24)), u'in 1 day') |
|
|||
144 | self.assertEqual(age(n + delt(hours=24 * 5)), u'in 5 days') |
|
|||
145 | self.assertEqual(age(n + delt(months=1)), u'in 1 month') |
|
|||
146 | self.assertEqual(age(n + delt(months=1, days=1)), u'in 1 month and 1 day') |
|
|||
147 | self.assertEqual(age(n + delt(years=1, months=1)), u'in 1 year and 1 month') |
|
|||
148 |
|
153 | |||
149 | def test_tag_exctrator(self): |
|
154 | def test_tag_exctrator(self): | |
150 | sample = ( |
|
155 | sample = ( |
General Comments 0
You need to be logged in to leave comments.
Login now