diff --git a/slack_sdk/oauth/installation_store/internals.py b/slack_sdk/oauth/installation_store/internals.py index 52eeeb98b..aa344d664 100644 --- a/slack_sdk/oauth/installation_store/internals.py +++ b/slack_sdk/oauth/installation_store/internals.py @@ -1,4 +1,4 @@ -from datetime import datetime +from datetime import datetime, timezone from typing import Type, TypeVar, Union @@ -28,6 +28,9 @@ def _timestamp_to_type(ts: Union[TimestampType, datetime, str], target_type: Typ # see https://github.com/google/pytype/issues/1012 elif isinstance(ts, datetime): + if ts.tzinfo is None: + # naive datetime values (e.g., loaded from a database) are stored in UTC + ts = ts.replace(tzinfo=timezone.utc) result = target_type(ts.timestamp()) elif isinstance(ts, str): try: diff --git a/tests/slack_sdk/oauth/installation_store/test_internals.py b/tests/slack_sdk/oauth/installation_store/test_internals.py index 74cb935fc..a34dc617c 100644 --- a/tests/slack_sdk/oauth/installation_store/test_internals.py +++ b/tests/slack_sdk/oauth/installation_store/test_internals.py @@ -1,6 +1,9 @@ +import os import sys +import time import unittest from datetime import datetime, timezone +from unittest import mock import pytest @@ -38,6 +41,16 @@ def test_timestamp_to_type(ts, target_type, expected_result): assert result == expected_result +@pytest.mark.skipif(not hasattr(time, "tzset"), reason="time.tzset is not available on this platform") +def test_timestamp_to_type_naive_datetime_is_utc(): + try: + with mock.patch.dict(os.environ, {"TZ": "America/New_York"}): + time.tzset() + assert _timestamp_to_type(datetime(2023, 11, 28, 22, 9, 7), int) == 1701209347 + finally: + time.tzset() + + def test_timestamp_to_type_invalid_str(): match = "Invalid isoformat string" with pytest.raises(ValueError, match=match):