Files

20 lines
720 B
Python
Raw Permalink 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 时间工具(替代已弃用的 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()