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 Answer 1


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

// 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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.