normalize_datetime¶
- safir.pydantic.normalize_datetime(v)¶
Pydantic field validator for datetime fields.
Supports
datetime
fields given as either datetime objects or seconds since epoch (not the other types Pydantic natively supports) and ensures that the resulting datetime object is timezone-aware and in the UTC timezone.- Parameters:
- Returns:
- Return type:
datetime.datetime or None
Examples
Here is a partial model that uses this function as a field validator.
class Info(BaseModel): last_used: datetime | None = Field( None, title="Last used", description="When last used in seconds since epoch", examples=[1614986130], ) _normalize_last_used = field_validator("last_used", mode="before")( normalize_datetime )