diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2017-05-12 17:57:23 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2017-05-12 17:57:23 +0300 |
commit | e9308c189af388525becbcd42b81ea6d37738135 (patch) | |
tree | 9976c0b87b77e06ae40c52443f2c0d1e0acce7ad /esp8266/modules/ntptime.py | |
parent | 5f7ce2a1ca65840ef95d0c044088bc38453b7471 (diff) | |
download | micropython-e9308c189af388525becbcd42b81ea6d37738135.tar.gz micropython-e9308c189af388525becbcd42b81ea6d37738135.zip |
esp8266/scripts: Move drivers/modules to modules/ (frozen bytecode).
Diffstat (limited to 'esp8266/modules/ntptime.py')
-rw-r--r-- | esp8266/modules/ntptime.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/esp8266/modules/ntptime.py b/esp8266/modules/ntptime.py new file mode 100644 index 0000000000..a97e08e60d --- /dev/null +++ b/esp8266/modules/ntptime.py @@ -0,0 +1,36 @@ +try: + import usocket as socket +except: + import socket +try: + import ustruct as struct +except: + import struct + +# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60 +NTP_DELTA = 3155673600 + +host = "pool.ntp.org" + +def time(): + NTP_QUERY = bytearray(48) + NTP_QUERY[0] = 0x1b + addr = socket.getaddrinfo(host, 123)[0][-1] + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + s.settimeout(1) + res = s.sendto(NTP_QUERY, addr) + msg = s.recv(48) + s.close() + val = struct.unpack("!I", msg[40:44])[0] + return val - NTP_DELTA + +# There's currently no timezone support in MicroPython, so +# utime.localtime() will return UTC time (as if it was .gmtime()) +def settime(): + t = time() + import machine + import utime + tm = utime.localtime(t) + tm = tm[0:3] + (0,) + tm[3:6] + (0,) + machine.RTC().datetime(tm) + print(utime.localtime()) |