@@ -41,6 +41,7 @@ def __init__(
41
41
server : str = "0.adafruit.pool.ntp.org" ,
42
42
port : int = 123 ,
43
43
tz_offset : int = 0 ,
44
+ socket_timeout : int = 10 ,
44
45
) -> None :
45
46
"""
46
47
:param object socketpool: A socket provider such as CPython's `socket` module.
@@ -49,12 +50,14 @@ def __init__(
49
50
:param float tz_offset: Timezone offset in hours from UTC. Only useful for timezone ignorant
50
51
CircuitPython. CPython will determine timezone automatically and adjust (so don't use
51
52
this.) For example, Pacific daylight savings time is -7.
53
+ :param int socket_timeout: UDP socket timeout, in seconds.
52
54
"""
53
55
self ._pool = socketpool
54
56
self ._server = server
55
57
self ._port = port
56
58
self ._packet = bytearray (48 )
57
59
self ._tz_offset = tz_offset * 60 * 60
60
+ self ._socket_timeout = socket_timeout
58
61
59
62
# This is our estimated start time for the monotonic clock. We adjust it based on the ntp
60
63
# responses.
@@ -70,6 +73,7 @@ def datetime(self) -> time.struct_time:
70
73
for i in range (1 , len (self ._packet )):
71
74
self ._packet [i ] = 0
72
75
with self ._pool .socket (self ._pool .AF_INET , self ._pool .SOCK_DGRAM ) as sock :
76
+ sock .settimeout (self ._socket_timeout )
73
77
sock .sendto (self ._packet , (self ._server , self ._port ))
74
78
sock .recvfrom_into (self ._packet )
75
79
# Get the time in the context to minimize the difference between it and receiving
0 commit comments