I'm writing a C++ application on an embedded ARM device running Embedded Linux.
I am trying to convert a date and time string to seconds, subtract it from the current time in seconds, and taking action if the number of seconds elapsed is greater than a certain number.
Something that should be quite simple to implement has proved quite tricky, and i'm not sure why.
The time difference i'm calculating turns out to be a massive number, when it should really be a low number. See my below code. I'm manually hardcoding a time and date string for testing.
std::string timestr = "2020-12-21T16:07:00";
struct tm t = {0};
sscanf(timestr.c_str(), "%04d-%02d-%02dT%02d:%02d:%02d",
&t.tm_year, &t.tm_mon, &t.tm_mday,
&t.tm_hour, &t.tm_min, &t.tm_sec);
t.tm_year -= 1900; // This is required because my year should be the number of years since 1900
auto tp = std::chrono::system_clock::from_time_t(std::mktime(&t));
auto now = std::chrono::system_clock::now();
auto now_s = std::chrono::time_point_cast<std::chrono::seconds>(now);
auto tp_s = std::chrono::time_point_cast<std::chrono::seconds>(tp);
std::chrono::duration<double> diff = now-tp; // Huge number
auto elapsed = now_s - tp_s; // This value is massive and not as expected when printed out