From f2495e37981bddf270444802449afd5872d3102a Mon Sep 17 00:00:00 2001 From: ted Date: Tue, 16 Jul 2024 14:46:14 -0700 Subject: [PATCH 1/2] guard against invalid response Test to ensure response value is valid. Intermittent seconds=0 value have been seen. fixes #35 --- adafruit_ntp.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/adafruit_ntp.py b/adafruit_ntp.py index bc6db0d..d1b29af 100644 --- a/adafruit_ntp.py +++ b/adafruit_ntp.py @@ -102,6 +102,15 @@ def datetime(self) -> time.struct_time: self.next_sync = destination + cache_offset * 1_000_000_000 seconds = struct.unpack_from("!I", self._packet, offset=PACKET_SIZE - 8)[0] + # value should always be larger; giving a small buffer to handle jitter. + if (seconds + 5) < self._monotonic_start: + failed_offset = (self._monotonic_start - seconds) / 1_000_000_000 + raise ArithmeticError( + "need a time machine, ntp time is " + + str(failed_offset) + + "seconds in the past." + ) + self._monotonic_start = ( seconds + self._tz_offset From a923944b8b56f448c03f9567deb75bc272361421 Mon Sep 17 00:00:00 2001 From: ted Date: Mon, 22 Jul 2024 19:57:10 -0700 Subject: [PATCH 2/2] PR feedback, test zero --- adafruit_ntp.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/adafruit_ntp.py b/adafruit_ntp.py index d1b29af..5d71a72 100644 --- a/adafruit_ntp.py +++ b/adafruit_ntp.py @@ -103,13 +103,8 @@ def datetime(self) -> time.struct_time: seconds = struct.unpack_from("!I", self._packet, offset=PACKET_SIZE - 8)[0] # value should always be larger; giving a small buffer to handle jitter. - if (seconds + 5) < self._monotonic_start: - failed_offset = (self._monotonic_start - seconds) / 1_000_000_000 - raise ArithmeticError( - "need a time machine, ntp time is " - + str(failed_offset) - + "seconds in the past." - ) + if seconds == 0: + raise ArithmeticError("ntp value is invalid (empty)") self._monotonic_start = ( seconds