Skip to content

Commit

Permalink
Enhance date/time and duration (de)serialization.
Browse files Browse the repository at this point in the history
Closes #29.
  • Loading branch information
nekitdev committed Mar 11, 2024
1 parent 5c7f3bd commit c201c33
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions melody/shared/date_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from pendulum import UTC, Date, DateTime, Duration, date, duration, from_timestamp, now, parse
from pendulum import datetime as date_time
from typing_aliases import is_instance

from melody.shared.converter import CONVERTER

Expand Down Expand Up @@ -45,30 +46,55 @@ def convert_standard_date_time(standard_date_time: StandardDateTime) -> DateTime
)


def structure_date_time_ignore_type(string: str, date_time_type: Type[DateTime]) -> DateTime:
return parse(string) # type: ignore
def unstructure_date_time(date_time: DateTime) -> str:
return date_time.to_iso8601_string()


def unstructure_date_time(date_time: DateTime) -> str:
return str(date_time)
NOT_DATE_TIME = "`{}` does not represent date/time"
not_date_time = NOT_DATE_TIME.format


def structure_date_ignore_type(string: str, date_type: Type[Date]) -> Date:
return parse(string).date() # type: ignore
def structure_date_time(string: str) -> DateTime:
result = parse(string)

if is_instance(result, DateTime):
return result

raise ValueError(not_date_time(string))


def structure_date_time_ignore_type(string: str, date_time_type: Type[DateTime]) -> DateTime:
return structure_date_time(string)


def unstructure_date(date: Date) -> str:
return str(date)
return date.to_date_string()


def structure_duration_ignore_type(seconds: int, duration_type: Type[Duration]) -> Duration:
return duration(seconds=seconds)
def structure_date(string: str) -> Date:
return structure_date_time(string).date()


def structure_date_ignore_type(string: str, date_type: Type[Date]) -> Date:
return structure_date(string)


# NOTE: since we operate on `duration_ms` directly, durations are represented as seconds
# the main usage of durations is to denote `expires_in` fields, which should be in seconds


def unstructure_duration(duration: Duration) -> int:
return int(duration.total_seconds())


def structure_duration(seconds: int) -> Duration:
return duration(seconds=seconds)


def structure_duration_ignore_type(seconds: int, duration_type: Type[Duration]) -> Duration:
return structure_duration(seconds)


CONVERTER.register_structure_hook(DateTime, structure_date_time_ignore_type)
CONVERTER.register_unstructure_hook(DateTime, unstructure_date_time)

Expand Down

0 comments on commit c201c33

Please sign in to comment.