-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
37 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,33 @@ | ||
from datetime import datetime | ||
from email.utils import parsedate_to_datetime | ||
|
||
from pydantic.validators import parse_datetime | ||
|
||
|
||
class DateTimeOrStr(datetime): | ||
@classmethod | ||
def __get_validators__(cls): | ||
yield validate_dt_or_str | ||
|
||
@classmethod | ||
def __get_pydantic_json_schema__(cls, field_schema): | ||
field_schema.update( | ||
examples=[datetime(1970, 1, 1, 0, 0, 0)], | ||
) | ||
yield cls.validate | ||
|
||
@classmethod | ||
def validate(cls, v): | ||
return validate_dt_or_str(v) | ||
def validate(cls, v) -> datetime: | ||
# Try to parse standard (RFC 821) | ||
try: | ||
return parsedate_to_datetime(v) | ||
except ValueError: | ||
pass | ||
# Try ISO | ||
try: | ||
return datetime.fromisoformat(v) | ||
except ValueError: | ||
pass | ||
# Try timestamp | ||
try: | ||
return datetime.fromtimestamp(int(v)) | ||
except ValueError: | ||
pass | ||
|
||
return parse_datetime(v) | ||
|
||
def __repr__(self): | ||
return f"DateTimeOrStp({super().__repr__()})" | ||
|
||
|
||
def validate_dt_or_str(value: str) -> datetime: | ||
# Try to parse standard (RFC 822) | ||
try: | ||
return parsedate_to_datetime(value) | ||
except ValueError: | ||
pass | ||
# Try ISO | ||
try: | ||
return datetime.fromisoformat(value) | ||
except ValueError: | ||
pass | ||
# Try timestamp | ||
try: | ||
return datetime.fromtimestamp(int(value)) | ||
except ValueError: | ||
pass | ||
|
||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters