Skip to content

Commit 09a5ae3

Browse files
authored
Fix timestamp comparison (#1038)
Signed-off-by: Miguel Angel Garcia <[email protected]>
1 parent e364a96 commit 09a5ae3

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

prometheus_client/samples.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def __ne__(self, other: object) -> bool:
2828
return not self == other
2929

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

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

3636

3737
# Timestamp and exemplar are optional.

tests/test_samples.py

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ def test_gt(self):
1212
self.assertEqual(samples.Timestamp(1, 2) > samples.Timestamp(1, 1), True)
1313
self.assertEqual(samples.Timestamp(2, 1) > samples.Timestamp(1, 1), True)
1414
self.assertEqual(samples.Timestamp(2, 2) > samples.Timestamp(1, 1), True)
15+
self.assertEqual(samples.Timestamp(0, 2) > samples.Timestamp(1, 1), False)
16+
self.assertEqual(samples.Timestamp(2, 0) > samples.Timestamp(1, 1), True)
1517

1618
def test_lt(self):
1719
self.assertEqual(samples.Timestamp(1, 1) < samples.Timestamp(1, 1), False)
@@ -21,6 +23,8 @@ def test_lt(self):
2123
self.assertEqual(samples.Timestamp(1, 2) < samples.Timestamp(1, 1), False)
2224
self.assertEqual(samples.Timestamp(2, 1) < samples.Timestamp(1, 1), False)
2325
self.assertEqual(samples.Timestamp(2, 2) < samples.Timestamp(1, 1), False)
26+
self.assertEqual(samples.Timestamp(0, 2) < samples.Timestamp(1, 1), True)
27+
self.assertEqual(samples.Timestamp(2, 0) < samples.Timestamp(1, 1), False)
2428

2529

2630
if __name__ == '__main__':

0 commit comments

Comments
 (0)