# Inspired by https://shubhamjain.co/til/how-to-render-human-readable-time-in-jinja/,
# updated to our needs
-def humanize_ts(time):
- """
- Convert date in ISO format to relative, human readable string
- like 'an hour ago', 'Yesterday', '3 months ago',
- 'just now', etc
+def humanize_ts(time, max_interval="years"):
+ """Convert date/time in ISO format to relative, human readable string.
+
+ Example return values: 'an hour ago', 'Yesterday', '3 months ago',
+ 'just now', etc.
+
+ When optional max_interval is set to "days", the return value will
+ report at most the number of days ago, not week, months or years.
"""
if jinja2.is_undefined(time):
return time
+ if max_interval not in ["years", "days"]:
+ raise ValueError
+
now = datetime.now(timezone.utc)
if time[-1] == 'Z': # Convert Zulu time zone to datetime compatible format
time = time[0:-1] + '+00:00'
return str(int(second_diff / 3600)) + " hours ago"
if day_diff == 1:
return "Yesterday"
- if day_diff < 7:
+ if day_diff < 7 or max_interval == "days":
return str(day_diff) + " days ago"
if day_diff < 31:
return str(int(day_diff / 7)) + " weeks ago"
<div class="btnline">
<input type="button" value="{{ verb[event]|first }}"
onclick="addEvent('{{ event }}', '{{ verb[events[0]]|last }} the {{ title }}')" />
- ({{ last_events[event] | humanize if event in last_events else "never" }})
+ ({{ last_events[event] | humanize(max_interval="days") if event in last_events else "never" }})
</div>
{%- endfor -%}
{%- else -%} {# Nobody logged in - show overview with summary times #}
{# Calculate maximum timestamp of all relevant events #}
{%- set when = last_events.items() | selectattr(0, 'in',
- events) | map(attribute=1) | max | humanize -%}
+ events) | map(attribute=1) | max | humanize(max_interval="days") -%}
{%- if when -%}
{{ verb[events[0]]|last }} {{ when }}
{%- else -%}