Files

21 lines
558 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.
"""时间工具。"""
from datetime import UTC, datetime
def utc_now() -> datetime:
return datetime.now(UTC)
def to_iso(value: datetime | None) -> str | None:
if value is None:
return None
if value.tzinfo is None:
value = value.replace(tzinfo=UTC)
return value.astimezone(UTC).isoformat().replace("+00:00", "Z")
def as_utc(value: datetime) -> datetime:
"""SQLite 读回 naive datetime 时补 UTC;PG(timezone=True) 读回 aware 则原样返回。"""
return value if value.tzinfo else value.replace(tzinfo=UTC)