Source code for jsonype.time_converters

from datetime import date, datetime, time

from jsonype.basic_from_json_converters import (FromJsonConverter,
                                                FunctionBasedFromSimpleJsonConverter)
from jsonype.basic_to_json_converters import FunctionBasedToSimpleJsonConverter, ToJsonConverter


# mimic class name
# noinspection PyPep8Naming
[docs] def ToDatetime() -> FromJsonConverter[datetime, None]: # noqa: N802 """Return a converter that converts a JSON string to a :class:`datetime.datetime`. The JSON string is expected to be in ISO-format (as generated by :meth:`datetime.datetime.isoformat`), otherwise the conversion raises a :class:`FromJsonConversionError` """ return FunctionBasedFromSimpleJsonConverter(datetime.fromisoformat, str, datetime)
# mimic class name # noinspection PyPep8Naming
[docs] def FromDatetime() -> ToJsonConverter[datetime]: # noqa: N802 """Return a converter that converts objects of type :class:`datetime.datetime`. A ``datetime`` is converted to its iso-string using :meth:`datetime.datetime.isoformat` """ return FunctionBasedToSimpleJsonConverter[datetime](datetime.isoformat, datetime)
# mimic class name # noinspection PyPep8Naming
[docs] def ToDate() -> FromJsonConverter[date, None]: # noqa: N802 """Return a converter that converts a JSON string to a :class:`datetime.date`. The JSON string is expected to be in ISO-format (as generated by :meth:`datetime.date.isoformat`), otherwise the conversion raises a :class:`FromJsonConversionError` """ return FunctionBasedFromSimpleJsonConverter(date.fromisoformat, str, date)
# mimic class name # noinspection PyPep8Naming
[docs] def FromDate() -> ToJsonConverter[date]: # noqa: N802 """Return a converter that converts objects of type :class:`datetime.date`. A ``date`` is converted to its iso-string using :meth:`datetime.date.isoformat` """ # correct according to mypy # noinspection PyTypeChecker return FunctionBasedToSimpleJsonConverter[date](date.isoformat, date)
# mimic class name # noinspection PyPep8Naming
[docs] def ToTime() -> FromJsonConverter[time, None]: # noqa: N802 """Return a converter that converts a JSON string to a :class:`datetime.time`. The JSON string is expected to be in ISO-format (as generated by :meth:`datetime.time.isoformat`), otherwise the conversion raises a :class:`FromJsonConversionError` """ return FunctionBasedFromSimpleJsonConverter(time.fromisoformat, str, time)
# mimic class name # noinspection PyPep8Naming
[docs] def FromTime() -> ToJsonConverter[time]: # noqa: N802 """Return a converter that converts objects of type :class:`datetime.time`. A ``time`` is converted to its iso-string using :meth:`datetime.time.isoformat` """ # correct according to mypy # noinspection PyTypeChecker return FunctionBasedToSimpleJsonConverter[time](time.isoformat, time)