##// END OF EJS Templates
Rewrite of the age() utility function so it can be translated.
Vincent Duvert -
r2303:7090e394 beta
parent child Browse files
Show More
@@ -24,6 +24,8 b''
24 24 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 25
26 26 import re
27 from datetime import datetime
28 from pylons.i18n.translation import _, ungettext
27 29 from rhodecode.lib.vcs.utils.lazy import LazyProperty
28 30
29 31
@@ -284,40 +286,76 b' def engine_from_config(configuration, pr'
284 286 return engine
285 287
286 288
287 def age(curdate):
289 def age(prevdate):
288 290 """
289 291 turns a datetime into an age string.
290 292
291 :param curdate: datetime object
293 :param prevdate: datetime object
292 294 :rtype: unicode
293 295 :returns: unicode words describing age
294 296 """
295 297
296 from datetime import datetime
297 from webhelpers.date import time_ago_in_words
298 order = ['year', 'month', 'day', 'hour', 'minute', 'second']
299 deltas = {}
300
301 # Get date parts deltas
302 now = datetime.now()
303 for part in order:
304 deltas[part] = getattr(now, part) - getattr(prevdate, part)
298 305
299 _ = lambda s: s
306 # Fix negative offsets (there is 1 second between 10:59:59 and 11:00:00,
307 # not 1 hour, -59 minutes and -59 seconds)
300 308
301 if not curdate:
302 return ''
309 for num, length in [(5, 60), (4, 60), (3, 24)]: # seconds, minutes, hours
310 part = order[num]
311 carry_part = order[num - 1]
312
313 if deltas[part] < 0:
314 deltas[part] += length
315 deltas[carry_part] -= 1
303 316
304 agescales = [(_(u"year"), 3600 * 24 * 365),
305 (_(u"month"), 3600 * 24 * 30),
306 (_(u"day"), 3600 * 24),
307 (_(u"hour"), 3600),
308 (_(u"minute"), 60),
309 (_(u"second"), 1), ]
310
311 age = datetime.now() - curdate
312 age_seconds = (age.days * agescales[2][1]) + age.seconds
313 pos = 1
314 for scale in agescales:
315 if scale[1] <= age_seconds:
316 if pos == 6:
317 pos = 5
318 return '%s %s' % (time_ago_in_words(curdate,
319 agescales[pos][0]), _('ago'))
320 pos += 1
317 # Same thing for days except that the increment depends on the (variable)
318 # number of days in the month
319 month_lengths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
320 if deltas['day'] < 0:
321 if prevdate.month == 2 and (prevdate.year % 4 == 0 and
322 (prevdate.year % 100 != 0 or prevdate.year % 400 == 0)):
323 deltas['day'] += 29
324 else:
325 deltas['day'] += month_lengths[prevdate.month - 1]
326
327 deltas['month'] -= 1
328
329 if deltas['month'] < 0:
330 deltas['month'] += 12
331 deltas['year'] -= 1
332
333 # Format the result
334 fmt_funcs = {
335 'year': lambda d: ungettext(u'%d year', '%d years', d) % d,
336 'month': lambda d: ungettext(u'%d month', '%d months', d) % d,
337 'day': lambda d: ungettext(u'%d day', '%d days', d) % d,
338 'hour': lambda d: ungettext(u'%d hour', '%d hours', d) % d,
339 'minute': lambda d: ungettext(u'%d minute', '%d minutes', d) % d,
340 'second': lambda d: ungettext(u'%d second', '%d seconds', d) % d,
341 }
342
343 for i, part in enumerate(order):
344 value = deltas[part]
345 if value == 0:
346 continue
347
348 if i < 5:
349 sub_part = order[i + 1]
350 sub_value = deltas[sub_part]
351 else:
352 sub_value = 0
353
354 if sub_value == 0:
355 return _(u'%s ago') % fmt_funcs[part](value)
356
357 return _(u'%s and %s ago') % (fmt_funcs[part](value),
358 fmt_funcs[sub_part](sub_value))
321 359
322 360 return _(u'just now')
323 361
General Comments 0
You need to be logged in to leave comments. Login now