Skip to content

Commit c4ec11f

Browse files
committed
run pre-commit
1 parent 35b75bd commit c4ec11f

File tree

5 files changed

+38
-17
lines changed

5 files changed

+38
-17
lines changed

adafruit_ntp.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,28 @@
2727

2828
NTP_TO_UNIX_EPOCH = 2208988800 # 1970-01-01 00:00:00
2929

30+
3031
class NTP:
3132
"""Network Time Protocol (NTP) helper module for CircuitPython.
3233
This module does not handle daylight savings or local time. It simply requests
3334
UTC from a NTP server.
3435
"""
3536

36-
def __init__(self, socketpool, *, server: str="0.adafruit.pool.ntp.org", port: int = 123, tz_offset: int = 0) -> None:
37+
def __init__(
38+
self,
39+
socketpool,
40+
*,
41+
server: str = "0.adafruit.pool.ntp.org",
42+
port: int = 123,
43+
tz_offset: int = 0,
44+
) -> None:
3745
"""
3846
:param object socketpool: A socket provider such as CPython's `socket` module.
3947
:param str server: The domain of the ntp server to query.
4048
:param int port: The port of the ntp server to query.
4149
:param float tz_offset: Timezone offset in hours from UTC. Only useful for timezone ignorant
42-
CircuitPython. CPython will determine timezone automatically and adjust (so don't use this.)
43-
For example, Pacific daylight savings time is -7.
50+
CircuitPython. CPython will determine timezone automatically and adjust (so don't use
51+
this.) For example, Pacific daylight savings time is -7.
4452
"""
4553
self._pool = socketpool
4654
self._server = server
@@ -56,19 +64,29 @@ def __init__(self, socketpool, *, server: str="0.adafruit.pool.ntp.org", port: i
5664

5765
@property
5866
def datetime(self) -> time.struct_time:
67+
"""Current time from NTP server."""
5968
if time.monotonic_ns() > self.next_sync:
6069
self._packet[0] = 0b00100011 # Not leap second, NTP version 4, Client mode
6170
for i in range(1, len(self._packet)):
6271
self._packet[i] = 0
6372
with self._pool.socket(self._pool.AF_INET, self._pool.SOCK_DGRAM) as sock:
6473
sock.sendto(self._packet, (self._server, self._port))
65-
size, address = sock.recvfrom_into(self._packet)
74+
sock.recvfrom_into(self._packet)
6675
# Get the time in the context to minimize the difference between it and receiving
6776
# the packet.
6877
destination = time.monotonic_ns()
6978
poll = struct.unpack_from("!B", self._packet, offset=2)[0]
70-
self.next_sync = destination + (2 ** poll) * 1_000_000_000
71-
seconds = struct.unpack_from("!I", self._packet, offset=len(self._packet) - 8)[0]
72-
self._monotonic_start = seconds + self._tz_offset - NTP_TO_UNIX_EPOCH - (destination // 1_000_000_000)
79+
self.next_sync = destination + (2**poll) * 1_000_000_000
80+
seconds = struct.unpack_from(
81+
"!I", self._packet, offset=len(self._packet) - 8
82+
)[0]
83+
self._monotonic_start = (
84+
seconds
85+
+ self._tz_offset
86+
- NTP_TO_UNIX_EPOCH
87+
- (destination // 1_000_000_000)
88+
)
7389

74-
return time.localtime(time.monotonic_ns() // 1_000_000_000 + self._monotonic_start)
90+
return time.localtime(
91+
time.monotonic_ns() // 1_000_000_000 + self._monotonic_start
92+
)

examples/ntp_cpython.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
"""Tests NTP with CPython socket"""
2-
31
# SPDX-FileCopyrightText: 2022 Scott Shawcroft for Adafruit Industries
42
# SPDX-License-Identifier: MIT
53

6-
import adafruit_ntp
4+
"""Tests NTP with CPython socket"""
5+
76
import socket
87
import time
98

9+
import adafruit_ntp
10+
1011
# Don't use tz_offset kwarg with CPython because it will adjust automatically.
1112
ntp = adafruit_ntp.NTP(socket)
1213

examples/ntp_set_rtc.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33

44
"""Example demonstrating how to set the realtime clock (RTC) based on NTP time."""
55

6-
import adafruit_ntp
6+
import time
7+
78
import rtc
89
import socketpool
9-
import time
1010
import wifi
1111

12+
import adafruit_ntp
13+
1214
# Get wifi details and more from a secrets.py file
1315
try:
1416
from secrets import secrets

examples/ntp_simpletest.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33

44
"""Print out time based on NTP."""
55

6-
import adafruit_ntp
7-
import socketpool
86
import time
7+
8+
import socketpool
99
import wifi
1010

11+
import adafruit_ntp
12+
1113
# Get wifi details and more from a secrets.py file
1214
try:
1315
from secrets import secrets
@@ -23,4 +25,3 @@
2325
while True:
2426
print(ntp.datetime)
2527
time.sleep(1)
26-

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
22
#
33
# SPDX-License-Identifier: Unlicense
4-

0 commit comments

Comments
 (0)