20 lines
720 B
Python
20 lines
720 B
Python
"""UTC 时间工具(替代已弃用的 datetime.utcnow)
|
||
|
||
Python 3.12+ 起 datetime.utcnow() 被标记为弃用,
|
||
统一使用 datetime.now(timezone.utc) 的快捷封装。
|
||
注意:SQLite 不保存时区信息,为兼容既有数据与比较逻辑,
|
||
默认返回 naive UTC 时间(与 utcnow 行为一致,但来源非弃用 API)。
|
||
"""
|
||
|
||
from datetime import datetime, timezone
|
||
|
||
|
||
def utcnow() -> datetime:
|
||
"""返回当前 UTC 时间(naive,与 datetime.utcnow() 行为一致)"""
|
||
return datetime.now(timezone.utc).replace(tzinfo=None)
|
||
|
||
|
||
def utcnow_iso() -> str:
|
||
"""返回当前 UTC 时间的 ISO 格式字符串(带 Z 后缀标识 UTC)"""
|
||
return datetime.now(timezone.utc).isoformat()
|