Skip to content

Commit f2495e3

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

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

adafruit_ntp.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ def datetime(self) -> time.struct_time:
102102
self.next_sync = destination + cache_offset * 1_000_000_000
103103
seconds = struct.unpack_from("!I", self._packet, offset=PACKET_SIZE - 8)[0]
104104

105+
# value should always be larger; giving a small buffer to handle jitter.
106+
if (seconds + 5) < self._monotonic_start:
107+
failed_offset = (self._monotonic_start - seconds) / 1_000_000_000
108+
raise ArithmeticError(
109+
"need a time machine, ntp time is "
110+
+ str(failed_offset)
111+
+ "seconds in the past."
112+
)
113+
105114
self._monotonic_start = (
106115
seconds
107116
+ self._tz_offset

0 commit comments

Comments
 (0)