diff --git a/src/aiodynamo/utils.py b/src/aiodynamo/utils.py index 9a70fe9..b02154f 100644 --- a/src/aiodynamo/utils.py +++ b/src/aiodynamo/utils.py @@ -168,9 +168,12 @@ def serialize_dict(value: Mapping[str, Any]) -> Dict[str, Dict[str, Any]]: def parse_amazon_timestamp(timestamp: str) -> datetime.datetime: - return datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%SZ").replace( - tzinfo=datetime.timezone.utc - ) + if "." in timestamp: + value = datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%fZ") + else: + value = datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%SZ") + + return value.replace(tzinfo=datetime.timezone.utc) async def wait( diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 9734ef9..01e9818 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -1,4 +1,5 @@ import base64 +import datetime from decimal import Decimal from functools import partial from typing import Any, Callable, Dict @@ -10,7 +11,28 @@ ) from aiodynamo.types import NumericTypeConverter -from aiodynamo.utils import deserialize, dy2py +from aiodynamo.utils import deserialize, dy2py, parse_amazon_timestamp + + +@pytest.mark.parametrize( + ("amazon_timestamp", "expected"), + [ + ( + "2020-03-12T15:37:51Z", + datetime.datetime(2020, 3, 12, 15, 37, 51, tzinfo=datetime.timezone.utc), + ), + ( + "2024-12-06T08:03:52.192266Z", + datetime.datetime( + 2024, 12, 6, 8, 3, 52, 192266, tzinfo=datetime.timezone.utc + ), + ), + ], +) +def test_parse_amazon_timestamp( + amazon_timestamp: str, expected: datetime.datetime +) -> None: + assert parse_amazon_timestamp(amazon_timestamp) == expected def test_binary_decode() -> None: