adafruit-circuitpython-ntp 3.0.13__tar.gz → 3.3.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/.readthedocs.yaml +3 -0
  2. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/PKG-INFO +9 -13
  3. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/README.rst +7 -11
  4. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/adafruit_circuitpython_ntp.egg-info/PKG-INFO +9 -13
  5. adafruit_circuitpython_ntp-3.3.2/adafruit_ntp.py +145 -0
  6. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/docs/conf.py +0 -1
  7. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/examples/ntp_connection_manager.py +2 -2
  8. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/examples/ntp_cpython.py +1 -1
  9. adafruit_circuitpython_ntp-3.3.2/examples/ntp_set_rtc.py +37 -0
  10. adafruit_circuitpython_ntp-3.3.2/examples/ntp_simpletest.py +32 -0
  11. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/pyproject.toml +1 -1
  12. adafruit-circuitpython-ntp-3.0.13/adafruit_ntp.py +0 -98
  13. adafruit-circuitpython-ntp-3.0.13/examples/ntp_set_rtc.py +0 -32
  14. adafruit-circuitpython-ntp-3.0.13/examples/ntp_simpletest.py +0 -27
  15. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/.github/PULL_REQUEST_TEMPLATE/adafruit_circuitpython_pr.md +0 -0
  16. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/.github/workflows/build.yml +0 -0
  17. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/.github/workflows/failure-help-text.yml +0 -0
  18. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/.github/workflows/release_gh.yml +0 -0
  19. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/.github/workflows/release_pypi.yml +0 -0
  20. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/.gitignore +0 -0
  21. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/.pre-commit-config.yaml +0 -0
  22. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/.pylintrc +0 -0
  23. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/CODE_OF_CONDUCT.md +0 -0
  24. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/LICENSE +0 -0
  25. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/LICENSES/CC-BY-4.0.txt +0 -0
  26. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/LICENSES/MIT.txt +0 -0
  27. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/LICENSES/Unlicense.txt +0 -0
  28. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/README.rst.license +0 -0
  29. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/adafruit_circuitpython_ntp.egg-info/SOURCES.txt +0 -0
  30. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/adafruit_circuitpython_ntp.egg-info/dependency_links.txt +0 -0
  31. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/adafruit_circuitpython_ntp.egg-info/requires.txt +0 -0
  32. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/adafruit_circuitpython_ntp.egg-info/top_level.txt +0 -0
  33. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/docs/_static/favicon.ico +0 -0
  34. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/docs/_static/favicon.ico.license +0 -0
  35. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/docs/api.rst +0 -0
  36. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/docs/api.rst.license +0 -0
  37. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/docs/examples.rst +0 -0
  38. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/docs/examples.rst.license +0 -0
  39. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/docs/index.rst +0 -0
  40. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/docs/index.rst.license +0 -0
  41. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/docs/requirements.txt +0 -0
  42. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/optional_requirements.txt +0 -0
  43. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/requirements.txt +0 -0
  44. {adafruit-circuitpython-ntp-3.0.13 → adafruit_circuitpython_ntp-3.3.2}/setup.cfg +0 -0
@@ -8,6 +8,9 @@
8
8
  # Required
9
9
  version: 2
10
10
 
11
+ sphinx:
12
+ configuration: docs/conf.py
13
+
11
14
  build:
12
15
  os: ubuntu-20.04
13
16
  tools:
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: adafruit-circuitpython-ntp
3
- Version: 3.0.13
3
+ Version: 3.3.2
4
4
  Summary: Network Time Protocol (NTP) helper for Python
5
5
  Author-email: Adafruit Industries <circuitpython@adafruit.com>
6
6
  License: MIT
@@ -78,22 +78,18 @@ Usage Example
78
78
 
79
79
  .. code-block:: python
80
80
 
81
+ import adafruit_connection_manager
81
82
  import adafruit_ntp
82
- import socketpool
83
+ import os
83
84
  import time
84
85
  import wifi
85
86
 
