Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions infrahub_sdk/timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
23 changes: 16 additions & 7 deletions tests/unit/sdk/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
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():
t1 = 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():
Expand Down Expand Up @@ -65,15 +65,15 @@ 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(
"input_str,expected_str,expected_str_no_z",
[
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",
),
Expand All @@ -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():
Expand All @@ -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):
Expand Down