Render article/feed times in the viewer's local timezone, client-side

Everything is stored and server-rendered in UTC (config.settings.base's
TIME_ZONE), so article times, the article detail header, and the
dashboard's "last fetched" column were always showing UTC regardless of
where the viewer actually is.

Article/feed timestamps now render as <time datetime="..."> elements
carrying the UTC instant plus a UTC-formatted fallback as their initial
text; static/js/localize-times.js reformats them using the browser's
own timezone via toLocaleString(), both on page load and after every
htmx swap (pagination, the inline article expand, the dashboard's retry
action all inject new <time> elements without a full page load).

No server-side change needed — this doesn't touch the (currently
unused) per-user timezone field, and works correctly even if the
viewer travels or changes their system timezone, with no settings page
required. Verified live in the browser against a UTC-stored timestamp
rendering correctly shifted to the sandbox's local zone.

Known related issue, out of scope here: the stream's "Received
today/yesterday" date-group headers (apps.reader.views._group_by_date)
are still computed server-side via timezone.localdate(), which is also
effectively UTC — an article just before/after UTC midnight could land
in a date group that doesn't match its now-correctly-localized
time-of-day. Flagged for the user rather than fixed, since correcting
it properly would mean moving date-grouping to the client too.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
ksmithandClaude Sonnet 5 committed 2026-10-31 13:31:07 -06:00
1 parent 90837b339c
commit fd68400822
5 files changed
+56 -3

No files matched your search

+41
View File
@@ -0,0 +1,41 @@
(function () {
// Everything is stored and server-rendered in UTC (config.settings.base
// TIME_ZONE) — these <time datetime="..."> elements carry that UTC
// instant plus a UTC-formatted fallback as their initial text, and this
// reformats them using the browser's own timezone, client-side, so the
// server never needs to know (or guess) where the viewer actually is.
var SHORT_OPTIONS = { hour: "numeric", minute: "2-digit" };
var LONG_OPTIONS = {
year: "numeric",
month: "short",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
};
function localizeTimesWithin(root) {
if (!root || typeof root.querySelectorAll !== "function") return;
root.querySelectorAll("time[data-local-format]").forEach(function (el) {
var iso = el.getAttribute("datetime");
if (!iso) return;
var date = new Date(iso);
if (isNaN(date.getTime())) return;
var options = el.dataset.localFormat === "short" ? SHORT_OPTIONS : LONG_OPTIONS;
try {
el.textContent = date.toLocaleString(undefined, options);
} catch (e) {
/* Intl unavailable or threw — leave the server-rendered UTC text as-is */
}
});
}
localizeTimesWithin(document);
// Article rows/content are frequently swapped in by htmx (pagination,
// the inline expand-article view, the dashboard's retry action) without
// a full page load, so every swap needs the same pass over just the
// new content.
document.body.addEventListener("htmx:afterSwap", function (event) {
localizeTimesWithin(event.target);
});
})();
+1
View File
@@ -53,6 +53,7 @@
{% endif %} {% endif %}
<script src="{% static 'js/stream.js' %}" defer></script> <script src="{% static 'js/stream.js' %}" defer></script>
<script src="{% static 'js/localize-times.js' %}" defer></script>
{% if user.is_authenticated %} {% if user.is_authenticated %}
<script src="{% static 'js/theme.js' %}" defer></script> <script src="{% static 'js/theme.js' %}" defer></script>
<script src="{% static 'js/sidebar-state.js' %}" defer></script> <script src="{% static 'js/sidebar-state.js' %}" defer></script>
+7 -1
View File
@@ -31,7 +31,13 @@
<td>{{ feed.subscriber_count }}</td> <td>{{ feed.subscriber_count }}</td>
<td>{{ feed.get_last_fetch_status_display }}</td> <td>{{ feed.get_last_fetch_status_display }}</td>
<td>{{ feed.fetch_error_count }}</td> <td>{{ feed.fetch_error_count }}</td>
<td>{{ feed.last_fetched_at|default:"never" }}</td> <td>
{% if feed.last_fetched_at %}
<time datetime="{{ feed.last_fetched_at|date:"c" }}" data-local-format="long">{{ feed.last_fetched_at|date:"M j, Y H:i" }}</time>
{% else %}
never
{% endif %}
</td>
<td>{{ feed.last_error_message|default:"—" }}</td> <td>{{ feed.last_error_message|default:"—" }}</td>
<td> <td>
<form method="post" action="{% url 'core:retry_feed' feed.id %}"> <form method="post" action="{% url 'core:retry_feed' feed.id %}">
+4 -1
View File
@@ -2,7 +2,10 @@
<h2 class="article-content-title">{{ article.title|default:"(untitled)" }}</h2> <h2 class="article-content-title">{{ article.title|default:"(untitled)" }}</h2>
<p class="article-meta"> <p class="article-meta">
{% if article.feed.favicon_url %}<img src="{{ article.feed.favicon_url }}" alt="" class="feed-favicon">{% endif %} {% if article.feed.favicon_url %}<img src="{{ article.feed.favicon_url }}" alt="" class="feed-favicon">{% endif %}
{{ article.feed.title }} &middot; {{ article.published_at|date:"M j, Y H:i" }} {{ article.feed.title }}
{% if article.published_at %}
&middot; <time datetime="{{ article.published_at|date:"c" }}" data-local-format="long">{{ article.published_at|date:"M j, Y H:i" }}</time>
{% endif %}
{% if article.author %} &middot; {{ article.author }}{% endif %} {% if article.author %} &middot; {{ article.author }}{% endif %}
{% if article.link %} &middot; <a href="{{ article.link }}" target="_blank" rel="noopener noreferrer">Original &#8599;</a>{% endif %} {% if article.link %} &middot; <a href="{{ article.link }}" target="_blank" rel="noopener noreferrer">Original &#8599;</a>{% endif %}
</p> </p>
+3 -1
View File
@@ -8,7 +8,9 @@
<a href="{% url 'reader:article_detail' article.pk %}" class="article-title" <a href="{% url 'reader:article_detail' article.pk %}" class="article-title"
data-inline-url="{% url 'reader:article_inline' article.pk %}" data-inline-url="{% url 'reader:article_inline' article.pk %}"
onclick="return toggleArticle(event, {{ article.pk }})">{{ article.title|default:"(untitled)" }}</a> onclick="return toggleArticle(event, {{ article.pk }})">{{ article.title|default:"(untitled)" }}</a>
<span class="article-time">{{ article.published_at|date:"g:i A" }}</span> {% if article.published_at %}
<time class="article-time" datetime="{{ article.published_at|date:"c" }}" data-local-format="short">{{ article.published_at|date:"g:i A" }}</time>
{% endif %}
</div> </div>
<div id="article-body-{{ article.pk }}" class="article-inline-body" hidden></div> <div id="article-body-{{ article.pk }}" class="article-inline-body" hidden></div>
</li> </li>