86
- # Get wifi details and more from a secrets.py file
87
- try:
88
- from secrets import secrets
89
- except ImportError:
90
- print("WiFi secrets are kept in secrets.py, please add them there!")
91
- raise
87
+ wifi_ssid = os.getenv("CIRCUITPY_WIFI_SSID")
88
+ wifi_password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
89
+ wifi.radio.connect(wifi_ssid, wifi_password)
92
90
 
93
- wifi.radio.connect(secrets["ssid"], secrets["password"])
94
-
95
- pool = socketpool.SocketPool(wifi.radio)
96
- ntp = adafruit_ntp.NTP(pool, tz_offset=0)
91
+ pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
92
+ ntp = adafruit_ntp.NTP(pool, tz_offset=0, cache_seconds=3600)
97
93
 
98
94
  while True:
99
95
  print(ntp.datetime)
@@ -59,22 +59,18 @@ Usage Example
59
59
 
60
60
  .. code-block:: python
61
61
 
62
+ import adafruit_connection_manager
62
63
  import adafruit_ntp
63
- import socketpool
64
+ import os
64
65
  import time
65
66
  import wifi
66
67
 
67
- # Get wifi details and more from a secrets.py file
68
- try:
69
- from secrets import secrets
70
- except ImportError:
71
- print("WiFi secrets are kept in secrets.py, please add them there!")
72
- raise
68
+ wifi_ssid = os.getenv("CIRCUITPY_WIFI_SSID")
69
+ wifi_password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
70
+ wifi.radio.connect(wifi_ssid, wifi_password)
73
71
 
74
- wifi.radio.connect(secrets["ssid"], secrets["password"])
75
-
76
- pool = socketpool.SocketPool(wifi.radio)
77
- ntp = adafruit_ntp.NTP(pool, tz_offset=0)
72
+ pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
73
+ ntp = adafruit_ntp.NTP(pool, tz_offset=0, cache_seconds=3600)
78
74
 
79
75
  while True:
80
76
  print(ntp.datetime)
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: adafruit-circuitpython-ntp
3
- Version: 3.0.13
3
+ Version: 3.3.2
4
4
  Summary: Network Time Protocol (NTP) helper for Python
5
5
  Author-email: Adafruit Industries <circuitpython@adafruit.com>
6
6
  License: MIT
@@ -78,22 +78,18 @@ Usage Example
78
78
 
79
79
  .. code-block:: python
80
80
 
81
+ import adafruit_connection_manager
81
82
  import adafruit_ntp
82
- import socketpool
83
+ import os
83
84
  import time
84
85
  import wifi
85
86
 
86
- # Get wifi details and more from a secrets.py file
87
- try:
88
- from secrets import secrets
89
- except ImportError:
90
- print("WiFi secrets are kept in secrets.py, please add them there!")
91
- raise
87
+ wifi_ssid = os.getenv("CIRCUITPY_WIFI_SSID")
88
+ wifi_password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
89
+ wifi.radio.connect(wifi_ssid, wifi_password)
92
90
 
93
- wifi.radio.connect(secrets["ssid"], secrets["password"])
94
-
95
- pool = socketpool.SocketPool(wifi.radio)
96
- ntp = adafruit_ntp.NTP(pool, tz_offset=0)
91
+ pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio)
92
+ ntp = adafruit_ntp.NTP(pool, tz_offset=0, cache_seconds=3600)
97
93
 
98
94
  while True:
99
95
  print(ntp.datetime)
