extmod/ntptime: Factor out ntptime module from esp8266 port.

The ntptime module was previously only included in the ESP8266 port.  This
commit factors that module out into the extmod directory, makes it support
different epochs, and includes it in the rp2 port.
This commit is contained in:
Ian Davies 2022-07-06 16:22:57 +01:00 committed by Damien George
parent b560b9fe71
commit fbe9417b90
5 changed files with 16 additions and 6 deletions

View File

@ -1,3 +1,5 @@
import utime
try:
import usocket as socket
except:
@ -7,9 +9,6 @@ try:
except:
import struct
# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
NTP_DELTA = 3155673600
# The NTP host can be configured at runtime by doing: ntptime.host = 'myhost.org'
host = "pool.ntp.org"
@ -26,6 +25,17 @@ def time():
finally:
s.close()
val = struct.unpack("!I", msg[40:44])[0]
EPOCH_YEAR = utime.gmtime(0)[0]
if EPOCH_YEAR == 2000:
# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
NTP_DELTA = 3155673600
elif EPOCH_YEAR == 1970:
# (date(1970, 1, 1) - date(1900, 1, 1)).days * 24*60*60
NTP_DELTA = 2208988800
else:
raise Exception("Unsupported epoch: {}".format(EPOCH_YEAR))
return val - NTP_DELTA
@ -33,7 +43,6 @@ def time():
def settime():
t = time()
import machine
import utime
tm = utime.gmtime(t)
machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))

View File

@ -1,6 +1,6 @@
freeze("$(PORT_DIR)/modules")
freeze("$(MPY_DIR)/tools", ("upip.py", "upip_utarfile.py"))
freeze("$(MPY_DIR)/ports/esp8266/modules", "ntptime.py")
freeze("$(MPY_DIR)/extmod", "ntptime.py")
freeze("$(MPY_DIR)/drivers/dht", "dht.py")
freeze("$(MPY_DIR)/drivers/onewire")
include("$(MPY_DIR)/extmod/uasyncio/manifest.py")

View File

@ -1,5 +1,4 @@
freeze("$(BOARD_DIR)", "_boot.py", opt=3)
freeze("$(PORT_DIR)/modules", ("apa102.py", "ntptime.py", "port_diag.py"))
freeze("$(MPY_DIR)/drivers/dht", "dht.py")
freeze("$(MPY_DIR)/drivers/onewire")
include("$(MPY_DIR)/extmod/webrepl/manifest.py")

View File

@ -1,4 +1,5 @@
freeze("$(PORT_DIR)/modules")
freeze("$(MPY_DIR)/extmod", "ntptime.py")
freeze("$(MPY_DIR)/tools", ("upip.py", "upip_utarfile.py"))
freeze("$(MPY_DIR)/drivers/dht", "dht.py")
freeze("$(MPY_DIR)/drivers/onewire")

View File

@ -2,6 +2,7 @@ include("../manifest.py")
freeze("$(MPY_DIR)/tools", "upip.py")
freeze("$(MPY_DIR)/tools", "upip_utarfile.py")
freeze("$(MPY_DIR)/extmod", "ntptime.py")
if os.path.isdir(convert_path("$(MPY_LIB_DIR)")):
freeze("$(MPY_LIB_DIR)/python-ecosys/urequests", "urequests.py")