I'm using the following function to read the current epoch:
time_t _GetEpoch(void) { _i16 ret; _u8 pConfigOpt = SL_DEVICE_GENERAL_DATE_TIME; _u16 pConfigLen = sizeof(SlDateTime_t); SlDateTime_t dateTime = {0}; ret = sl_DeviceGet(SL_DEVICE_GENERAL, &pConfigOpt, &pConfigLen, (unsigned char *) &dateTime); ASSERT_ON_ERROR(ret); struct tm t; time_t t_of_day; t.tm_year = dateTime.tm_year - 1900; t.tm_mon = dateTime.tm_mon - 1; t.tm_mday = dateTime.tm_day; t.tm_hour = dateTime.tm_hour; t.tm_min = dateTime.tm_min; t.tm_sec = dateTime.tm_sec; t.tm_isdst = -1; t_of_day = mktime(&t); UART_PRINT(0, "[SYS] struct tm: %d/%d/%d %d:%d:%d\r\n", t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec); UART_PRINT(0, "[SYS] time_t : %lld\r\n", t_of_day); return t_of_day; }
My application will put in hibernation the MCU for a minute using:
Power_shutdown(0, 60000);
on wakeup I call the above function to read the current epoch and do something appropriate. Sometimes the reading is wrong, and interestingly enough, it will return a date in 2022, then if I read it again it will show the correct value:
[SYS] struct tm: 117/10/20 10:23:34 [SYS] time_t : 1511173414 [SYS] Shutdown [SYS] struct tm: 117/10/20 10:24:41 [SYS] time_t : 1511173481 [SYS] Shutdown [SYS] struct tm: 122/1/20 21:7:48 [SYS] time_t : 1645391268 [SYS] Shutdown [SYS] struct tm: 117/10/20 10:25:40 [SYS] time_t : 1511173540
Of course there MUST be something wrong in my code. What could cause such a wrong RTC reading?