@@ -0,0 +1,145 @@
1
+ # SPDX-FileCopyrightText: 2022 Scott Shawcroft for Adafruit Industries
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+
5
+ """
6
+ `adafruit_ntp`
7
+ ================================================================================
8
+
9
+ Network Time Protocol (NTP) helper for CircuitPython
10
+
11
+ * Author(s): Scott Shawcroft
12
+
13
+ Implementation Notes
14
+ --------------------
15
+ **Hardware:**
16
+ **Software and Dependencies:**
17
+
18
+ * Adafruit CircuitPython firmware for the supported boards:
19
+ https://github.com/adafruit/circuitpython/releases
20
+
21
+ """
22
+ import struct
23
+ import time
24
+
25
+ from micropython import const
26
+
27
+
28
+ __version__ = "3.3.2"
29
+ __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NTP.git"
30
+
31
+ NTP_TO_UNIX_EPOCH = 2208988800 # 1970-01-01 00:00:00
32
+ PACKET_SIZE = const(48)
33
+
34
+
35
+ class NTP: # pylint:disable=too-many-instance-attributes
36
+ """Network Time Protocol (NTP) helper module for CircuitPython.
37
+ This module does not handle daylight savings or local time. It simply requests
38
+ UTC from a NTP server.
39
+ """
40
+
41
+ def __init__( # pylint:disable=too-many-arguments
42
+ self,
43
+ socketpool,
44
+ *,
45
+ server: str = "0.adafruit.pool.ntp.org",
46
+ port: int = 123,
47
+ tz_offset: float = 0,
48
+ socket_timeout: int = 10,
49
+ cache_seconds: int = 0,
50
+ ) -> None:
51
+ """
52
+ :param object socketpool: A socket provider such as CPython's `socket` module.
53
+ :param str server: The domain of the ntp server to query.
54
+ :param int port: The port of the ntp server to query.
55
+ :param float tz_offset: Timezone offset in hours from UTC. Only useful for timezone ignorant
56
+ CircuitPython. CPython will determine timezone automatically and adjust (so don't use
57
+ this.) For example, Pacific daylight savings time is -7.
58
+ :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).
61
+ """
62
+ self._pool = socketpool
63
+ self._server = server
64
+ self._port = port
65
+ self._socket_address = None
66
+ self._packet = bytearray(PACKET_SIZE)
67
+ self._tz_offset = int(tz_offset * 60 * 60)
68
+ self._socket_timeout = socket_timeout
69
+ self._cache_seconds = cache_seconds
70
+
71
+ # This is our estimated start time for the monotonic clock. We adjust it based on the ntp
72
+ # responses.
73
+ self._monotonic_start_ns = 0
74
+
75
+ self.next_sync = 0
76
+
77
+ def _update_time_sync(self) -> None:
78
+ """Update the time sync value. Raises OSError exception if no response
79
+ is received within socket_timeout seconds, ArithmeticError for substantially incorrect
80
+ NTP results."""
81
+ if self._socket_address is None:
82
+ self._socket_address = self._pool.getaddrinfo(self._server, self._port)[0][
83
+ 4
84
+ ]
85
+
86
+ self._packet[0] = 0b00100011 # Not leap second, NTP version 4, Client mode
87
+ for i in range(1, PACKET_SIZE):
88
+ self._packet[i] = 0
89
+ with self._pool.socket(self._pool.AF_INET, self._pool.SOCK_DGRAM) as sock:
90
+ sock.settimeout(self._socket_timeout)
91
+ local_send_ns = time.monotonic_ns() # expanded
92
+ sock.sendto(self._packet, self._socket_address)
93
+ sock.recv_into(self._packet)
94
+ # Get the time in the context to minimize the difference between it and receiving
95
+ # the packet.
96
+ local_recv_ns = time.monotonic_ns() # was destination
97
+
98
+ poll = struct.unpack_from("!B", self._packet, offset=2)[0]
99
+
100
+ cache_offset_s = max(2**poll, self._cache_seconds)
101
+ self.next_sync = local_recv_ns + cache_offset_s * 1_000_000_000
102
+
103
+ srv_recv_s, srv_recv_f = struct.unpack_from("!II", self._packet, offset=32)
104
+ srv_send_s, srv_send_f = struct.unpack_from("!II", self._packet, offset=40)
105
+
106
+ # Convert the server times from NTP to UTC for local use
107
+ srv_recv_ns = (srv_recv_s - NTP_TO_UNIX_EPOCH) * 1_000_000_000 + (
108
+ srv_recv_f * 1_000_000_000 // 2**32
109
+ )
110
+ srv_send_ns = (srv_send_s - NTP_TO_UNIX_EPOCH) * 1_000_000_000 + (
111
+ srv_send_f * 1_000_000_000 // 2**32
112
+ )
113
+
114
+ # _round_trip_delay = (local_recv_ns - local_send_ns) - (srv_send_ns - srv_recv_ns)
115
+ # Calculate (best estimate) offset between server UTC and board monotonic_ns time
116
+ clock_offset = (
117
+ (srv_recv_ns - local_send_ns) + (srv_send_ns - local_recv_ns)
118
+ ) // 2
119
+
120
+ self._monotonic_start_ns = clock_offset + self._tz_offset * 1_000_000_000
121
+
122
+ @property
123
+ def datetime(self) -> time.struct_time:
124
+ """Current time from NTP server. Accessing this property causes the NTP time request,
125
+ unless there has already been a recent request."""
126
+ if time.monotonic_ns() > self.next_sync:
127
+ self._update_time_sync()
128
+
129
+ # Calculate the current time based on the current and start monotonic times
130
+ current_time_s = (
131
+ time.monotonic_ns() + self._monotonic_start_ns
132
+ ) // 1_000_000_000
133
+
134
+ return time.localtime(current_time_s)
135
+
136
+ @property
137
+ def utc_ns(self) -> int:
138
+ """UTC (unix epoch) time in nanoseconds. Accessing this property causes the NTP time
139
+ request, unless there has already been a recent request. Raises OSError exception if
140
+ no response is received within socket_timeout seconds, ArithmeticError for substantially
141
+ incorrect NTP results."""
142
+ if time.monotonic_ns() > self.next_sync:
143
+ self._update_time_sync()
144
+
145
+ return time.monotonic_ns() + self._monotonic_start_ns
@@ -103,7 +103,6 @@ napoleon_numpy_docstring = False
103
103
  import sphinx_rtd_theme
