8000 extmod/ntptime.py: Factor out ntptime module. · micropython/micropython@4b4843f · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b4843f

Browse files
committed
extmod/ntptime.py: Factor out ntptime module.
The ntptime module was previously only included in the ESP8266 port. This commit factors that module out into the extmod directory.
1 parent 1664939 commit 4b4843f

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
lines changed

ports/esp8266/modules/ntptime.py renamed to extmod/ntptime.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import time
2+
13
try:
24
import usocket as socket
35
except:
@@ -7,9 +9,6 @@
79
except:
810
import struct
911

10-
# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
11-
NTP_DELTA = 3155673600
12-
1312
# The NTP host can be configured at runtime by doing: ntptime.host = 'myhost.org'
1413
host = "pool.ntp.org"
1514

@@ -26,14 +25,26 @@ def time():
2625
finally:
2726
s.close()
2827
val = struct.unpack("!I", msg[40:44])[0]
28+
29+
EPOCH_YEAR = time.gmtime(0)[0]
30+
if EPOCH_YEAR == 2000:
31+
# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
32+
NTP_DELTA = 3155673600
33+
elif EPOCH_YEAR == 1970:
34+
# (date(1970, 1, 1) - date(1900, 1, 1)).days * 24*60*60
35+
NTP_DELTA = 2208988800
36+
else:
37+
raise Exception(
38+
f"Unsupported EPOCH_YEAR provided in this port of MicroPython: {EPOCH_YEAR}. Cannot interpret NTP time."
39+
)
40+
2941
return val - NTP_DELTA
3042

3143

3244
# There's currently no timezone support in MicroPython, and the RTC is set in UTC time.
3345
def settime():
3446
t = time()
3547
import machine
36-
import utime
3748

38-
tm = utime.gmtime(t)
49+
tm = time.gmtime(t)
3950
machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))

ports/esp32/boards/manifest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
freeze("$(PORT_DIR)/modules")
22
freeze("$(MPY_DIR)/tools", ("upip.py", "upip_utarfile.py"))
3-
freeze("$(MPY_DIR)/ports/esp8266/modules", "ntptime.py")
3+
freeze("$(MPY_DIR)/extmod", "ntptime.py")
44
freeze("$(MPY_DIR)/drivers/dht", "dht.py")
55
freeze("$(MPY_DIR)/drivers/onewire")
66
include("$(MPY_DIR)/extmod/uasyncio/manifest.py")

ports/esp8266/boards/GENERIC_512K/manifest.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
freeze("$(BOARD_DIR)", "_boot.py", opt=3)
2-
freeze("$(PORT_DIR)/modules", ("apa102.py", "ntptime.py", "port_diag.py"))
32
freeze("$(MPY_DIR)/drivers/dht", "dht.py")
43
freeze("$(MPY_DIR)/drivers/onewire")
54
include("$(MPY_DIR)/extmod/webrepl/manifest.py")

ports/esp8266/boards/manifest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
freeze("$(PORT_DIR)/modules")
2+
freeze("$(MPY_DIR)/extmod", "ntptime.py")
23
freeze("$(MPY_DIR)/tools", ("upip.py", "upip_utarfile.py"))
34
freeze("$(MPY_DIR)/drivers/dht", "dht.py")
45
freeze("$(MPY_DIR)/drivers/onewire")

ports/rp2/boards/PICO_W/manifest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
freeze("$(MPY_DIR)/tools", "upip.py")
44
freeze("$(MPY_DIR)/tools", "upip_utarfile.py")
5+
freeze("$(MPY_DIR)/extmod", "ntptime.py")
56

67
if os.path.isdir(convert_path("$(MPY_LIB_DIR)")):
78
freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py")

0 commit comments

Comments
 (0)
0