You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
20 lines
557 B
Python
20 lines
557 B
Python
import datetime
|
|
import dateutil
|
|
from apistar.typesystem import TypeSystemError
|
|
|
|
|
|
class Datetime(datetime.datetime):
|
|
native_type = datetime.datetime
|
|
|
|
def __new__(cls, *args, **kwargs) -> datetime:
|
|
if args and isinstance(args[0], cls.native_type):
|
|
return args[0]
|
|
if args and isinstance(args[0], str):
|
|
try:
|
|
return dateutil.parser.parse(args[0])
|
|
except ValueError:
|
|
raise TypeSystemError(cls=cls, code='type') from None
|
|
return cls.native_type(*args, **kwargs)
|
|
|
|
|