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
settimeofday(...)
is what you want.