Files
iot-device-management-service/app/utils/timezone.py

21 lines
593 B
Python
Raw Normal View History

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