20 lines
581 B
Python
20 lines
581 B
Python
|
|
from datetime import datetime, timezone
|
||
|
|
from typing import Annotated
|
||
|
|
|
||
|
|
from pydantic import BaseModel, ConfigDict, PlainSerializer
|
||
|
|
|
||
|
|
|
||
|
|
def _serialize_utc(dt: datetime) -> str:
|
||
|
|
"""All DB datetimes are naive UTC; emit RFC3339 with Z so browsers parse
|
||
|
|
them into the user's local timezone."""
|
||
|
|
if dt.tzinfo is None:
|
||
|
|
dt = dt.replace(tzinfo=timezone.utc)
|
||
|
|
return dt.isoformat().replace("+00:00", "Z")
|
||
|
|
|
||
|
|
|
||
|
|
UTCDateTime = Annotated[datetime, PlainSerializer(_serialize_utc, return_type=str)]
|
||
|
|
|
||
|
|
|
||
|
|
class AppModel(BaseModel):
|
||
|
|
model_config = ConfigDict(from_attributes=True)
|