30 lines
872 B
Python
30 lines
872 B
Python
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 的转换函数。
|
|
支持 (timestamp) 或 (formatter, timestamp) 调用形式。
|
|
"""
|
|
if len(args) == 2:
|
|
# 被作为 bound method 调用: (self, timestamp)
|
|
ts = args[1]
|
|
elif len(args) == 1:
|
|
# 被作为普通函数调用: (timestamp)
|
|
ts = args[0]
|
|
else:
|
|
# 无参数调用(不常见,但作为兜底)
|
|
ts = datetime.now().timestamp()
|
|
|
|
return datetime.fromtimestamp(ts, CHINA_TZ).timetuple()
|