From b10be8150528540f133cde0eb66f3df3f746cb7c Mon Sep 17 00:00:00 2001 From: Zichao Lin Date: Mon, 6 Jul 2026 08:20:14 +0800 Subject: [PATCH] fix(admin-invites): display times in Asia/Shanghai (CST) instead of UTC --- app/routers/web.py | 16 +++++++++++++++- app/templates/admin_invites.html | 6 +++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/routers/web.py b/app/routers/web.py index cb0ba92..218572e 100644 --- a/app/routers/web.py +++ b/app/routers/web.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import datetime, timezone, timedelta from pathlib import Path from uuid import uuid4 @@ -35,6 +35,20 @@ def _country_flag(code: str) -> str: templates.env.filters["flag"] = _country_flag +_CST = timezone(timedelta(hours=8)) + + +def _to_local(dt: datetime) -> datetime: + """Convert UTC datetime to Asia/Shanghai (CST).""" + if dt is None: + return None + if dt.tzinfo is None: + dt = dt.replace(tzinfo=timezone.utc) + return dt.astimezone(_CST) + + +templates.env.filters["to_local"] = _to_local + def _redirect(url: str) -> RedirectResponse: return RedirectResponse(url, status_code=303) diff --git a/app/templates/admin_invites.html b/app/templates/admin_invites.html index 33383ea..2d5d6a4 100644 --- a/app/templates/admin_invites.html +++ b/app/templates/admin_invites.html @@ -50,14 +50,14 @@
{{ c.code }} 创建者: {{ c.creator.username if c.creator else '-' }} - {{ c.created_at.strftime('%Y-%m-%d %H:%M') }} + {{ c.created_at|to_local|strftime('%Y-%m-%d %H:%M') }} {% if c.max_uses is not none %} 已用 {{ c.use_count }} / {{ c.max_uses }} {% else %} 已用 {{ c.use_count }} / ∞ {% endif %} {% if c.expires_at %} - 过期: {{ c.expires_at.strftime('%Y-%m-%d %H:%M') }} + 过期: {{ c.expires_at|to_local|strftime('%Y-%m-%d %H:%M') }} {% else %} 永不过期 {% endif %} @@ -74,7 +74,7 @@
- +