What about this code? I grabed it from my project for Pico W. It requires WiFi network nearby. You can get actual time from NTP server and use it in your code. It is untested, just as an example...
Just realised your code does more than just set the time. That's all my code below does. I call it once near the start then call setclock() on a periodic timer.
Code:
# this assumes you already have code to establish a network connectionimport machineimport ntptimeimport timedef setclock(dummy): # set RTC. # ntptime.settime() always returns UTC. COde attempts to adjust that to DST # based on UK changeover criteria # "dummy" is present only to facilitate calling from a timer. global dst try: ntptime.settime() log.debug(time.gmtime()) # calculate DST. UK only. # starts 01:00 last sunday in march # ends 02:00 last sund in october t = time.mktime((time.gmtime()[0], 3, 31, 01, 00, 00, 00, 00 ) ) wd = time.gmtime(t)[6] dst['start'] = t - (wd * 24 * 60 * 60) t = time.mktime((time.gmtime()[0], 10, 31, 01, 00, 00, 00, 00 ) ) wd = time.gmtime(t)[6] dst['end'] = t - (wd * 24 * 60 * 60) t = time.gmtime() if dst['start'] <= time.mktime(t) < dst['end']: t = rtc.datetime((t[0], t[1], t[2], t[6], t[3] + 1, t[4], t[5], 0 )) except Exception as e: print'Unable to set time')dst = {'start':None, 'end':None}rtc = machine.RTCsetclock(None)
Statistics: Posted by thagrol — Wed Apr 24, 2024 11:00 pm