It’s very convenient to get UTC time with the ntp module.
But as stated in the doc there is no support for local time. I have found useful for my own needs to write this function to get Central European Time (CET) from UTC including daylight saving corrections.
Code: Select all
# Micropython esp8266
# This code returns the Central European Time (CET) including daylight saving
# Winter (CET) is UTC+1H Summer (CEST) is UTC+2H
# Changes happen last Sundays of March (CEST) and October (CET) at 01:00 UTC
# Ref. formulas : http://www.webexhibits.org/daylightsaving/i.html
# Since 1996, valid through 2099
import time
def cettime():
year = time.localtime()[0] #get current year
HHMarch = time.mktime((year,3 ,(31-(int(5*year/4+4))%7),1,0,0,0,0,0)) #Time of March change to CEST
HHOctober = time.mktime((year,10,(31-(int(5*year/4+1))%7),1,0,0,0,0,0)) #Time of October change to CET
now=time.time()
if now < HHMarch : # we are before last sunday of march
cet=time.localtime(now+3600) # CET: UTC+1H
elif now < HHOctober : # we are before last sunday of october
cet=time.localtime(now+7200) # CEST: UTC+2H
else: # we are after last sunday of october
cet=time.localtime(now+3600) # CET: UTC+1H
return(cet)
Maybe it can be useful to others.
--
Jmp0