aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/template_filters.py
diff options
context:
space:
mode:
authorDennis Fink2026-11-18 19:18:18 +0100
committerDennis Fink2026-11-18 19:18:18 +0100
commitbeb0131b089293512c2dab2243b9b2ff314e2387 (patch)
tree2690eaa17c689a77bccdc1268e971a96ec0e368f /webmentions_ssg/template_filters.py
parente3e195ea07fd57288d32c842689ebed7fba636f0 (diff)
downloadwebmentions-ssg-beb0131b089293512c2dab2243b9b2ff314e2387.tar.gz
webmentions-ssg-beb0131b089293512c2dab2243b9b2ff314e2387.zip
feat(core): show relative timestamps
Add reusable UTC template filters backed by `humanize` so timestamps render as relative ages while preserving machine-readable `<time datetime>` values. Keep exact UTC timestamps available in tooltips across dashboard, received, sent, detail, and public status views.
Diffstat (limited to 'webmentions_ssg/template_filters.py')
-rw-r--r--webmentions_ssg/template_filters.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/webmentions_ssg/template_filters.py b/webmentions_ssg/template_filters.py
new file mode 100644
index 0000000..e6cac2d
--- /dev/null
+++ b/webmentions_ssg/template_filters.py
@@ -0,0 +1,31 @@
+# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
+#
+# SPDX-License-Identifier: BSD-3-Clause
+
+"""Helpers for displaying UTC timestamps in templates."""
+
+from datetime import UTC, datetime
+
+import humanize
+
+
+def as_utc(value: datetime) -> datetime:
+ """Normalize a timestamp to UTC, treating timezone-naive DB values as UTC."""
+ if value.tzinfo is None:
+ return value.replace(tzinfo=UTC)
+ return value.astimezone(UTC)
+
+
+def utc_isoformat(value: datetime) -> str:
+ """Return an unambiguous machine-readable timestamp for the time element."""
+ return as_utc(value).isoformat()
+
+
+def utc_timestamp(value: datetime) -> str:
+ """Return the complete UTC timestamp for the hover tooltip."""
+ return as_utc(value).strftime("%Y-%m-%d %H:%M:%S UTC")
+
+
+def relative_time(value: datetime) -> str:
+ """Render a timestamp as a human-readable age relative to the current UTC time."""
+ return humanize.naturaltime(datetime.now(UTC) - as_utc(value))