3 ms·
I do currently hardcode my time zone (although it could be done as a setting) so I suppose it's not 100% automatic...maybe with a geoip lookup! NTP gets you th
by blutack 5y ago
I do currently hardcode my time zone (although it could be done as a setting) so I suppose it's not 100% automatic...maybe with a geoip lookup!
NTP gets you the date, so you just need a library or algo that maps your time zone into rules for what dates to change on and by how much. Luckily, one ships in the ESP32 sdk. I used something similar to the below, although adapting to my location. Works great for me.
https://werner.rothschopf.net/microcontroller/202103_arduino_esp32_ntp_en.htm https://werner.rothschopf.net/microcontroller/202103_arduino...
You would theoretically need to update your tz descriptor if your country rules changed. My countries rules never do so I've never worried about it.
This could probably be worked around by periodically checking an endpoint for updates to the tzdb descriptor (like a Linux distro does via the package manager).
Alternatively, you could just knock up a very basic web service which returns date/UTC time of next change and new UTC offset for a given timezone. Then poll once a day or whatever. For bonus points, don't use JSON because it's a pest to parse on the ESP. Or use
http://worldtimeapi.org/ http://worldtimeapi.org/ and accept the third party dep.
- stavros 5y agoThat's fantastic, thanks! Hardcoding my timezone is no problem, as long as I don't have to change the offset manually every six months. Thanks!