Page 1 of 1

time.h and xtensa toolchain

Posted: Mon Mar 02, 2020 9:51 pm
by kyrpav
I am trying to get current time of my system (debian) to set it to an rtc with esp32.

i used code:
  1.     time_t currentTime;
  2.     time(&currentTime);
  3.     printf("%s",ctime(&currentTime));
And i am always getting 1970 1st of January.

I also tried another eclipse that has nothing to do with esp32, so it is a normal C/C++ eclipse and created a cmake program normally. The same code where toolchain was the default from user/bin/gcc worked properly. is there something that i can do to the idf project so time.h can work?

Re: time.h and xtensa toolchain

Posted: Tue Mar 03, 2020 6:10 am
by ESP_igrr
Hi kyrpav,
On an ESP32, the time starts counting from 0 after reset, which is why you are getting 1970 (start of Unix epoch).
Usually the application will synchronize the time using SNTP after connecting to Wi-Fi or Ethernet. Please check examples/protocols/sntp to see how this can be done.
Once the time is set (by SNTP or using settimeofday function) you can call "time" to get the current time. If the ESP32 goes into deep sleep mode, timekeeping will continue using the RTC. However time always gets reset to 0 after other kinds of reset (power on, CPU reset, watchdog, etc).