1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# vim: set filencoding=utf8
"""
Foundry Template Filters
@author: Mike Crute (mcrute@gmail.com)
@organization: SoftGroup Interactive, Inc.
@date: May 03, 2010
"""
from datetime import datetime
from foundry.utils import frozendict
def pluralize(word, how_many=1):
"Naive pluralization function."
return word if how_many == 1 else word + "s"
def nice_date_delta(from_date, to_date=datetime.now()):
"""
Provides a friendly text representation (ie. 7 months)
for the delta between two dates.
"""
delta = to_date - from_date
months = delta.days / 30
if months > 0:
return "{0} {1}".format(months, pluralize("month", months))
weeks = delta.days / 7
if weeks > 0:
return "{0} {1}".format(weeks, pluralize("week", weeks))
if delta.days > 0:
return "{0} {1}".format(delta.days, pluralize("day", delta.days))
hours = delta.seconds / (60 * 60)
if hours > 0:
return "{0} {1}".format(hours, pluralize("hour", hours))
minutes = delta.seconds / 60
if minutes > 0:
return "{0} {1}".format(minutes, pluralize("minute", minutes))
return "seconds ago"
#: Template filter registry
TEMPLATE_FILTERS = frozendict({
'nice_date_delta': nice_date_delta,
'pluralize': pluralize,
})
|