CET time

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
JumpZero
Posts: 27
Joined: Mon Oct 30, 2017 5:54 am
Location: Arcachon - France

CET time

Post by JumpZero » Fri Nov 10, 2017 7:44 am

CET Time
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

SpotlightKid
Posts: 417
Joined: Wed Apr 08, 2015 5:19 am

Re: CET time

Post by SpotlightKid » Mon Dec 04, 2017 11:17 pm

Neat. Copied this to my collection of useful MicroPython snippets, thanks!

User avatar
JohnLittle
Posts: 11
Joined: Mon Oct 08, 2018 7:10 pm

Re: CET time

Post by JohnLittle » Mon Oct 08, 2018 7:34 pm

Hi all,

I know this thread started a long time ago, but here is my solution based on JumpZero's code. Sorry, bbcode is off for my account (maybe off for new users?), I'll prettify this when I work out how :)

So basically, I call my sync_ntp() function every 15 minutes to synchronise utime.localtime() or utime.time() with ntp time, and adjust the local timezone offsets if any. woff and soff are winter and summer offset, 0 and 1 for Britain (for now) and 1 and 2 for CET.

I didn't feel like I wanted to call a conversion function like JumpZero's. For my purpose, doing arithmetic directly on utime.localtime() and utime.time() as mangled epoch made more sense. Also, utime instead of time because the utime module is sufficient for my needs.

Cheers,
John

[code]# inspired by
# viewtopic.php?f=2&t=4034
# and code from ntptime.settime()
#
# winter offset and summer offset set for Britain. use 1 and 2 for CET
def sync_ntp(woff=0,soff=1):
trials = 10
while trials > 0:
try:
ntptime.settime()
break
except Exception as e:
print(".", end="")
utime.sleep(1)
trials -= 1

if trials == 0:
print(str(e))
return

t = utime.time()
tm = list(utime.localtime(t))
tm = tm[0:3] + [0,] + tm[3:6] + [0,]
year = tm[0]

#Time of March change for the current year
t1 = utime.mktime((year,3,(31-(int(5*year/4+4))%7),1,0,0,0,0))
#Time of October change for the current year
t2 = utime.mktime((year,10,(31-(int(5*year/4+1))%7),1,0,0,0,0))

if t >= t1 and t < t2:
tm[4] += soff #UTC + 1H for BST
else:
tm[4] += woff #UTC + 0H otherwise

machine.RTC().datetime(tm)
print(utime.localtime())[/code]

User avatar
moooond
Posts: 5
Joined: Wed Jul 08, 2020 4:14 pm
Contact:

Re: CET time

Post by moooond » Fri Jul 10, 2020 11:39 am

while the whole tzdb is around 400k the individual files for each zone are rather small. e.g. Europe/Vienna is 2k in size. if, like on an esp32, we want to download time form ntp. we could get just the zonefile where we are in and maybe a small library that parses just that.. maybe i will look into this..

Post Reply