I have the following code to parse a datetime string
received from GPS satellites into the struct tm
, and then use mktime()
to get the epoch
from it, the result is correct on my Debian machine, but wrong on my ESP32 with ESP-IDF
, do you have any suggestion on why this is happening, is something wrong with DST or timezone stuff?
#include "rs_time.h"
time_t time_from_gnss_info_time(const char * datetime_str){
time_t epoch;
struct tm tm;
sscanf(
datetime_str,
"%4d%2d%2d%2d%2d%2d",
&tm.tm_year,
&tm.tm_mon,
&tm.tm_mday,
&tm.tm_hour,
&tm.tm_min,
&tm.tm_sec
);
epoch = mktime(&tm); // result is '1462765068' or Mon May 9 03:37:48 2016
printf("the date and time is: %s %ld ",ctime(&epoch), time(NULL));
return epoch;
}
the value for epoch
after using mktime()
when the datetime_str
is '20210913221332'
is: 1462765068
, also the ctime()
representation is : Mon May 9 03:37:48 2016