-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
57 lines (49 loc) · 1.26 KB
/
utils.py
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
54
55
56
57
import jinja2
from datetime import datetime
def friendly_time(dt, past_="ago",
future_="from now",
default="just now"):
"""
Returns string representing "time since"
or "time until" e.g.
3 days ago, 5 hours from now etc.
"""
if dt is None or isinstance(dt, jinja2.runtime.Undefined): return None
now = datetime.utcnow()
if now > dt:
diff = now - dt
dt_is_past = True
else:
diff = dt - now
dt_is_past = False
periods = (
(diff.days / 365, "year", "years"),
(diff.days / 30, "month", "months"),
(diff.days / 7, "week", "weeks"),
(diff.days, "day", "days"),
(diff.seconds / 3600, "hour", "hours"),
(diff.seconds / 60, "minute", "minutes"),
(diff.seconds, "second", "seconds"),
)
for period, singular, plural in periods:
if period:
return "%d %s %s" % (period,
singular if period == 1 else plural,
past_ if dt_is_past else future_)
return default
INSULTS = ["softheads",
"dingus",
"dingoes",
"morans",
"softheads",
"haters",
"gits",
"weenies",
"softheads",
"roundheads",
"commies",
"commit bastards",
"closet Bush supporters",
"friends",
"comrades"
]