Skip to content

Commit 1c4456b

Browse files
committed
guard against invalid response
Test to ensure response value is valid. Intermittent seconds=0 value have been seen. fixes adafruit#35
1 parent fc0f07c commit 1c4456b

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

adafruit_ntp.py

+10
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ def datetime(self) -> time.struct_time:
9494
poll = struct.unpack_from("!B", self._packet, offset=2)[0]
9595
self.next_sync = destination + (2**poll) * 1_000_000_000
9696
seconds = struct.unpack_from("!I", self._packet, offset=PACKET_SIZE - 8)[0]
97+
98+
# value should always be larger; giving a small buffer to handle jitter.
99+
if (seconds + 5) < self._monotonic_start:
100+
failed_offset = (self._monotonic_start - seconds) / 1_000_000_000
101+
raise ArithmeticError(
102+
"need a time machine, ntp time is "
103+
+ str(failed_offset)
104+
+ "seconds in the past."
105+
)
106+
97107
self._monotonic_start = (
98108
seconds
99109
+ self._tz_offset

0 commit comments

Comments
 (0)