"""Small shared helpers.""" import uuid from datetime import UTC, date, datetime, time from typing import Any from zoneinfo import ZoneInfo from sqlalchemy import inspect def utcnow() -> datetime: return datetime.now(UTC) def local_now() -> datetime: """Current time in the company's timezone (`APP_TIMEZONE`), timezone-aware.""" from app.config.settings import settings return datetime.now(ZoneInfo(settings.APP_TIMEZONE)) def local_today() -> date: """Today's date in the company's timezone — the server clock runs on UTC.""" return local_now().date() def _jsonable(value: Any) -> Any: if isinstance(value, uuid.UUID): return str(value) if isinstance(value, datetime | date | time): return value.isoformat() if hasattr(value, "value"): # StrEnum and friends return value.value return value def snapshot(instance: Any, fields: list[str] | None = None) -> dict[str, Any]: """Serialise an ORM instance's column values for the audit log's JSONB columns. Only loaded column attributes are read, so this never triggers lazy IO inside a transaction that is about to commit. """ mapper = inspect(instance).mapper names = fields or [c.key for c in mapper.column_attrs] return {name: _jsonable(getattr(instance, name, None)) for name in names}