From 7e3d6de7552265a7f2fa3173f9f1db49dd26ac1b Mon Sep 17 00:00:00 2001 From: Aaron McCarty Date: Wed, 12 Mar 2025 10:11:37 -0700 Subject: [PATCH 1/2] add trailing microsecond 0s to serialized timestamps --- infrahub_sdk/timestamp.py | 8 +++++--- tests/unit/sdk/test_timestamp.py | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/infrahub_sdk/timestamp.py b/infrahub_sdk/timestamp.py index 15a2d18b..ffeaf214 100644 --- a/infrahub_sdk/timestamp.py +++ b/infrahub_sdk/timestamp.py @@ -88,9 +88,11 @@ def __repr__(self) -> str: return f"Timestamp: {self.to_string()}" def to_string(self, with_z: bool = True) -> str: - if with_z: - return self._obj.instant().format_common_iso() - return self.to_datetime().isoformat() + time_str = self.to_datetime().isoformat(timespec="microseconds") + if with_z and time_str.endswith("+00:00"): + time_str = time_str[:-6] + time_str += "Z" + return time_str def to_timestamp(self) -> int: return self._obj.timestamp() diff --git a/tests/unit/sdk/test_timestamp.py b/tests/unit/sdk/test_timestamp.py index d147e0fb..d0a7923e 100644 --- a/tests/unit/sdk/test_timestamp.py +++ b/tests/unit/sdk/test_timestamp.py @@ -65,7 +65,7 @@ def test_parse_string(): ) def test_to_datetime(input_str, expected_datetime): assert isinstance(Timestamp(input_str).to_datetime(), datetime) - assert str(Timestamp(input_str).to_datetime()) == str(expected_datetime) + assert Timestamp(input_str).to_datetime() == expected_datetime @pytest.mark.parametrize( @@ -73,7 +73,7 @@ def test_to_datetime(input_str, expected_datetime): [ pytest.param( "2022-01-01T10:01:01.123000Z", - "2022-01-01T10:01:01.123Z", + "2022-01-01T10:01:01.123000Z", "2022-01-01T10:01:01.123000+00:00", id="milliseconds", ), @@ -94,13 +94,13 @@ def test_to_string_default(input_str, expected_str, expected_str_no_z): def test_add(): t1 = Timestamp("2022-01-01T10:01:01.123Z") t2 = t1.add(hours=1) - assert t2.to_string() == "2022-01-01T11:01:01.123Z" + assert t2.to_string() == "2022-01-01T11:01:01.123000Z" def test_subtract(): t1 = Timestamp("2022-01-01T10:05:01.123Z") t2 = t1.subtract(hours=1) - assert t2.to_string() == "2022-01-01T09:05:01.123Z" + assert t2.to_string() == "2022-01-01T09:05:01.123000Z" def test_compare(): @@ -119,6 +119,15 @@ def test_compare(): assert t11 == t12 +def test_serialize(): + time_no_z = "2022-01-01T11:00:00.000000+00:00" + time = "2022-01-01T11:00:00.000000Z" + timestamp = Timestamp(time) + + assert timestamp.to_string(with_z=False) == time_no_z + assert timestamp.to_string() == time + + @pytest.mark.parametrize("invalid_str", ["blurple", "1122334455667788", "2023-45-99"]) def test_invalid_raises_correct_error(invalid_str): with pytest.raises(TimestampFormatError): From 0b57dc71777cc4fa78ae85d56580e105e775df26 Mon Sep 17 00:00:00 2001 From: Aaron McCarty Date: Wed, 12 Mar 2025 10:20:47 -0700 Subject: [PATCH 2/2] update some more unit tests --- tests/unit/sdk/test_timestamp.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/sdk/test_timestamp.py b/tests/unit/sdk/test_timestamp.py index d0a7923e..c800e46a 100644 --- a/tests/unit/sdk/test_timestamp.py +++ b/tests/unit/sdk/test_timestamp.py @@ -12,11 +12,11 @@ def test_init_empty(): t1 = Timestamp() assert isinstance(t1, Timestamp) - assert t1.to_string() == t1._obj.instant().format_common_iso() + assert t1.to_datetime() == t1._obj.py_datetime() t2 = Timestamp(None) assert isinstance(t2, Timestamp) - assert t2.to_string() == t2._obj.instant().format_common_iso() + assert t2.to_datetime() == t2._obj.py_datetime() def test_init_timestamp(): @@ -24,7 +24,7 @@ def test_init_timestamp(): t2 = Timestamp(t1) assert t1.to_string() == t2.to_string() assert isinstance(t2, Timestamp) - assert t2.to_string() == t2._obj.instant().format_common_iso() + assert t2.to_datetime() == t2._obj.py_datetime() def test_parse_string():