Skip to content

Commit 0d8f406

Browse files
committed
fix datetime util using 3.11 features
1 parent 6eb53e3 commit 0d8f406

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

localstack-core/localstack/utils/time.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import time
2-
from datetime import UTC, date, datetime, timezone, tzinfo
2+
from datetime import date, datetime, timezone, tzinfo
33
from typing import Optional
4+
from zoneinfo import ZoneInfo
45

56
TIMESTAMP_FORMAT = "%Y-%m-%dT%H:%M:%S"
67
TIMESTAMP_FORMAT_TZ = "%Y-%m-%dT%H:%M:%SZ"
@@ -56,7 +57,7 @@ def parse_timestamp(ts_str: str) -> datetime:
5657
try:
5758
value = datetime.strptime(ts_str, ts_format)
5859
if value.tzinfo is None:
59-
value = value.replace(tzinfo=UTC)
60+
value = value.replace(tzinfo=ZoneInfo("UTC"))
6061
return value
6162
except ValueError:
6263
pass

tests/unit/test_common.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import threading
66
import time
77
import zipfile
8-
from datetime import UTC, date, datetime
8+
from datetime import UTC, date, datetime, timedelta
99
from zoneinfo import ZoneInfo
1010

1111
import pytest
@@ -57,7 +57,11 @@ def test_isoformat_milliseconds(self):
5757
)
5858
def test_parse_timestamp_timezone_aware(self, str_format):
5959
datetime_obj = common.parse_timestamp(str_format)
60-
assert datetime_obj.tzinfo == UTC
60+
# we cannot assert that tzinfo is `datetime.UTC` because it is only supported starting Python 3.11
61+
# so we assert manually that the returned `tzinfo` is UTC
62+
# we are using ZoneInfo("UTC") in the `parse_timestamp` utility as it is in the import path of the CLI
63+
assert datetime_obj.tzinfo is not None
64+
assert datetime_obj.utcoffset() == timedelta(0)
6165

6266
def test_base64_to_hex(self):
6367
env = common.base64_to_hex("Zm9vIGJhcg ==")

0 commit comments

Comments
 (0)