Files
shanxia2.0/backend/app/schemas/pl/datetime_parse.py
T
2026-07-04 08:21:49 +08:00

28 lines
929 B
Python

"""运输计划相关日期时间解析:前端 date-picker 仅传 YYYY-MM-DD。"""
from __future__ import annotations
from datetime import date, datetime, time
from typing import Annotated, Optional
from pydantic import BeforeValidator
def parse_optional_plan_datetime(value: object) -> Optional[datetime]:
if value is None:
return None
if isinstance(value, datetime):
return value
if isinstance(value, date):
return datetime.combine(value, time.min)
if isinstance(value, str):
text = value.strip()
if not text:
return None
if len(text) == 10 and text[4] == "-" and text[7] == "-":
return datetime.strptime(text, "%Y-%m-%d")
return datetime.fromisoformat(text.replace("Z", "+00:00"))
raise ValueError("invalid datetime")
OptionalPlanDateTime = Annotated[Optional[datetime], BeforeValidator(parse_optional_plan_datetime)]