feat(audit): every row keeps its date; dividers for Today, Yesterday, Last 7 days, Older (#5214)

The operator found "Today 08:03" in place of a date odd on the audit log,
and asked for the recency groups as dividers instead. |when now always
writes the date ("Thu 8 Oct 08:03"); mark_day_groups gives each entry its
group in the reader's zone, and a divider row opens each group. A scrolled
page carries the group the last one ended in, so it is not opened twice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-08 08:22:03 -04:00
co-authored by Claude Opus 5.5
parent 31e93e96cf
commit 7a444da871
7 changed files with 100 additions and 26 deletions
+23 -12
View File
@@ -9,7 +9,7 @@ here. A moment is written in the reader's own zone, on a 24-hour clock
"""
from __future__ import annotations
from datetime import datetime, timedelta, timezone, tzinfo
from datetime import datetime, timezone, tzinfo
from functools import lru_cache
from typing import Any
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError, available_timezones
@@ -120,29 +120,40 @@ def ago(when: datetime | None, now: datetime | None = None, tz: tzinfo | None =
def when(moment: datetime | None, tz: tzinfo | None = None, now: datetime | None = None) -> Markup:
"""The exact moment in the reader's zone on a 24-hour clock, as short as
it can be read: 'Today 06:58', 'Yesterday 23:41', 'Tomorrow 02:00',
'Tue 6 Oct 14:12' this year, '6 Oct 2025 14:12' before it."""
"""The exact moment in the reader's zone on a 24-hour clock, always with
its date: 'Thu 8 Oct 06:58' this year, '6 Oct 2025 14:12' before it. A
list that groups by recency says Today or Yesterday in a divider
(day_group), never in place of the date (#5214)."""
if moment is None:
return Markup(NONE)
tz = tz or timezone.utc
moment = _utc(moment)
local = moment.astimezone(tz)
today = (now or datetime.now(timezone.utc)).astimezone(tz).date()
this_year = (now or datetime.now(timezone.utc)).astimezone(tz).year
day, hm = local.date(), f"{local:%H:%M}"
if day == today:
text = f"Today {hm}"
elif day == today - timedelta(days=1):
text = f"Yesterday {hm}"
elif day == today + timedelta(days=1):
text = f"Tomorrow {hm}"
elif day.year == today.year:
if day.year == this_year:
text = f"{local:%a} {day.day} {local:%b} {hm}"
else:
text = f"{day.day} {local:%b} {day.year} {hm}"
return _time(moment, local, text)
DAY_GROUPS = ("Today", "Yesterday", "Last 7 days", "Older")
def day_group(moment: datetime, tz: tzinfo | None = None, now: datetime | None = None) -> str:
"""Which of DAY_GROUPS a past moment falls in, by calendar day in the
reader's zone: the dividers of a newest-first log."""
tz = tz or timezone.utc
day = _utc(moment).astimezone(tz).date()
back = ((now or datetime.now(timezone.utc)).astimezone(tz).date() - day).days
if back <= 0:
return DAY_GROUPS[0]
if back == 1:
return DAY_GROUPS[1]
return DAY_GROUPS[2] if back < 7 else DAY_GROUPS[3]
def status_class(value: Any, kind: str, thresholds: dict) -> str:
"""Grade a reading against Settings → Thresholds (to_thresholds_cfg):
'ok', 'warn' or 'crit', or 'unknown' when there is no reading.