From fa519d12b9e58ec2f6c3daa708bb6cc5b73afb98 Mon Sep 17 00:00:00 2001 From: Kris <2893855659@qq.com> Date: Mon, 22 Jun 2026 21:20:35 +0800 Subject: [PATCH] refactor(utils/datetime): rewrite utc datetime formatting logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 明确 naive 时区 datetime 的处理逻辑,补充和数据库存储约定的注释,将原逻辑拆分为显式的 naive/aware 分支处理,保持原有功能不变 --- backend/package/yuxi/utils/datetime_utils.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/backend/package/yuxi/utils/datetime_utils.py b/backend/package/yuxi/utils/datetime_utils.py index 3f181d65..9d5699d6 100644 --- a/backend/package/yuxi/utils/datetime_utils.py +++ b/backend/package/yuxi/utils/datetime_utils.py @@ -120,10 +120,15 @@ def format_utc_datetime(value: dt.datetime | None) -> str | None: Format a datetime to UTC ISO 8601 string, handling naive datetimes. Returns None for None input. - Naive datetimes are assumed to be in UTC. + Naive datetimes are assumed to be in UTC (与 DB 存储约定一致:所有 DateTime + 列以 UTC naive 存储)。aware datetimes 统一转换为 UTC。 """ if value is None: return None + if value.tzinfo is None: + # naive datetime 视为 UTC,直接附加 Z 后缀 + return value.isoformat() + "Z" + # aware datetime 转换为 UTC return utc_isoformat(value)