li / services /time_utils.py
xiaolongmr
Add shop quota management and China time display
a1ff683
Raw
History Blame Contribute Delete
721 Bytes
from __future__ import annotations
from datetime import datetime, timedelta, timezone
try:
from zoneinfo import ZoneInfo
except ImportError: # pragma: no cover
ZoneInfo = None # type: ignore[assignment]
CHINA_TZ = ZoneInfo("Asia/Shanghai") if ZoneInfo else timezone(timedelta(hours=8))
def china_now() -> datetime:
return datetime.now(CHINA_TZ)
def china_now_string() -> str:
return china_now().strftime("%Y-%m-%d %H:%M:%S")
def china_from_timestamp_string(value: float) -> str:
return datetime.fromtimestamp(value, CHINA_TZ).strftime("%Y-%m-%d %H:%M:%S")
def china_date_from_timestamp_string(value: float) -> str:
return datetime.fromtimestamp(value, CHINA_TZ).strftime("%Y-%m-%d")