How does chrono on an arduino get the current unix timestamp?

I use the nodemcu ESP8266. I am currently working on a clock. So far, I had been sure, I would need to get the time from the internet. I did this using the worldtimeapi and it worked. Just for fun, I wanted to see what would happen if I used chrono (std::chrono). So I tried this code:

const auto now = std::chrono::system_clock::now();
const std::time_t t_c = std::chrono::system_clock::to_time_t(now);
tft.setCursor(0, 0);
tft.printf("The system clock is currently at %s", std::ctime(&t_c));

To my surprise, it gave me the correct time with a unix offset of zero. Can anyone explain to me how this is possible? It seems unlikely to me, that the arduino has a clock inside of it that's running at all times. The idea of chrono getting the time from the internet itself also seems weird, as the documentation clearly says, it returns the SYSTEM time. Lastly, it's unlikely that it gets the time from the PC it's connected to since the same C++ code on my PC returns the time with a unix offset of +2.

How does chrono on the esp8266 get the current unix timestamp?

Short answer - the wifi stack runs SNTP by default.

1 Like

Thanks, that's good to know. Two quick questions:

  1. Since it's SNTP and not NTP, it's less reliable ans shouldn't be used for serious projects, right?
  2. I should implement NTP on my arduino instead of using an API to get more precise times, right?

Research NTP. Short answer,

  1. No
  2. No.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.