Files
iot-device-management-service/app/utils/timezone.py
16337 b1b96a3ebc 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>
2026-03-09 16:23:05 +08:00

21 lines
593 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
时区工具
本系统所有时间统一使用北京时间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)