提交
This commit is contained in:
1 parent
b4a49183db
commit
33c038b8d9
37 files changed
+749
No files matched your search
@@ -0,0 +1,27 @@
|
||||
"""运输计划相关日期时间解析:前端 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)]
|
||||
Reference in new issue
Block a user