esp8266/modules/ntptime.py: Always close socket, and set day-of-week.

Fixes issue #5189.
This commit is contained in:
Damien George 2019-10-10 18:05:56 +11:00
parent 580a2656d1
commit 79ab82ea77
1 changed files with 7 additions and 6 deletions

View File

@ -17,9 +17,11 @@ def time():
NTP_QUERY[0] = 0x1b
addr = socket.getaddrinfo(host, 123)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.settimeout(1)
res = s.sendto(NTP_QUERY, addr)
msg = s.recv(48)
finally:
s.close()
val = struct.unpack("!I", msg[40:44])[0]
return val - NTP_DELTA
@ -31,5 +33,4 @@ def settime():
import machine
import utime
tm = utime.localtime(t)
tm = tm[0:3] + (0,) + tm[3:6] + (0,)
machine.RTC().datetime(tm)
machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))