update NTP to timezone

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
guyd
Posts: 81
Joined: Fri Jul 20, 2018 6:08 am

update NTP to timezone

Post by guyd » Tue Aug 14, 2018 5:02 am

Hi,
I'm using micropython on ESP32, and during boot ( main.py ), and enabling wifi connection, I use the following code to update time

Code: Select all

impoty ntptime
ntptime.settime()
How do I fix it to UTC+3 ?

Guy

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

Re: update NTP to timezone

Post by pythoncoder » Thu Aug 16, 2018 8:38 am

Timezone support is not yet implemented (as far as I know).

My approach is to let the RTC run UTC and perform the correction in code. Use mktime to get seconds since the epoch, add or subtract 3600*hours, and put the result into localtime. Rollover is then handled correctly.
Peter Hinch
Index to my micropython libraries.

guyd
Posts: 81
Joined: Fri Jul 20, 2018 6:08 am

Re: update NTP to timezone

Post by guyd » Thu Aug 16, 2018 12:40 pm

Hi
That was my first try... but since I need utc +3, adding 3*3600 to given time, I get hour 24, 25 when day changes ( and without date update)

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

Re: update NTP to timezone

Post by pythoncoder » Thu Aug 16, 2018 2:40 pm

I'm not sure I follow:

Code: Select all

>>> import utime
>>> t =utime.mktime((2018,8,16,22,0,0,3,0))
>>> t += 4*3600
>>> utime.localtime(t)
(2018, 8, 17, 2, 0, 0, 4, 229)
>>> 
The date and time have rolled over correctly. What I'm suggesting is code along these lines:

Code: Select all

utime.localtime(utime.mktime(utime.localtime()) + 3*3600)
The innermost mktime() retrieves UTC time in seconds since the epoch. Add the offset and convert to a datetime tuple.
Peter Hinch
Index to my micropython libraries.

guyd
Posts: 81
Joined: Fri Jul 20, 2018 6:08 am

Re: update NTP to timezone

Post by guyd » Sat Aug 25, 2018 10:27 am

thank you.
I understand what you did different than me, but this why ( without updating "RTC" )- I'll need to convert time every time I need.
I wrote this :

Code: Select all

import ntptime
import machine
import utime

ntptime.settime()
rtc = machine.RTC()
utc_shift = 3

tm = utime.localtime(utime.mktime(utime.localtime()) + utc_shift*3600)
tm = tm[0:3] + (0,) + tm[3:6] + (0,)
rtc.datetime(tm)

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

Realtime RTC updates

Post by pythoncoder » Sun Aug 26, 2018 5:38 am

Bear in mind that updating the RTC periodically can cause problems depending on the design of your application.

This is because sudden jumps in the RTC time can cause issues if any part of the application uses the RTC for timing. This can be avoided by letting the RTC free-run and performing corrections at runtime.
Peter Hinch
Index to my micropython libraries.

guyd
Posts: 81
Joined: Fri Jul 20, 2018 6:08 am

Re: update NTP to timezone

Post by guyd » Sun Aug 26, 2018 7:41 am

Hi,

thank you for pointing it out ( earlier ) - Since ESP has scheduled tasks, I altered the design that a main RPI send MQTT commands ( on schedule ) to activate ESP clients.

Guy

TDB43
Posts: 1
Joined: Sat Apr 06, 2019 8:31 pm

Re: update NTP to timezone

Post by TDB43 » Sat Apr 06, 2019 8:46 pm

This is a good option:
You have only to fill YOUR_WIFI_SSID and YOUR_WIFI_PASSWORD with the connection data of your wifi network.

>>>def do_connect(wifi_ssid,wifi_passwd): #useful function for connecting to your local WiFi network
>>>"nbsp" import network, time
>>> wlan = network.WLAN(network.STA_IF)
>>> wlan.active(True)
>>> if not wlan.isconnected():
>>> print('\nConnecting to network', end='')
>>> wlan.connect(wifi_ssid, wifi_passwd)
>>> while not wlan.isconnected():
>>> print('.', end='')
>>> time.sleep(0.5)
>>> pass

>>> import ubinascii
>>> print()
>>> print("Interface's MAC: ", ubinascii.hexlify(network.WLAN().config('mac'),':').decode()) # print the interface's MAC
>>> print("Interface's IP/netmask/gw/DNS: ", wlan.ifconfig(),"\n") # print the interface's IP/netmask/gw/DNS addresses

>>>do_connect("YOUR_WIFI_SSID","YOUR_WIFI_PASSWORD")

>>>import ntptime #NTP-time (from pool.ntp.org)
>>>ntptime.settime() #https://github.com/micropython/micropyt ... ntptime.py

>>>from machine import RTC
>>>(year, month, mday, week_of_year, hour, minute, second, milisecond)=RTC().datetime()
>>>RTC().init((year, month, mday, week_of_year, hour+2, minute, second, milisecond)) # GMT correction. GMT+2
>>>print ("Fecha/Hora (year, month, mday, week of year, hour, minute, second, milisecond):", RTC().datetime())
>>>print ("{:02d}/{:02d}/{} {:02d}:{:02d}:{:02d}".format(RTC().datetime()[2],RTC().datetime()[1],RTC().datetime()[0],RTC().datetime()[4],RTC().datetime()[5],RTC

Post Reply