104
104
 
105
105
  html_theme = "sphinx_rtd_theme"
106
- html_theme_path = [sphinx_rtd_theme.get_html_theme_path(), "."]
107
106
 
108
107
  # Add any paths that contain custom static files (such as style sheets) here,
109
108
  # relative to this directory. They are copied after the builtin static files,
@@ -33,7 +33,7 @@ except ImportError:
33
33
  # get the socket pool from connection manager
34
34
  socket = adafruit_connection_manager.get_radio_socketpool(radio)
35
35
 
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)
38
38
 
39
39
  print(ntp.datetime)
@@ -9,7 +9,7 @@ import time
9
9
  import adafruit_ntp
10
10
 
11
11
  # 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)
13
13
 
14
14
  while True:
15
15
  print(ntp.datetime)
@@ -0,0 +1,37 @@
1
+ # SPDX-FileCopyrightText: 2022 Scott Shawcroft for Adafruit Industries
2
+ # SPDX-License-Identifier: MIT
3
+
4
+ """Example demonstrating how to set the realtime clock (RTC) based on NTP time."""
5
+
6
+ import os
7
+ import time
8
+
9
+ import rtc
10
+ import socketpool
11
+ import wifi
12
+
13
+ import adafruit_ntp
14
+
15
+ # Get wifi AP credentials from a settings.toml file
16
+ wifi_ssid = os.getenv("CIRCUITPY_WIFI_SSID")
17
+ wifi_password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
18
+ if wifi_ssid is None:
19
+ print("WiFi credentials are kept in settings.toml, please add them there!")
20
+ raise ValueError("SSID not found in environment variables")
21
+
22
+ try:
23
+ wifi.radio.connect(wifi_ssid, wifi_password)
24
+ except ConnectionError:
25
+ print("Failed to connect to WiFi with provided credentials")
26
+ raise
27
+
28
+ pool = socketpool.SocketPool(wifi.radio)
29
+ ntp = adafruit_ntp.NTP(pool, tz_offset=0, cache_seconds=3600)
30
+
31
+ # NOTE: This changes the system time so make sure you aren't assuming that time
32
+ # doesn't jump.
33
+ rtc.RTC().datetime = ntp.datetime
34
+
35
+ while True:
36
+ print(time.localtime())
37
+ time.sleep(1)
@@ -0,0 +1,32 @@
1
+ # SPDX-FileCopyrightText: 2022 Scott Shawcroft for Adafruit Industries
2
+ # SPDX-License-Identifier: MIT
3
+
4
+ """Print out time based on NTP."""
5
+
6
+ import os
7
+ import time
8
+
9
+ import socketpool
10
+ import wifi
11
+
12
+ import adafruit_ntp
13
+
14
+ # Get wifi AP credentials from a settings.toml file
15
+ wifi_ssid = os.getenv("CIRCUITPY_WIFI_SSID")
16
+ wifi_password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
17
+ if wifi_ssid is None:
18
+ print("WiFi credentials are kept in settings.toml, please add them there!")
19
+ raise ValueError("SSID not found in environment variables")
20
+
21
+ try:
22
+ wifi.radio.connect(wifi_ssid, wifi_password)
23
+ except ConnectionError:
24
+ print("Failed to connect to WiFi with provided credentials")
25
+ raise
26
+
27
+ pool = socketpool.SocketPool(wifi.radio)
28
+ ntp = adafruit_ntp.NTP(pool, tz_offset=0, cache_seconds=3600)
29
+
30
+ while True:
31
+ print(ntp.datetime)
32
+ time.sleep(1)
@@ -12,7 +12,7 @@ requires = [
12
12
  [project]
13
13
  name = "adafruit-circuitpython-ntp"
14
14
  description = "Network Time Protocol (NTP) helper for Python"
15
- version = "3.0.13"
15
+ version = "3.3.2"
16
16
  readme = "README.rst"
17
17
  authors = [
18
18
  {name = "Adafruit Industries", email = "circuitpython@adafruit.com"}
@@ -1,98 +0,0 @@
1
- # SPDX-FileCopyrightText: 2022 Scott Shawcroft for Adafruit Industries
2
- #
3
- # SPDX-License-Identifier: MIT
4
-
5
- """
6
- `adafruit_ntp`
7
- ================================================================================
8
-
9
- Network Time Protocol (NTP) helper for CircuitPython
10
-
11
- * Author(s): Scott Shawcroft
12
-
13
- Implementation Notes
14
- --------------------
15
- **Hardware:**
16
- **Software and Dependencies:**
17
-
18
- * Adafruit CircuitPython firmware for the supported boards:
19
- https://github.com/adafruit/circuitpython/releases
20
-
21
- """
22
- import struct
23
- import time
24
-
25
- __version__ = "3.0.13"
26
- __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NTP.git"
27
-
28
- NTP_TO_UNIX_EPOCH = 2208988800 # 1970-01-01 00:00:00
29
-
30
-
31
- class NTP:
32
- """Network Time Protocol (NTP) helper module for CircuitPython.
33
- This module does not handle daylight savings or local time. It simply requests
34
- UTC from a NTP server.
35
- """
36
-
37
- def __init__(
38
- self,
39
- socketpool,
40
- *,
41
- server: str = "0.adafruit.pool.ntp.org",
42
- port: int = 123,
43
- tz_offset: float = 0,
44
- socket_timeout: int = 10,
45
- ) -> None:
46
- """
47
- :param object socketpool: A socket provider such as CPython's `socket` module.
48
- :param str server: The domain of the ntp server to query.
49
- :param int port: The port of the ntp server to query.
50
- :param float tz_offset: Timezone offset in hours from UTC. Only useful for timezone ignorant
51
- CircuitPython. CPython will determine timezone automatically and adjust (so don't use
52
- this.) For example, Pacific daylight savings time is -7.
53
- :param int socket_timeout: UDP socket timeout, in seconds.
54
- """
55
- self._pool = socketpool
56
- self._server = server
57
- self._port = port
58
- self._packet = bytearray(48)
59
- self._tz_offset = int(tz_offset * 60 * 60)
60
- self._socket_timeout = socket_timeout
61
-
62
- # This is our estimated start time for the monotonic clock. We adjust it based on the ntp
63
- # responses.
64
- self._monotonic_start = 0
65
-
66
- self.next_sync = 0
67
-
68
- @property
69
- def datetime(self) -> time.struct_time:
70
- """Current time from NTP server. Accessing this property causes the NTP time request,
71
- unless there has already been a recent request. Raises OSError exception if no response
72
- is received within socket_timeout seconds"""
73
- if time.monotonic_ns() > self.next_sync:
74
- self._packet[0] = 0b00100011 # Not leap second, NTP version 4, Client mode
75
- for i in range(1, len(self._packet)):
76
- self._packet[i] = 0
77
- with self._pool.socket(self._pool.AF_INET, self._pool.SOCK_DGRAM) as sock:
78
- sock.settimeout(self._socket_timeout)
79
- sock.sendto(self._packet, (self._server, self._port))
80
- sock.recvfrom_into(self._packet)
81
- # Get the time in the context to minimize the difference between it and receiving
82
- # the packet.
83
- destination = time.monotonic_ns()
84
- poll = struct.unpack_from("!B", self._packet, offset=2)[0]
85
- self.next_sync = destination + (2**poll) * 1_000_000_000
86
- seconds = struct.unpack_from(
87
- "!I", self._packet, offset=len(self._packet) - 8
88
- )[0]
89
- self._monotonic_start = (
90
- seconds
91
- + self._tz_offset
92
- - NTP_TO_UNIX_EPOCH
93
- - (destination // 1_000_000_000)
94
- )
95
-
96
- return time.localtime(
97
- time.monotonic_ns() // 1_000_000_000 + self._monotonic_start
98
- )
@@ -1,32 +0,0 @@
1
- # SPDX-FileCopyrightText: 2022 Scott Shawcroft for Adafruit Industries
2
- # SPDX-License-Identifier: MIT
3
-
4
- """Example demonstrating how to set the realtime clock (RTC) based on NTP time."""
5
-
6
- import time
7
-
8
- import rtc
9
- import socketpool
10
- import wifi
11
-
12
- import adafruit_ntp
13
-
14
- # Get wifi details and more from a secrets.py file
15
- try:
16
- from secrets import secrets
17
- except ImportError:
18
- print("WiFi secrets are kept in secrets.py, please add them there!")
19
- raise
20
-
21
- wifi.radio.connect(secrets["ssid"], secrets["password"])
22
-
23
- pool = socketpool.SocketPool(wifi.radio)
24
- ntp = adafruit_ntp.NTP(pool, tz_offset=0)
25
-
26
- # NOTE: This changes the system time so make sure you aren't assuming that time
27
- # doesn't jump.
28
- rtc.RTC().datetime = ntp.datetime
29
-
30
- while True:
31
- print(time.localtime())
32
- time.sleep(1)
@@ -1,27 +0,0 @@
1
- # SPDX-FileCopyrightText: 2022 Scott Shawcroft for Adafruit Industries
2
- # SPDX-License-Identifier: MIT
3
-
4
- """Print out time based on NTP."""
5
-
6
- import time
7
-
8
- import socketpool
9
- import wifi
10
-
11
- import adafruit_ntp
12
-
13
- # Get wifi details and more from a secrets.py file
14
- try:
15
- from secrets import secrets
16
- except ImportError:
17
- print("WiFi secrets are kept in secrets.py, please add them there!")
18
- raise
19
-
20
- wifi.radio.connect(secrets["ssid"], secrets["password"])
21
-
22
- pool = socketpool.SocketPool(wifi.radio)
23
- ntp = adafruit_ntp.NTP(pool, tz_offset=0)
24
-
25
- while True:
26
- print(ntp.datetime)
27
- time.sleep(1)