Skip to content

Commit

Permalink
extmod/ntptime: Factor out ntptime module from esp8266 port.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
daviesian authored and dpgeorge committed Aug 5, 2022
1 parent b560b9f commit fbe9417
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
17 changes: 13 additions & 4 deletions ports/esp8266/modules/ntptime.py → extmod/ntptime.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import utime

try:
import usocket as socket
except:
Expand All @@ -7,9 +9,6 @@
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"

Expand All @@ -26,14 +25,24 @@ 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


# There's currently no timezone support in MicroPython, and the RTC is set in UTC 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))
2 changes: 1 addition & 1 deletion ports/esp32/boards/manifest.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
1 change: 0 additions & 1 deletion ports/esp8266/boards/GENERIC_512K/manifest.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
1 change: 1 addition & 0 deletions ports/esp8266/boards/manifest.py
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
1 change: 1 addition & 0 deletions ports/rp2/boards/PICO_W/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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")

0 comments on commit fbe9417

Please sign in to comment.