-
Notifications
You must be signed in to change notification settings - Fork 13.3k
gettimeofday incorrect value for tv_usec #3814
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
Comments
so basically it boils down to micros() - millis()*1000 |
I think you have the right idea, but the problem with using micros directly like that is that it is only 32-bit. This could all be gravy if we had a Something like this is a step in the right direction:
I think that would work in a pinch. Unfortunately this would still be bugged over the long haul because s_bootTime is never updated and millis() overflows after 2^32 ms. If we had the micros64(), and I think it can be done effectively, then it could be:
That doesn't solve the long term inherent drift people encounter on the device, but that is another problem to tackle. I think the best strategy there is to periodically get a fresh NTP stamp to re-establish the base. In other words, s_bootTime needs to get updated from an NTP source every now and then. Probably need to rename it too! |
The current implementation _gettimeofday_r is incorrectly setting the tv_usec field.
https://github.com/esp8266/Arduino/blob/master/cores/esp8266/time.c#L104
According to http://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html
tv_usec should be "This is the rest of the elapsed time (a fraction of a second), represented as the number of microseconds. It is always less than one million."
The current code is simply doing
tp->tv_usec = micros();
. This leads to the wall clock time advancing forward too quickly. I am certainly aware of the caveats of tracking wallclock (ie CLOCK_REALTIME) in ESP8266, but this is just making it far worse.I have some ideas on how to square this away once and for all (crossing my fingers). I will try to follow-up with additional info or possibly a pull request.
The text was updated successfully, but these errors were encountered: