|
|
@@ -1,6 +1,7 @@
|
|
|
# This Python file uses the following encoding: utf-8
|
|
|
# @author runhey
|
|
|
# github https://github.com/runhey
|
|
|
+import re
|
|
|
from typing import Any
|
|
|
from collections.abc import Callable, Generator
|
|
|
from datetime import timedelta, time, datetime
|
|
|
@@ -19,6 +20,22 @@ def format_timedelta(tdelta: timedelta):
|
|
|
minutes, seconds = divmod(rem, 60)
|
|
|
return f"{days:02d} {hours:02d}:{minutes:02d}:{seconds:02d}"
|
|
|
|
|
|
+def datadelta_validator(v: Any) -> timedelta:
|
|
|
+ if isinstance(v, str):
|
|
|
+ try:
|
|
|
+ pattern = r'(\d{1,2})\s+(\d{1,2}):(\d{1,2}):(\d{1,2})'
|
|
|
+ match = re.match(pattern, v)
|
|
|
+ if match:
|
|
|
+ days = int(match.group(1))
|
|
|
+ hours = int(match.group(2))
|
|
|
+ minutes = int(match.group(3))
|
|
|
+ seconds = int(match.group(4))
|
|
|
+ return TimeDelta(days=days, hours=hours, minutes=minutes, seconds=seconds)
|
|
|
+ return TimeDelta(days=1, hours=0, minutes=0, seconds=0)
|
|
|
+ except ValueError:
|
|
|
+ raise ValueError('Invalid interval value. Expected format: seconds')
|
|
|
+ return v
|
|
|
+
|
|
|
def datetime_validator(v: Any) -> datetime:
|
|
|
if isinstance(v, str):
|
|
|
return datetime.fromisoformat(v)
|
|
|
@@ -35,7 +52,7 @@ MultiLine = Annotated[str,
|
|
|
|
|
|
|
|
|
TimeDelta = Annotated[timedelta,
|
|
|
- BeforeValidator(lambda v: isinstance(v, str) or isinstance(v, timedelta)),
|
|
|
+ BeforeValidator(datadelta_validator),
|
|
|
PlainSerializer(format_timedelta, return_type=str),
|
|
|
WithJsonSchema({'type': 'time_delta'}, mode='serialization'),]
|
|
|
|