-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandy_time.jinja
101 lines (93 loc) · 3.28 KB
/
handy_time.jinja
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
{# Templates to display time #}
{#
from 'handy_time.jinja' import long_time, short_time, smart_time,
delta_mins, delta_hours, delta_days, delta_duration, format_hms
#}
{%- macro long_time(ts) -%}
{%- set d = ((as_timestamp(ts) + 43200 - as_timestamp(now())) / 43200) | int(0) -%}
{%- set abs_d = d | abs -%}
{%- set s = (
"Today" if d == 0 else
"Tomorrow" if d > 0 and d < 3 else
"Yesterday" if d < 0 and d > -3 else
((abs_d + 1) / 2) | round | int(0) | string + " days" if abs_d < 28 else
((abs_d + 1) / 14) | round | int(0) | string + " weeks" if abs_d < 120 else
((abs_d + 1) / 60) | round | int(0) | string + " months" if abs_d < 1460 else
((abs_d + 1) / 730) | round | int(0) | string + " years"
) -%}
{%- if d > 0 and abs_d > 3 -%}
in {{ s }}
{%- elif d < 0 and abs_d > 2 -%}
{{ s }} ago
{%- else -%}
{{ s }}
{%- endif -%}
{%- endmacro -%}
{%- macro short_time(ts) -%}
{%- set d = now()|as_timestamp - ts|as_timestamp -%}
{%- set d = (((d / 30) | round(0, 'ceil')) * 30) - 30 -%}
{%- set abs_d = d | abs -%}
{%- set days = (abs_d / 86400) | round(0, 'floor') -%}
{%- set hours = ((abs_d % 86400) / 3600) | round(0, 'floor') -%}
{%- set mins = (((abs_d % 86400) % 3600) / 60) | round(0, 'floor') -%}
{%- set secs = (((abs_d % 86400) % 3600) % 60) | round(0, 'floor') -%}
{%- set s = (
(days|string + 'd ' if days > 0 else '') +
(hours|string + 'h ' if hours > 0 else '') +
(mins|string + 'm ' if mins > 0 else '') +
(secs|string + 's ' if secs > 0 and days < 1 else '')
) -%}
{%- if d == 0 -%}
now
{%- elif d < 0 -%}
in {{ s | trim }}
{%- else -%}
{{ s | trim }} ago
{%- endif -%}
{%- endmacro -%}
{%- macro smart_time(ts) -%}
{%- set d = now()|as_timestamp - ts|as_timestamp -%}
{%- set d = ((d / 30) | round(0, 'ceil')) * 30 -%}
{%- set abs_d = d | abs -%}
{%- set days = (abs_d / 86400) | round(0, 'floor') -%}
{%- if days >= 14 -%}
{{ long_time(ts) }}
{%- else -%}
{{ short_time(ts) }}
{%- endif -%}
{%- endmacro -%}
{%- macro delta_days(ts) -%}
{%- set d = ts|as_timestamp - now()|as_timestamp -%}
{%- set days = (d / 86400) | round(0) -%}
{{ days }}
{%- endmacro -%}
{%- macro delta_hours(ts) -%}
{%- set d = ts|as_timestamp - now()|as_timestamp -%}
{%- set hours = (d / 3600) | round(0) -%}
{{ hours }}
{%- endmacro -%}
{%- macro delta_mins(ts) -%}
{%- set d = ts|as_timestamp - now()|as_timestamp -%}
{%- set mins = (d / 60) | round(0) -%}
{{ mins }}
{%- endmacro -%}
{%- macro format_hms(t) -%}
{%- set ta = t.split(':') -%}
{%- if ta[0]|int > 0 -%}{{- ta[0] -}}h {% endif -%}
{%- if ta[1]|int > 0 -%}{{- ta[1] -}}m {% endif -%}
{%- if ta[2]|int > 0 -%}{{- ta[2] -}}s {%- endif -%}
{%- endmacro -%}
{%- macro delta_duration(ts1, ts2) -%}
{%- set d = ts1|as_timestamp - ts2|as_timestamp -%}
{%- set sign = "+" if d >= 0 else "-" -%}
{%- set d = d | abs -%}
{%- set days = (d // 86400) | int -%}
{%- set h = ((d % 86400) // 3600) | int -%}
{%- set m = ((d % 3600) // 60) | int -%}
{%- set s = (d % 60) | int -%}
{%- set dhms = (days|string + "d " if days > 0 else "") +
(h|string + "h " if h > 0 else "") +
(m|string + "m " if m > 0 else "") +
(s|string + "s" if s > 0 else "") -%}
{{ sign + dhms if d != 0 else '-' }}
{%- endmacro -%}