timezone support in MicroPython ?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
User avatar
c.man
Posts: 21
Joined: Thu Jul 06, 2017 9:12 pm

timezone support in MicroPython ?

Post by c.man » Sat Aug 12, 2017 2:51 pm

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 ?

User avatar
deshipu
Posts: 1385
Joined: Thu May 28, 2015 5:54 pm

Re: timezone support in MicroPython ?

Post by deshipu » Sat Aug 12, 2017 5:07 pm

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.

vahithosan
Posts: 13
Joined: Wed Jul 26, 2017 5:15 pm

Re: timezone support in MicroPython ?

Post by vahithosan » Sat Aug 12, 2017 5:24 pm

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))

User avatar
c.man
Posts: 21
Joined: Thu Jul 06, 2017 9:12 pm

Re: timezone support in MicroPython ?

Post by c.man » Sat Aug 12, 2017 7:01 pm

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

jimeer
Posts: 3
Joined: Mon Jul 30, 2018 1:44 pm
Location: Barnsley
Contact:

Re: timezone support in MicroPython ?

Post by jimeer » Mon Jul 30, 2018 2:25 pm

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]

User avatar
pythoncoder
Posts: 4940
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

How about this appoach?

Post by pythoncoder » Mon Jul 30, 2018 4:31 pm

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.
Peter Hinch

Christian Walther
Posts: 129
Joined: Fri Aug 19, 2016 11:55 am

Re: timezone support in MicroPython ?

Post by Christian Walther » Mon Jul 30, 2018 8:01 pm

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.

JumpZero
Posts: 38
Joined: Mon Oct 30, 2017 5:54 am
Location: Arcachon - France

Re: timezone support in MicroPython ?

Post by JumpZero » Tue Jul 31, 2018 6:28 am

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

User avatar
pythoncoder
Posts: 4940
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: timezone support in MicroPython ?

Post by pythoncoder » Tue Jul 31, 2018 8:23 am

Thanks @Christian Walther, @JumpZero. Useful information and code.
Peter Hinch

andydatablocks
Posts: 2
Joined: Wed Sep 05, 2018 1:21 pm

Re: timezone support in MicroPython ?

Post by andydatablocks » Wed Sep 05, 2018 1:23 pm

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)

Post Reply