Skip to content

fix: 统一时间戳为带时区的 UTC datetime,修复跨时区部署时间显示错误#70

Merged
ZhengZhenyu merged 3 commits intoopensourceways:mainfrom
ZhengZhenyu:main
Feb 24, 2026
Merged

fix: 统一时间戳为带时区的 UTC datetime,修复跨时区部署时间显示错误#70
ZhengZhenyu merged 3 commits intoopensourceways:mainfrom
ZhengZhenyu:main

Conversation

@ZhengZhenyu
Copy link
Collaborator

  • 新增 app/core/timezone.py:提供 utc_now()、get_app_tz()、to_app_tz() 工具函数
  • 新增 APP_TIMEZONE 配置项(默认 Asia/Shanghai),控制 ICS/邮件等服务端输出的本地化时区
  • 将所有模型的 DateTime 列改为 DateTime(timezone=True),default 改用 utc_now
  • 替换所有 datetime.utcnow() 直接调用为 utc_now()(含 services、api、security)
  • ICS 日历改为标准 UTC Z 格式(DTSTART:20240115T103000Z)
  • docker-compose.yml 添加 TZ 环境变量与 APP_TIMEZONE 联动
  • 生成 Alembic 迁移 986ddbdad1d7

变更说明

关联 Issue:#

Checklist

  • 功能实现正确,无明显 Bug
  • 测试通过(新增/修改功能已补充测试,覆盖率 ≥ 80%)
  • 无敏感信息硬编码(密钥、密码、Token)
  • 数据库有变更时迁移文件已生成并测试
  • 必要的文档已同步更新

- 新增 app/core/timezone.py:提供 utc_now()、get_app_tz()、to_app_tz() 工具函数
- 新增 APP_TIMEZONE 配置项(默认 Asia/Shanghai),控制 ICS/邮件等服务端输出的本地化时区
- 将所有模型的 DateTime 列改为 DateTime(timezone=True),default 改用 utc_now
- 替换所有 datetime.utcnow() 直接调用为 utc_now()(含 services、api、security)
- ICS 日历改为标准 UTC Z 格式(DTSTART:20240115T103000Z)
- docker-compose.yml 添加 TZ 环境变量与 APP_TIMEZONE 联动
- 生成 Alembic 迁移 986ddbdad1d7
@opensourceways-bot
Copy link
Contributor

CLA Signature Manual

Because of the CLA label update fail, please comment /check-cla once again. 🙏

- 删除迁移中 _alembic_tmp_campaign_contacts 的 drop/create 操作,
  避免全新数据库执行 alembic upgrade head 时报 no such table 错误
- timezone.py / ics.py:将 timezone as _tz / _tz.utc 改为 datetime.UTC
  (ruff UP017)
- auth.py、community_dashboard.py、ics.py:修正 import 块排序 (ruff I001)
@opensourceways-bot
Copy link
Contributor

CLA Signature Manual

Because of the CLA label update fail, please comment /check-cla once again. 🙏

SQLite 的 DateTime(timezone=True) 在某些环境下读取时返回 naive datetime,
与 utc_now()(offset-aware)比较会引发 TypeError。
在 is_expired 中防御性处理:若 expires_at 无 tzinfo,先附加 UTC 再比较。
@opensourceways-bot
Copy link
Contributor

CLA Signature Manual

Because of the CLA label update fail, please comment /check-cla once again. 🙏

@ZhengZhenyu ZhengZhenyu merged commit f254f22 into opensourceways:main Feb 24, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants