From a3a50fa807bf4378be43e1a5d7706af6f98c0aa1 Mon Sep 17 00:00:00 2001 From: OG T Date: Thu, 26 Mar 2026 18:48:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(api):=20=E6=B4=BB=E8=BA=8D=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=20500=20=E9=8C=AF=E8=AA=A4=E4=BF=AE=E5=BE=A9=20(timez?= =?UTF-8?q?one=20=E6=AF=94=E8=BC=83)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因: incidents.sort() 比較 timezone-aware 與 naive datetime 錯誤: can't compare offset-naive and offset-aware datetimes 修復: safe_created_at() 統一轉換為 timestamp Co-Authored-By: Claude Opus 4.5 --- apps/api/src/api/v1/incidents.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/apps/api/src/api/v1/incidents.py b/apps/api/src/api/v1/incidents.py index c4cb154ee..f9deb5ceb 100644 --- a/apps/api/src/api/v1/incidents.py +++ b/apps/api/src/api/v1/incidents.py @@ -130,7 +130,18 @@ async def list_incidents() -> IncidentListResponse: incidents = await incident_service.get_active_incidents() # 按時間排序 (最新優先) - incidents.sort(key=lambda i: i.created_at, reverse=True) + # 2026-03-26 修復: 處理 timezone-aware 與 naive datetime 混合問題 + from datetime import UTC + + def safe_created_at(i: Incident) -> float: + """安全取得 timestamp,處理 timezone 混合問題""" + dt = i.created_at + if dt.tzinfo is None: + # naive datetime 假設為 UTC + dt = dt.replace(tzinfo=UTC) + return dt.timestamp() + + incidents.sort(key=safe_created_at, reverse=True) # Phase 6.5: 為每個事件生成決策令牌 (非同步並行) responses = []