26 lines
674 B
Python
26 lines
674 B
Python
# -*- coding: utf-8 -*-
|
|
"""时间与 ID:两个产品都用东八区,都只用「人可读的 ISO 秒」存时间。"""
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
from datetime import date, datetime, timedelta, timezone
|
|
|
|
TZ = timezone(timedelta(hours=8))
|
|
|
|
|
|
def now() -> str:
|
|
return datetime.now(TZ).isoformat(timespec="seconds")
|
|
|
|
|
|
def today() -> str:
|
|
return datetime.now(TZ).date().isoformat()
|
|
|
|
|
|
def add_days(days: int, base: str | None = None) -> str:
|
|
d = date.fromisoformat(base) if base else date.fromisoformat(today())
|
|
return (d + timedelta(days=days)).isoformat()
|
|
|
|
|
|
def new_id(prefix: str) -> str:
|
|
return f"{prefix}_{uuid.uuid4().hex[:12]}"
|