-
Notifications
You must be signed in to change notification settings - Fork 638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set RTC to same timezone and daylight settings upon boot #1828
Conversation
Why though? configTime just enables sntp loop & timer, which will do nothing bc there is no server set (besides setting timer and wasting some time in timer func). AFAIK, this may be different between lwip and lwip2, and lwip2 uses TimeLib-like approach and not the real RTC memory time |
If you call |
I mean, why use it at all? It is not synced anyways using the lwip's sntp and we increase binary size too, because of the way linkage works. BTW, have you tried with lwip2 builds? (removing -DPIO_FRAMEWORK_ARDUINO_LWIP_HIGHER_BANDWIDTH from the build flags). Would it not just return 0 there? |
To answer my own question, time(0) results are:
Initially I thought that there was a way to set this outside of sntp code, but does not seem like it :/ |
I tested this with LWIP 2 (
The reason it's useful to get the RTC time in the correct timezone is that you don't need to wait on NTP sync to complete in order to connect a BearSSL/CA client (e.g. MQTT). In most cases the RTC will be close to synchronized with the NTP clock (for example after a reboot) so you can speed up the initial connect with a few seconds (a wrong time will results in a failing BearSSL connection). |
Sorry for being a bit dense, but do you mean that you have it synced between resets somehow? AFAIK it just keeps a global var and does not do network sync. configTime is a separate thing from TimeLib / NtpClientLib. And it is not RTC per-se, since ESP8266 does not keep time there, but some internal timekeep timer thing that Core devs added. |
I understand that's it not a (battery powered) RTC in the traditional sense, but it serves the same purpose (keep clock state between reboots). Only after the NTP sync is done, the system time (from |
I am missing one thing still - how does the time from NtpClientLib gets into the time() results? Whenever time() is called in our context, it should always return seconds from boot (i.e. 0) plus timezone offset and not the real timestamp. Looking at the routines that you mention, they are only relevant for lwip1.4 code and those RTC register writes are only triggered when |
Ok. So there's one way that this may happen: time() will return valid timestamp from the initial sync, but it will never be updated after that (if that what is really happening, this also is causing really funny issue accessing ntp server twice in a row) |
Wow good find, I was scratching my head why I couldn't reproduce the issue anymore at a different location - |
Yep, given that this is not doing the intended thing. This does incentivize revisiting #1380 again, and maybe just dropping NTPClient when lwip2 is detected. Minimal port would be to add timezone conversion rules and minimal TimeLib compatibility layer. |
Ok closing this, I agree that #1380 would probably be the better approach |
btw, see esp8266/Arduino#6373 |
@mcspr Why don't you PR on lwip2 repo ? |
I have not tested anything yet 🙃 And comments also suggest to check whether arithmetic works ok. Plus, there's also requirement to set the default time closer to today's date And there are also some other things I wanted to try first, like customizable sntp_set_time (or esp8266/arduino settimeofday) callback a-la IDF (commit / discussion PR / forum discussion) |
(Arduino has the time_is_set-like callback) |
Yes, but I was looking for any reason to make time_is_set cb itself overridable. The way commit above chooses to either use the default, setting time directly via settimeofday or through adjtime (which is another interesting feature). Or it can be overriden by the user app to avoid doing that at all, working as a notification of sntp success instead of system time change |
esp8266/Arduino#6373 is now merged in core: esp8266/Arduino@ffe5476 |
This sets the RTC to the same timezone and daylight settings as the NTP config. This fixes that
time(nullptr)
returns the same time before and after NTP sync.