Skip to content

Commit

Permalink
Fix timestamp comparison (#1038)
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Angel Garcia <[email protected]>
  • Loading branch information
magmax authored May 28, 2024
1 parent e364a96 commit 09a5ae3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions prometheus_client/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def __ne__(self, other: object) -> bool:
return not self == other

def __gt__(self, other: "Timestamp") -> bool:
return self.sec > other.sec or self.nsec > other.nsec
return self.nsec > other.nsec if self.sec == other.sec else self.sec > other.sec

def __lt__(self, other: "Timestamp") -> bool:
return self.sec < other.sec or self.nsec < other.nsec
return self.nsec < other.nsec if self.sec == other.sec else self.sec < other.sec


# Timestamp and exemplar are optional.
Expand Down
4 changes: 4 additions & 0 deletions tests/test_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def test_gt(self):
self.assertEqual(samples.Timestamp(1, 2) > samples.Timestamp(1, 1), True)
self.assertEqual(samples.Timestamp(2, 1) > samples.Timestamp(1, 1), True)
self.assertEqual(samples.Timestamp(2, 2) > samples.Timestamp(1, 1), True)
self.assertEqual(samples.Timestamp(0, 2) > samples.Timestamp(1, 1), False)
self.assertEqual(samples.Timestamp(2, 0) > samples.Timestamp(1, 1), True)

def test_lt(self):
self.assertEqual(samples.Timestamp(1, 1) < samples.Timestamp(1, 1), False)
Expand All @@ -21,6 +23,8 @@ def test_lt(self):
self.assertEqual(samples.Timestamp(1, 2) < samples.Timestamp(1, 1), False)
self.assertEqual(samples.Timestamp(2, 1) < samples.Timestamp(1, 1), False)
self.assertEqual(samples.Timestamp(2, 2) < samples.Timestamp(1, 1), False)
self.assertEqual(samples.Timestamp(0, 2) < samples.Timestamp(1, 1), True)
self.assertEqual(samples.Timestamp(2, 0) < samples.Timestamp(1, 1), False)


if __name__ == '__main__':
Expand Down

0 comments on commit 09a5ae3

Please sign in to comment.