Repository navigation
fix(admin): 公告发布时间改用 UTC 写入,修复线上显示偏 8 小时 - #333
Merged
Rosshow merged 1 commit intoOct 19, 2026
Merged
Conversation
published_at 原用 datetime.now()(进程本地时间,生产容器 +08:00), 而 app/core/db.py 已把 DB 会话时区固定为 +00:00、前端 parseUtcDate 又把 无时区 ISO 串统一按 UTC 解析再转本地,导致发布时间被多加 8 小时 (生产实测「发布于 16:39」显示成次日 00:39)。 改为 _utcnow() = datetime.now(timezone.utc).replace(tzinfo=None), 与 created_at/updated_at 的 func.now()(UTC 会话)口径一致; 新建/编辑转发布/单独发布三个入口统一收口。 测试补回归钉:新建与发布写出的 published_at 必须落在当前 UTC 时刻 ±120s 内(本地 +08 环境可稳定抓出旧的本地时间写法)。
Rosshow
approved these changes
Oct 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
生产公告「测试」真实发布时刻是 16:39(北京),但后台列表把它显示成次日 00:39,差 8 小时。
根因
published_at用datetime.now()写入,取的是进程本地时间(生产容器+08:00);而项目的时区口径是全链路 UTC:
backend/app/core/db.py的_ensure_utc_session把每个 DB 会话time_zone固定为+00:00,因此created_at/updated_at的func.now()都是 UTC;parseUtcDate对无时区 ISO 串统一补Z当 UTC 解析,再转本地展示。于是
published_at存的是 +08 墙上时间、却被当 UTC 再 +8 小时 → 整体偏 8 小时。同一条公告
created_at=08:39(UTC,正确)与published_at=16:39(本地,错误)自相矛盾,即为实证。改动
backend/app/modules/admin/api/announcement.py:新增_utcnow()(datetime.now(timezone.utc).replace(tzinfo=None)),替换新建 / 编辑转发布 / 单独发布三处datetime.now()。backend/tests/test_announcement_api.py:补回归钉_assert_is_utc,断言写入的published_at落在当前 UTC 时刻 ±120s 内(本地 +08 环境可稳定抓出旧写法;CI 为 UTC 时区不会误报)。验证
pytest tests/test_announcement_api.py -q→ 15 passedlocal - utc = 8.0h,旧写法差值 28800s 必然触发断言存量数据
生产库
announcements仅 id=1 一条,published_at需由2026-10-19 16:39:29回退为2026-10-19 08:39:29(UTC),已单独处理。