fix: 统一数据库时间为北京时间(UTC+8)
- 新增 app/utils/timezone.py 提供 beijing_now() 工具函数 - models.py 所有表的 created_at/updated_at 默认值从 UTC 改为北京时间 - 解决 event_time(边缘端北京时间)与 handled_at(服务端UTC)差8小时的问题 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
20
app/utils/timezone.py
Normal file
20
app/utils/timezone.py
Normal file
@@ -0,0 +1,20 @@
|
||||
"""
|
||||
时区工具
|
||||
|
||||
本系统所有时间统一使用北京时间(UTC+8)。
|
||||
边缘端上报的 event_time 是北京时间,服务端生成的时间也使用北京时间,
|
||||
确保数据库中所有时间字段语义一致。
|
||||
"""
|
||||
|
||||
from datetime import datetime, timezone, timedelta
|
||||
|
||||
# 北京时区 UTC+8
|
||||
BEIJING_TZ = timezone(timedelta(hours=8))
|
||||
|
||||
|
||||
def beijing_now() -> datetime:
|
||||
"""返回当前北京时间(naive datetime,无时区信息)
|
||||
|
||||
用于数据库存储,与边缘端上报的 event_time 格式一致。
|
||||
"""
|
||||
return datetime.now(BEIJING_TZ).replace(tzinfo=None)
|
||||
Reference in New Issue
Block a user