I need to find the difference between two date to time periods in seconds. Here is what I did: Assigned the date and time variables using sscanf. Assigned the variables to struct tm object (timeInfo).
Example of time string: Thu, 29 Mar 2018 11:45:00
Everything works fine as expected. Now comes the problem. I have obtained the current time as seen in the program. But when I get currentTime the members such as tm_wday,tm_yday and tm_isdst are assigned to it whereas the string I took doesn't have these details. Should I assign them inorder to obtain the diff seconds right?
Is there any workaround?
int GetDiffTimeInMilliseconds(LPCSTR expireValue)
{
const char monthNames[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
int hh,mm,ss,day,year,monthNumber;
char weekday[10],month[5],timezone[5];
struct tm timeInfo,currentTime;
time_t now;
sscanf(expireValue,"%[^,], %d %s %d %d:%d:%d %s",weekday,&day,month,&year,&hh,&mm,&ss,timezone);
cout<<weekday<<" "<<day<<" "<<month<<" "<<year<<" "<<hh<<":"<<mm<<":"<<ss;
monthNumber = (strstr(monthNames,month)-monthNames)/3;
timeInfo.tm_mday = day;
timeInfo.tm_mon = monthNumber;
timeInfo.tm_year = year - 1900;
timeInfo.tm_hour = hh;
timeInfo.tm_min = mm;
timeInfo.tm_sec = ss;
time(&now);
currentTime = *localtime(&now);
INT64 seconds= difftime(mktime(&timeInfo),now);
cout<<"The seconds are: "<<seconds;
}