From 09a5ae30602a7a81f6174dae4ba08b93ee7feed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel=20Garc=C3=ADa?= Date: Tue, 28 May 2024 22:11:34 +0200 Subject: [PATCH] Fix timestamp comparison (#1038) Signed-off-by: Miguel Angel Garcia --- prometheus_client/samples.py | 4 ++-- tests/test_samples.py | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/prometheus_client/samples.py b/prometheus_client/samples.py index 8735fed9..53c47264 100644 --- a/prometheus_client/samples.py +++ b/prometheus_client/samples.py @@ -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. diff --git a/tests/test_samples.py b/tests/test_samples.py index 796afe7e..7b59218b 100644 --- a/tests/test_samples.py +++ b/tests/test_samples.py @@ -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) @@ -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__':