How long to sync time?

papadeltasierra
Posts: 28
Joined: Thu Nov 03, 2022 1:57 pm

How long to sync time?

Postby papadeltasierra » Sun Dec 31, 2023 2:15 pm

Hi. I am using an ESP32c3 but am not using SNTP to set time but will instead learn it from an attached ZigBee device and network. I then use

Code: Select all

settimeofday()
to set the time on the ES32c3.

Prototyping by just setting a hard-coded value and then calling

Code: Select all

time()
seems to indicate that the ESP32c3 does not set the correct time immediately but appears to drift towards the correct time. Given that it is drifting from 1-Jan-1970 up to 2023 this takes a little time so two questions:

1. How do I know when time has stabilised?
2. Any way to speed the process along?

For subsequent updates I will use

Code: Select all

adjtime()
but I do ideally want the initial time to synchronize ASAP so that subsequent calls to

Code: Select all

time()
return sensible values.

Thanks.

papadeltasierra
Posts: 28
Joined: Thu Nov 03, 2022 1:57 pm

Re: How long to sync time?

Postby papadeltasierra » Mon Jan 01, 2024 10:59 am

This page, https://docs.espressif.com/projects/esp ... ime%20sync, claims:
To stop smooth time adjustment and update the current time immediately, use the POSIX function

Code: Select all

settimeofday()
.
May have to do some more investigation to try and see what is going on!

RalphD
Posts: 103
Joined: Thu Nov 25, 2021 9:02 pm

Re: How long to sync time?

Postby RalphD » Mon Jan 01, 2024 11:58 am

Well, I don't know if I get this right, but it all depends that the system time is set by some information from outside. After any reset any ESP will start at zero hence 1.1.1970 when Linux was born (more less) if you connect to a Zigbee network either one of the connected devices is having a clock which is set to the correct time or very likely one of the connected devices is using SNTP through the Zigbee gateway.

So it count down in any case how long does it take to have the SNTP protocoll setting the system time. That depends on many factors like Internet access speed, firmware of connected AP etc.

From my experience you should give the system at least 1 minute to get the correct time, and the time setting is not 'fading' towards the correct time, it jumps instantly to the time it is getting from the network what ever that is Zigbee, WiFi or Ethernet. So watching the setting of the year will give you the moment when time is set. I use simply something like this

Code: Select all

    // Is time set? If not, tm_year will be (1970 - 1900).
    if (timeinfo.tm_year > (2023 - 1900)) {
    	maxill_status->timeSet = 1;
    }

papadeltasierra
Posts: 28
Joined: Thu Nov 03, 2022 1:57 pm

Re: How long to sync time?

Postby papadeltasierra » Mon Jan 01, 2024 2:06 pm

Ralph, I'm not using any part of SNTP but my digging did throw something up interesting. I created a minimal app that does just this:
  • Create an event loop and timer popping every second, where the (event) callback just prints the result of time(NULL)
  • Hard-coded the time to "now" using settimeofday()
  • Started the timer.
The results were that my event/timer printf()s immediately showed the correct time ("now" plus as many seconds as expected).

This is definitely not what happens in my real application which is now making me wonder whether something is kicking me in the teeth, maybe a stack overflow or similar. I'm going to try disabling chunks of code and see what happens!

papadeltasierra
Posts: 28
Joined: Thu Nov 03, 2022 1:57 pm

Re: How long to sync time?

Postby papadeltasierra » Mon Jan 01, 2024 2:28 pm

Oh how I laughed! Turns out that this is all a formatting bug. I am using the limited formatting 'nano' library and was using "%lu" to trace the result of time(NULL). However the result is 64-bit and the 'nano' formatting results is some very strange results.

Casting (uint32_t)time(NULL) produces sensible results of the values I expect and to be clear...
If you call settimeofday(), time is immediately updated.

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 65 guests