comparison mercurial/templatefilters.py @ 19736:f08e542ce918

templatefilters: add short format for age formatting Implements a short output format for ages e.g. "1 second ago" is abbrevated as "1s ago".
author David Soria Parra <dsp@experimentalworks.net>
date Tue, 17 Sep 2013 17:42:13 +0200
parents 1afe5d3939db
children 681f7b9213a4
comparison
equal deleted inserted replaced
19735:6907251122e3 19736:f08e542ce918
13 """:addbreaks: Any text. Add an XHTML "<br />" tag before the end of 13 """:addbreaks: Any text. Add an XHTML "<br />" tag before the end of
14 every line except the last. 14 every line except the last.
15 """ 15 """
16 return text.replace('\n', '<br/>\n') 16 return text.replace('\n', '<br/>\n')
17 17
18 agescales = [("year", 3600 * 24 * 365), 18 agescales = [("year", 3600 * 24 * 365, 'Y'),
19 ("month", 3600 * 24 * 30), 19 ("month", 3600 * 24 * 30, 'M'),
20 ("week", 3600 * 24 * 7), 20 ("week", 3600 * 24 * 7, 'W'),
21 ("day", 3600 * 24), 21 ("day", 3600 * 24, 'd'),
22 ("hour", 3600), 22 ("hour", 3600, 'h'),
23 ("minute", 60), 23 ("minute", 60, 'm'),
24 ("second", 1)] 24 ("second", 1, 's')]
25 25
26 def age(date): 26 def age(date, abbrev=False):
27 """:age: Date. Returns a human-readable date/time difference between the 27 """:age: Date. Returns a human-readable date/time difference between the
28 given date/time and the current date/time. 28 given date/time and the current date/time.
29 """ 29 """
30 30
31 def plural(t, c): 31 def plural(t, c):
32 if c == 1: 32 if c == 1:
33 return t 33 return t
34 return t + "s" 34 return t + "s"
35 def fmt(t, c): 35 def fmt(t, c, a):
36 if abbrev:
37 return "%d%s" % (c, a)
36 return "%d %s" % (c, plural(t, c)) 38 return "%d %s" % (c, plural(t, c))
37 39
38 now = time.time() 40 now = time.time()
39 then = date[0] 41 then = date[0]
40 future = False 42 future = False
46 else: 48 else:
47 delta = max(1, int(now - then)) 49 delta = max(1, int(now - then))
48 if delta > agescales[0][1] * 2: 50 if delta > agescales[0][1] * 2:
49 return util.shortdate(date) 51 return util.shortdate(date)
50 52
51 for t, s in agescales: 53 for t, s, a in agescales:
52 n = delta // s 54 n = delta // s
53 if n >= 2 or s == 1: 55 if n >= 2 or s == 1:
54 if future: 56 if future:
55 return '%s from now' % fmt(t, n) 57 return '%s from now' % fmt(t, n, a)
56 return '%s ago' % fmt(t, n) 58 return '%s ago' % fmt(t, n, a)
57 59
58 def basename(path): 60 def basename(path):
59 """:basename: Any text. Treats the text as a path, and returns the last 61 """:basename: Any text. Treats the text as a path, and returns the last
60 component of the path after splitting by the path separator 62 component of the path after splitting by the path separator
61 (ignoring trailing separators). For example, "foo/bar/baz" becomes 63 (ignoring trailing separators). For example, "foo/bar/baz" becomes