void MainMenu::calculate_time(Ref* pSender,std::string start, std::string end)
{
time_t val = string_time(start);
time_t val2 = string_time(end);
timeLeft = difftime(val2, val);
log(“timeLeft %d”,(int)(timeLeft));
isCounting = true;
}
time_t MainMenu::string_time(std::string date)
{
log("%s",date.c_str());
struct tm tm;
time_t val;
strptime(date.c_str(),"%FT%T%z",&tm);
val = mktime(&tm);
log(“string to long %ld\n”, (long)val);
return val;
}
my problem is that:
log(“timeLeft %d”,(int)(timeLeft)); gives me -1
I tried an online c++ compiler
time_t string_time(std::string date)
{
struct tm tm;
time_t val;
//memset(&tm, 0, sizeof(struct tm));
strptime(date.c_str(),"%FT%T%z",&tm);
val = mktime(&tm);
printf("%ld\n", (long)val);
return val;
}
int main()
{
time_t val = string_time(“2010-11-04T23:23:01Z”);
time_t val2 = string_time(“2010-11-05T01:23:01Z”);
double val3 = difftime(val2, val); printf("%d", (int)val3);
}
Result:
g+±4.9 -std=c++14 -O2 -Wall -Wextra -pedantic -pthread -pedantic-errors main.cpp -lm && ./a.out
1288912981
1288920181
7200
it gives me the exact result which is 7200