mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-08 11:17:27 +00:00
handle dates as strings
This commit is contained in:
parent
330580c775
commit
12bbd1a4fe
1 changed files with 8 additions and 0 deletions
|
|
@ -5,6 +5,14 @@ from typing import Any
|
|||
def normalise_date(value: Any, allow_none: bool = True) -> date | None:
|
||||
if value is None and allow_none:
|
||||
return None
|
||||
|
||||
if isinstance(value, datetime):
|
||||
return value.date()
|
||||
|
||||
if isinstance(value, str):
|
||||
try:
|
||||
return datetime.strptime(value.strip(), "%d/%m/%Y").date()
|
||||
except ValueError as exc:
|
||||
raise ValueError(f"Invalid date string: {value!r}") from exc
|
||||
|
||||
raise ValueError(f"Unexpected date value: {value!r}")
|
||||
Loading…
Add table
Reference in a new issue