gmag11 / ESPNtpClient

High accuracy NTP library for ESP32 and ESP8266
MIT License
118 stars 25 forks source link

how does timesync work? #36

Closed Schawen closed 2 years ago

Schawen commented 2 years ago

I expected to get the same results from the following "code":

Serial.print("Time now: "); Serial.println(getTStr.( now() ) );
Serial.print("NTP  now: "); Serial.println(NTP.getTimeStr());

// Output a few seconds after reboot and NTP sync
Time now: 00:00:20
NTP  now: 12:49:48

Do I need to update the local time system on my own (by Interrupts)?

Is there a method to request the NTP time as time_t and not as String?

Schawen commented 2 years ago

The library description points out, that

After that, synchronization is done regularly without user intervention. Some parameters can be adjusted: server, sync frequency, time zone.

You don't need anything more. Time update is managed inside library so, after NTP.begin() no more calls to library are needed.

Together with the "advancedExample" code I became confused ...

I found the answer(?) here: #26
and here: basicExample:

`void ntpEventHandler (NTPEvent_t e) { //Serial.printf("NTP Event: %s\n", NTP.ntpEvent2str (e)); if (e.event == timeSyncd || e.event == partlySync) { time_t utcTime = time (NULL); tm* currentTime = localtime (&utcTime); // Serial.printf ("Year: %d\n", currentTime->tm_year + 1900); // Serial.printf ("Month: %d\n", currentTime->tm_mon + 1); // Serial.printf ("Day: %d\n", currentTime->tm_mday); // Serial.printf ("Hour: %d\n", currentTime->tm_hour); // Serial.printf ("Minute: %d\n", currentTime->tm_min); // Serial.printf ("Second: %d\n", currentTime->tm_sec);

    setTime (currentTime->tm_hour, currentTime->tm_min, currentTime->tm_sec, currentTime->tm_mday, currentTime->tm_mon + 1, currentTime->tm_year + 1900);
}

}`