Page 1 of 3

timezone support in MicroPython ?

Posted: Sat Aug 12, 2017 2:51 pm
by c.man
Hi,
I need to store exact time and date for my city.
So, I inserted in boot.py the following code:

Code: Select all

from ntptime import settime
settime()
so, I can read time by:

Code: Select all

import utime
utime.localtime()

Isn't there a timezone support and legal/solar time support ?
How I to do ?

Re: timezone support in MicroPython ?

Posted: Sat Aug 12, 2017 5:07 pm
by deshipu
I'm afraid the timezones data required for such support would be about two orders of magnitude larger than the memory available on the microcontrollers supported by MicroPython.

Re: timezone support in MicroPython ?

Posted: Sat Aug 12, 2017 5:24 pm
by vahithosan
c.man wrote:Hi,
I need to store exact time and date for my city.
So, I inserted in boot.py the following code:

Code: Select all

from ntptime import settime
settime()
so, I can read time by:

Code: Select all

import utime
utime.localtime()

Isn't there a timezone support and legal/solar time support ?
How I to do ?
I think . You will use rtc.

Code: Select all

import network
import time
import utime
import machine
from ntptime import settime

settime()
rtc=machine.RTC()

# for time convert to second
tampon1=utime.time() 
    
# for gmt. For me gmt+3. 
# 1 hour = 3600 seconds
# 3 hours = 10800 seconds
tampon2=tampon1+10800

# for second to convert time
(year, month, mday, hour, minute, second, weekday, yearday)=utime.localtime(tampon2)

# first 0 = week of year
# second 0 = milisecond
rtc.datetime((year, month, mday, 0, hour, minute, second, 0))

Re: timezone support in MicroPython ?

Posted: Sat Aug 12, 2017 7:01 pm
by c.man
deshipu wrote:I'm afraid the timezones data required for such support would be about two orders of magnitude larger than the memory available on the microcontrollers supported by MicroPython.
It would be enough to specify GMT (+1 for me: Rome) and legal/solar time in ntptime library.
Obviously you need to know what time zone you are referring to (now we are in legal time, so +1).
So effective time is +1 +1 +localtime

Re: timezone support in MicroPython ?

Posted: Mon Jul 30, 2018 2:25 pm
by jimeer
For me I just needed to add 1 hour (BST) at the appropriate time of year, for other zones it would be some offset. Most daylight saving countries use the last Sunday in March and the last Sunday in October. This does vary see the Wikipedia entry. I just worked backwards from November to get the too dates and added the offset if the current time was between the tow dates. It only needs to be done once a day but is quick enough to do more frequently than that.
[code]
def daylight(now,offset):
rt = time.localtime(now)
nows = now + (11 - rt[1]) * 2592000 # about mid November
# work back to find Sunday Oct
while time.localtime(nows)[1] != 10: #Oct
nows -= 86400
while time.localtime(nows)[6] != 6: #Sun
nows -= 86400
nowsOct = nows
# work back to find Sunday Mar
while time.localtime(nows)[1] != 3: #Mar
nows -= 86400
while time.localtime(nows)[6] != 6: #Sun
nows -= 86400
nowsMar = nows
# saving is used between dates
if (now > nowsMar) and (now < nowsOct):
now += (3600 * offset)
return now
[/code]

How about this appoach?

Posted: Mon Jul 30, 2018 4:31 pm
by pythoncoder
This doesn't take account of the time of day at which the change occurs.

The approach I'd use is to write a program on a PC which (for my own locale) calculated the time of the start and end of DST for each year of interest. The times would be in seconds since the MicroPython epoch. The output of the script would be a Python dict indexed by year and containing tuples of form (dst_start, dst_end) - (numbers below are arbitrary).

Code: Select all

dst = {2018:(12345, 67890), 2019:(88888, 99999),}
def ltime():
    t = utime.time()
    start, end = dst[utime.localtime(t)[0]
    return t if t < start or t > end else t + 3600
The same thing could be done with a file if the dict was regarded as being excessively big.

In either case the calculation is done once, offline on a PC, minimising the work to be done at runtime.

Re: timezone support in MicroPython ?

Posted: Mon Jul 30, 2018 8:01 pm
by Christian Walther
You don’t even need to write that program, it already exists: the zic time zone compiler that is part of the zoneinfo database. If you are on any kind of Unix-like OS, you likely have its output already in /usr/share/zoneinfo/: binary files containing exactly such a list of transition times (among other things). There is a Python API for reading them in pytz.

Re: timezone support in MicroPython ?

Posted: Tue Jul 31, 2018 6:28 am
by JumpZero
Hi
sometime ago I posted this
viewtopic.php?f=2&t=4034
this is for CET time but can easily be adapted
May be it can help
--
Jmp0

Re: timezone support in MicroPython ?

Posted: Tue Jul 31, 2018 8:23 am
by pythoncoder
Thanks @Christian Walther, @JumpZero. Useful information and code.

Re: timezone support in MicroPython ?

Posted: Wed Sep 05, 2018 1:23 pm
by andydatablocks
def dstTime():
year = time.localtime()[0] #get current year
# print(year)
HHMarch = time.mktime((year,3 ,(14-(int(5*year/4+1))%7),1,0,0,0,0,0)) #Time of March change to DST
HHNovember = time.mktime((year,10,(7-(int(5*year/4+1))%7),1,0,0,0,0,0)) #Time of November change to EST
# print(HHNovember)
now=time.time()
if now < HHMarch : # we are before last sunday of march
dst=time.localtime(now-18000) # EST: UTC-5H
elif now < HHNovember : # we are before last sunday of october
dst=time.localtime(now-14400) # DST: UTC-4H
else: # we are after last sunday of october
dst=time.localtime(now-18000) # EST: UTC-5H
return(dst)