8 years, 5 months ago.

time problems - again: mktime behavior

Hello. Again I cannot deal with time functions on STM32F4. I have to capture response from SIM808 built in ntp client in format:

Quote:

+CCLK : "16/05/29,17:23:25+00"

Nothing really hard, using strncmp and scanf. So here is my code

struct tm t;
sscanf(mail->str, "+CCLK: \"%d / %d/ %d , %d : %d : %d %*s", &t.tm_year, &t.tm_mon, &t.tm_mday, &t.tm_hour, &t.tm_min, &t.tm_sec);
// to fit convention "years from 1900"
t.tm_year += 100;
// months counted from 0
t.tm_mon--;
set_time(mktime(&t));

However, when i read the timestamp with time(NULL) I get timestamp from 2011 not 2016. I've even tried more direct approach:

                RTC_DateTypeDef dateStruct;
                RTC_TimeTypeDef timeStruct;
                static RTC_HandleTypeDef RtcHandle;
                RtcHandle.Instance = RTC;
                
                dateStruct.WeekDay  = t.tm_wday;
                dateStruct.Month    = t.tm_mon +1;
                dateStruct.Date     = t.tm_mday;
                dateStruct.Year     = t.tm_year -100;  // as mktime works with 1900 ==0 convention
                timeStruct.Hours    = t.tm_hour;
                timeStruct.Minutes  = t.tm_min;
                timeStruct.Seconds  = t.tm_sec;
                timeStruct.TimeFormat     = RTC_HOURFORMAT_24; // also tried 12 - no luck
                timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
                timeStruct.StoreOperation = RTC_STOREOPERATION_RESET;
                
                HAL_RTC_SetDate(&RtcHandle, &dateStruct, FORMAT_BIN);
                HAL_RTC_SetTime(&RtcHandle, &timeStruct, FORMAT_BIN);

results were exactly the same.

                time_t secs = mktime(&t);
                set_time(secs);

and this worked properly. How much is it different from first approach?

Be the first to answer this question.