先吐槽micropython的官方文档。很多东西压根就没交代,比如这里用到的ntptime库,其实是很方便的,但只是在《Quick reference for the ESP8266》里出现到了一次。
先说直接获得本地时间(东八区UTC+8)用法,只需要两三行:
1 2 3 4 5 6 7 8 | import ntptime def sync_ntp(): ntptime.NTP_DELTA = 3155644800 # 可选 UTC+8偏移时间(秒),不设置就是UTC0 ntptime.host = 'ntp1.aliyun.com' # 可选,ntp服务器,默认是"pool.ntp.org" ntptime.settime() # 修改设备时间 sync_ntp() print(rtc.datetime()) |
最后按需求加一个定时的任务就行了。
——————————到此就结束了,下面吧啦吧啦—————————
官方的用法示例:
1 2 3 4 5 6 7 8 9 10 11 | from machine import RTC rtc = RTC() rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time rtc.datetime() # get date and time # synchronize with ntp # need to be connected to wifi import ntptime ntptime.settime() # set the rtc datetime from the remote server rtc.datetime() # get the date and time in UTC |
本来ntptime是不支时区的,原打算自己写一个减8小时的方法,因为要考虑到周、月、闰月的变化,其实还挺麻烦,所以到github上查了ntptime的源码。
这里有一个NTP_DELTA的常量和host的变量。
注释很明确,host服务器可以自己改。
然后这个NTP_DELTA常量的存在就让我们有了可乘之机,不改动代码的前提下,直接修改常量减掉8*3600秒就OK了。
以下是ntptime的完整代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 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 # The NTP host can be configured at runtime by doing: ntptime.host = 'myhost.org' 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) 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 # 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) machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0)) |