Converting date

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

By the way I’m using App42’s Timer Service for cocos2d-x http://api.shephertz.com/app42-docs/timer-service/

I’m trying to get the difference between

app42TimerResponse->app42Timer.endTime

app42TimerResponse->app42Timer.startTime

When, where and with/for which calls?

For which call?

Look into the API docs:
http://api.shephertz.com/app42-docs/timer-service/#start-timer

I also suggest to use the new STL time functionalities:
http://www.cplusplus.com/reference/chrono/

I’d follow @IQD and use the STL for this. Nothing like using that your native language offers natively. It probably works :slight_smile: and is more portable to other platforms (potentially :slight_smile: )

got this already, just had to change the format “%FT%T%z”