CodeReview/backend/app/core/timezone.py

22 lines
572 B
Python
Raw Normal View History

2026-01-09 13:23:49 +08:00
from datetime import datetime, timedelta, timezone
# 中国标准时间 (UTC+8)
CHINA_TZ = timezone(timedelta(hours=8))
def get_now():
"""获取当前的中国时间 (UTC+8)"""
return datetime.now(CHINA_TZ)
def get_now_iso():
"""获取当前中国时间的 ISO 格式字符串"""
return get_now().isoformat()
def beijing_time(*args):
"""
用于 logging.Formatter.converter 的转换函数
"""
if args:
# args[0] 是 timestamp
return datetime.fromtimestamp(args[0], CHINA_TZ).timetuple()
return get_now().timetuple()