Skip to content

Commit

Permalink
fix: wrap timestamp converter (#1477)
Browse files Browse the repository at this point in the history
  • Loading branch information
LordOfPolls authored Jul 10, 2023
1 parent c66fde3 commit 61f34d8
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions interactions/client/utils/attr_converters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import inspect
import typing
import interactions
from datetime import datetime
from typing import Callable, Union, Any

Expand All @@ -20,13 +21,18 @@ def timestamp_converter(value: Union[datetime, int, float, str]) -> Timestamp:
A Timestamp object
"""
if isinstance(value, str):
return Timestamp.fromisoformat(value)
if isinstance(value, (float, int)):
return Timestamp.fromtimestamp(float(value))
if isinstance(value, datetime):
return Timestamp.fromdatetime(value)
raise TypeError("Timestamp must be one of: datetime, int, float, ISO8601 str")
try:
if isinstance(value, str):
return Timestamp.fromisoformat(value)
if isinstance(value, (float, int)):
return Timestamp.fromtimestamp(float(value))
if isinstance(value, datetime):
return Timestamp.fromdatetime(value)
raise TypeError("Timestamp must be one of: datetime, int, float, ISO8601 str")
except ValueError as e:
interactions.const.get_logger().warning("Failed to convert timestamp", exc_info=e)
# Should only happen if the timestamp is something stupid like 269533-01-01T00:00 - in which case we just return MISSING
return MISSING


def list_converter(converter) -> Callable[[list], list]:
Expand Down

0 comments on commit 61f34d8

Please sign in to comment.