time.h and xtensa toolchain

kyrpav
Posts: 37
Joined: Wed Jan 29, 2020 8:27 am

time.h and xtensa toolchain

Postby kyrpav » Mon Mar 02, 2020 9:51 pm

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?

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: time.h and xtensa toolchain

Postby ESP_igrr » Tue Mar 03, 2020 6:10 am

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).

Who is online

Users browsing this forum: No registered users and 90 guests