"""UUIDv7(36 字符形式),兼容 Python 3.10。""" from __future__ import annotations import os import time import uuid def new_uuid7() -> str: if hasattr(uuid, "uuid7"): return str(uuid.uuid7()) # type: ignore[attr-defined] # RFC 9562 简化实现 ts_ms = int(time.time() * 1000) & ((1 << 48) - 1) rand_a = int.from_bytes(os.urandom(2), "big") & 0x0FFF rand_b = int.from_bytes(os.urandom(8), "big") & ((1 << 62) - 1) value = (ts_ms << 80) | (0x7 << 76) | (rand_a << 64) | (0b10 << 62) | rand_b return str(uuid.UUID(int=value))