Skip to content

jetblack_serialization

module jetblack_iso8601

Summary

ISO 8601

Description

Utilities for converting dates and durations to and from ISO 8601 format.

function jetblack_iso8601.datetime_to_iso8601

Summary

Convert datetime to ISO 8601

jetblack_iso8601.datetime_to_iso8601(
timestamp: datetime
) -> str

Parameters

timestamp: datetime

The timestamp

Returns

str: The stringified ISO 8601 version of the timestamp

function jetblack_iso8601.iso8601_to_datetime

Summary

Parse an ISO 8601 datetime.

jetblack_iso8601.iso8601_to_datetime(
value: str
) -> Optional[datetime]

Parameters

value: str

The ISO 8601 date string

Returns

Optional[datetime]: A timestamp if the value could be parsed, otherwise None.

function jetblack_iso8601.iso8601_to_timedelta

Summary

Convert an ISO 8601 duration to a timedelta

jetblack_iso8601.iso8601_to_timedelta(
duration: str
) -> Optional[timedelta]

Parameters

duration: str

An ISO 8601 format duration

Returns

Optional[timedelta]: The duration as a timedelta if the string could be parsed, otherwise None.

function jetblack_iso8601.timedelta_to_iso8601

Summary

Convert a timedelta to an ISO 8601 duration string

Description

Prefers weeks to days, so a roundtrip of P7D becomes P1W. Also an zero value is removed. A zero duration becomes P0D.

jetblack_iso8601.timedelta_to_iso8601(
value: timedelta
) -> str

Parameters

value: timedelta

A timedelta

Returns

str: The ISO 8601 duration representation of the timedelta.