Skip to content

fix: wrap timestamp converter #1477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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