Skip to content

Commit 6bd1361

Browse files
authored
fix(oauth): parse ISO timestamps with negative UTC offsets
Signed-off-by: Sebastian Turner <sebastiant3484@gmail.com>
1 parent ab4e498 commit 6bd1361

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎slack_sdk/oauth/installation_store/internals.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22
from typing import Type, TypeVar, Union
33

44

5+
def _replace_utc_designator(iso_datetime_str: str) -> str:
6+
if iso_datetime_str.endswith(("Z", "z")):
7+
return iso_datetime_str[:-1] + "+00:00"
8+
return iso_datetime_str
9+
10+
511
def _from_iso_format_to_datetime(iso_datetime_str: str) -> datetime:
6-
if "+" not in iso_datetime_str:
7-
iso_datetime_str += "+00:00"
8-
return datetime.fromisoformat(iso_datetime_str)
12+
dt = datetime.fromisoformat(_replace_utc_designator(iso_datetime_str))
13+
if dt.tzinfo is None:
14+
dt = dt.replace(tzinfo=timezone.utc)
15+
return dt
916

1017

1118
def _from_iso_format_to_unix_timestamp(iso_datetime_str: str) -> float:

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ def test_iso_format(self):
2222
dt = _from_iso_format_to_datetime("2021-07-14 08:00:17")
2323
self.assertEqual(dt.timestamp(), 1626249617.0)
2424

25+
def test_iso_format_with_utc_offset(self):
26+
dt = _from_iso_format_to_datetime("2021-07-14 17:00:17+09:00")
27+
self.assertEqual(dt.timestamp(), 1626249617.0)
28+
dt = _from_iso_format_to_datetime("2021-07-14 03:00:17-05:00")
29+
self.assertEqual(dt.timestamp(), 1626249617.0)
30+
31+
def test_iso_format_with_utc_designator(self):
32+
for iso_datetime_str in ["2021-07-14T08:00:17Z", "2021-07-14T08:00:17z", "2021-07-14T08:00:17.000Z"]:
33+
dt = _from_iso_format_to_datetime(iso_datetime_str)
34+
self.assertEqual(dt.timestamp(), 1626249617.0)
35+
2536

2637
@pytest.mark.parametrize(
2738
"ts,target_type,expected_result",
@@ -30,6 +41,8 @@ def test_iso_format(self):
3041
(datetime(2023, 11, 28, 22, 9, 7, tzinfo=timezone.utc), int, 1701209347),
3142
("1701209605", int, 1701209605),
3243
("2023-11-28 22:11:19", int, 1701209479),
44+
("2023-11-28 17:11:19-05:00", int, 1701209479),
45+
("2023-11-28T22:11:19Z", int, 1701209479),
3346
(1701209998.3429494, float, 1701209998.3429494),
3447
(datetime(2023, 11, 28, 22, 20, 25, 262571, tzinfo=timezone.utc), float, 1701210025.262571),
3548
("1701210054.4672053", float, 1701210054.4672053),

0 commit comments

Comments
 (0)