0

If I call configTime() on a ESP8266 using ESP8266 Core, then time() gets changed from uptime to unixtime. However this sends a SNTP request. I have a DS3231 RTC attached without internet. How can I set time() without calling configTime()?

Thanks in advance

1
  • Probably settimeofday(...) is what you want. Commented Feb 23, 2022 at 12:19

1 Answer 1

4

Whenever you have epoch time, either from RTC, NTP, etc., use the POSIX function settimeofday() to set system time as @Majenko said

The sample code is as follows:

// Assuming we already have Unix `epoch` time, which is time from Jan 1 1970.
// From RTC, NTP, etc.
time_t epoch_t = epoch;

// set the time to UTC
setTime(epoch_t);

// set the system time to UTC
timeval tv = { epoch_t, 0 };
settimeofday(&tv, nullptr);

// Now we can have system time by calling time()
time_t timeNowUTC;
struct tm * timeInfo

timeNowUTC = time(nullptr);
timeInfo = localtime(&timeNowUTC);

For the record, the issue has been solved in Arduino Forum : Update time() from DS3231

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.