Skip to content

Commit 18c60dc

Browse files
authored
Merge pull request #37 from tedder/ted/configurable_cache_time_2
add configurable time between NTP calls
2 parents fc0f07c + 9a4dbe7 commit 18c60dc

File tree

6 files changed

+16
-8
lines changed

6 files changed

+16
-8
lines changed

README.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Usage Example
7070
wifi.radio.connect(wifi_ssid, wifi_password)
7171
7272
pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
73-
ntp = adafruit_ntp.NTP(pool, tz_offset=0)
73+
ntp = adafruit_ntp.NTP(pool, tz_offset=0, cache_seconds=3600)
7474
7575
while True:
7676
print(ntp.datetime)

adafruit_ntp.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def __init__(
4646
port: int = 123,
4747
tz_offset: float = 0,
4848
socket_timeout: int = 10,
49+
cache_seconds: int = 0,
4950
) -> None:
5051
"""
5152
:param object socketpool: A socket provider such as CPython's `socket` module.
@@ -55,6 +56,8 @@ def __init__(
5556
CircuitPython. CPython will determine timezone automatically and adjust (so don't use
5657
this.) For example, Pacific daylight savings time is -7.
5758
:param int socket_timeout: UDP socket timeout, in seconds.
59+
:param int cache_seconds: how many seconds to use a cached result from NTP server
60+
(default 0, which respects NTP server's minimum).
5861
"""
5962
self._pool = socketpool
6063
self._server = server
@@ -63,6 +66,7 @@ def __init__(
6366
self._packet = bytearray(PACKET_SIZE)
6467
self._tz_offset = int(tz_offset * 60 * 60)
6568
self._socket_timeout = socket_timeout
69+
self._cache_seconds = cache_seconds
6670

6771
# This is our estimated start time for the monotonic clock. We adjust it based on the ntp
6872
# responses.
@@ -74,7 +78,8 @@ def __init__(
7478
def datetime(self) -> time.struct_time:
7579
"""Current time from NTP server. Accessing this property causes the NTP time request,
7680
unless there has already been a recent request. Raises OSError exception if no response
77-
is received within socket_timeout seconds"""
81+
is received within socket_timeout seconds, ArithmeticError for substantially incorrect
82+
NTP results."""
7883
if time.monotonic_ns() > self.next_sync:
7984
if self._socket_address is None:
8085
self._socket_address = self._pool.getaddrinfo(self._server, self._port)[
@@ -92,8 +97,11 @@ def datetime(self) -> time.struct_time:
9297
# the packet.
9398
destination = time.monotonic_ns()
9499
poll = struct.unpack_from("!B", self._packet, offset=2)[0]
95-
self.next_sync = destination + (2**poll) * 1_000_000_000
100+
101+
cache_offset = max(2**poll, self._cache_seconds)
102+
self.next_sync = destination + cache_offset * 1_000_000_000
96103
seconds = struct.unpack_from("!I", self._packet, offset=PACKET_SIZE - 8)[0]
104+
97105
self._monotonic_start = (
98106
seconds
99107
+ self._tz_offset

examples/ntp_connection_manager.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
# get the socket pool from connection manager
3434
socket = adafruit_connection_manager.get_radio_socketpool(radio)
3535

36-
# adjust tz_offset for locale...
37-
ntp = adafruit_ntp.NTP(socket, tz_offset=-5)
36+
# adjust tz_offset for locale, only ping NTP server every hour
37+
ntp = adafruit_ntp.NTP(socket, tz_offset=-5, cache_seconds=3600)
3838

3939
print(ntp.datetime)

examples/ntp_cpython.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import adafruit_ntp
1010

1111
# Don't use tz_offset kwarg with CPython because it will adjust automatically.
12-
ntp = adafruit_ntp.NTP(socket)
12+
ntp = adafruit_ntp.NTP(socket, cache_seconds=3600)
1313

1414
while True:
1515
print(ntp.datetime)

examples/ntp_set_rtc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
wifi.radio.connect(secrets["ssid"], secrets["password"])
2222

2323
pool = socketpool.SocketPool(wifi.radio)
24-
ntp = adafruit_ntp.NTP(pool, tz_offset=0)
24+
ntp = adafruit_ntp.NTP(pool, tz_offset=0, cache_seconds=3600)
2525

2626
# NOTE: This changes the system time so make sure you aren't assuming that time
2727
# doesn't jump.

examples/ntp_simpletest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
wifi.radio.connect(secrets["ssid"], secrets["password"])
2121

2222
pool = socketpool.SocketPool(wifi.radio)
23-
ntp = adafruit_ntp.NTP(pool, tz_offset=0)
23+
ntp = adafruit_ntp.NTP(pool, tz_offset=0, cache_seconds=3600)
2424

2525
while True:
2626
print(ntp.datetime)

0 commit comments

Comments
 (0)