Skip to content

Commit 1710bda

Browse files
fix(oauth): treat naive datetime values as UTC when converting to timestamps (#1970)
Co-authored-by: William Bergamin <wbergamin@salesforce.com>
1 parent 463c8b3 commit 1710bda

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎slack_sdk/oauth/installation_store/internals.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import datetime
1+
from datetime import datetime, timezone
22
from typing import Type, TypeVar, Union
33

44

@@ -28,6 +28,8 @@ def _timestamp_to_type(ts: Union[TimestampType, datetime, str], target_type: Typ
2828
# see https://github.com/google/pytype/issues/1012
2929

3030
elif isinstance(ts, datetime):
31+
if ts.tzinfo is None:
32+
ts = ts.replace(tzinfo=timezone.utc)
3133
result = target_type(ts.timestamp())
3234
elif isinstance(ts, str):
3335
try:

‎tests/slack_sdk/oauth/installation_store/test_internals.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import os
12
import sys
3+
import time
24
import unittest
35
from datetime import datetime, timezone
6+
from unittest import mock
47

58
import pytest
69

@@ -38,6 +41,16 @@ def test_timestamp_to_type(ts, target_type, expected_result):
3841
assert result == expected_result
3942

4043

44+
@pytest.mark.skipif(not hasattr(time, "tzset"), reason="time.tzset is not available on this platform")
45+
def test_timestamp_to_type_naive_datetime_is_utc():
46+
try:
47+
with mock.patch.dict(os.environ, {"TZ": "America/New_York"}):
48+
time.tzset()
49+
assert _timestamp_to_type(datetime(2023, 11, 28, 22, 9, 7), int) == 1701209347
50+
finally:
51+
time.tzset()
52+
53+
4154
def test_timestamp_to_type_invalid_str():
4255
match = "Invalid isoformat string"
4356
with pytest.raises(ValueError, match=match):

0 commit comments

Comments
 (0)