Skip to content

Commit 41f6b85

Browse files
authored
add trailing microsecond 0s to serialized timestamps (#301)
* add trailing microsecond 0s to serialized timestamps * update some more unit tests
1 parent 42ee591 commit 41f6b85

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

infrahub_sdk/timestamp.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ def __repr__(self) -> str:
8888
return f"Timestamp: {self.to_string()}"
8989

9090
def to_string(self, with_z: bool = True) -> str:
91-
if with_z:
92-
return self._obj.instant().format_common_iso()
93-
return self.to_datetime().isoformat()
91+
time_str = self.to_datetime().isoformat(timespec="microseconds")
92+
if with_z and time_str.endswith("+00:00"):
93+
time_str = time_str[:-6]
94+
time_str += "Z"
95+
return time_str
9496

9597
def to_timestamp(self) -> int:
9698
return self._obj.timestamp()

tests/unit/sdk/test_timestamp.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@
1212
def test_init_empty():
1313
t1 = Timestamp()
1414
assert isinstance(t1, Timestamp)
15-
assert t1.to_string() == t1._obj.instant().format_common_iso()
15+
assert t1.to_datetime() == t1._obj.py_datetime()
1616

1717
t2 = Timestamp(None)
1818
assert isinstance(t2, Timestamp)
19-
assert t2.to_string() == t2._obj.instant().format_common_iso()
19+
assert t2.to_datetime() == t2._obj.py_datetime()
2020

2121

2222
def test_init_timestamp():
2323
t1 = Timestamp()
2424
t2 = Timestamp(t1)
2525
assert t1.to_string() == t2.to_string()
2626
assert isinstance(t2, Timestamp)
27-
assert t2.to_string() == t2._obj.instant().format_common_iso()
27+
assert t2.to_datetime() == t2._obj.py_datetime()
2828

2929

3030
def test_parse_string():
@@ -65,15 +65,15 @@ def test_parse_string():
6565
)
6666
def test_to_datetime(input_str, expected_datetime):
6767
assert isinstance(Timestamp(input_str).to_datetime(), datetime)
68-
assert str(Timestamp(input_str).to_datetime()) == str(expected_datetime)
68+
assert Timestamp(input_str).to_datetime() == expected_datetime
6969

7070

7171
@pytest.mark.parametrize(
7272
"input_str,expected_str,expected_str_no_z",
7373
[
7474
pytest.param(
7575
"2022-01-01T10:01:01.123000Z",
76-
"2022-01-01T10:01:01.123Z",
76+
"2022-01-01T10:01:01.123000Z",
7777
"2022-01-01T10:01:01.123000+00:00",
7878
id="milliseconds",
7979
),
@@ -94,13 +94,13 @@ def test_to_string_default(input_str, expected_str, expected_str_no_z):
9494
def test_add():
9595
t1 = Timestamp("2022-01-01T10:01:01.123Z")
9696
t2 = t1.add(hours=1)
97-
assert t2.to_string() == "2022-01-01T11:01:01.123Z"
97+
assert t2.to_string() == "2022-01-01T11:01:01.123000Z"
9898

9999

100100
def test_subtract():
101101
t1 = Timestamp("2022-01-01T10:05:01.123Z")
102102
t2 = t1.subtract(hours=1)
103-
assert t2.to_string() == "2022-01-01T09:05:01.123Z"
103+
assert t2.to_string() == "2022-01-01T09:05:01.123000Z"
104104

105105

106106
def test_compare():
@@ -119,6 +119,15 @@ def test_compare():
119119
assert t11 == t12
120120

121121

122+
def test_serialize():
123+
time_no_z = "2022-01-01T11:00:00.000000+00:00"
124+
time = "2022-01-01T11:00:00.000000Z"
125+
timestamp = Timestamp(time)
126+
127+
assert timestamp.to_string(with_z=False) == time_no_z
128+
assert timestamp.to_string() == time
129+
130+
122131
@pytest.mark.parametrize("invalid_str", ["blurple", "1122334455667788", "2023-45-99"])
123132
def test_invalid_raises_correct_error(invalid_str):
124133
with pytest.raises(TimestampFormatError):

0 commit comments

Comments
 (0)