2

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;

}
KESHAV K
  • 63
  • 10

3 Answers3

0

Try this:

time_t now;

time(&now);
struct tm *currentTime = localtime(&now);
Archie Yalakki
  • 512
  • 4
  • 12
  • I got that part of the code but in the timeInfo object, members such as **tm_wday, tm_yday and tm_isdst** are unknown. But in case of local time it's filled, Now I find the difference between given time and current time,I will get the wrong answer. – KESHAV K Feb 28 '18 at 10:43
0

Solution using std::chrono

#include <chrono> //std::chrono
#include <iostream> //std::cout
#include <thread> //std::this_thread::sleep_for

auto get_dif_duration(const std::chrono::time_point<std::chrono::system_clock> & time_point)
{
    return std::chrono::system_clock::now() - time_point;
}

int main()
{

    using namespace std::chrono_literals;

    auto start = std::chrono::system_clock::now();

    std::this_thread::sleep_for(2s);

    auto seconds = std::chrono::duration_cast<std::chrono::seconds>(get_dif_duration(start));

    std::cout << "Waited: " << seconds.count() << " seconds\n";

}
Smit Ycyken
  • 1,189
  • 1
  • 11
  • 25
0

I did it. This is the updated code of what I did to find the diff between given time and the present time.

Example of the date to time string: Sat, 31 Mar 2018 09:41:28 GMT

INT64 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;

time(&now);
currentTime = *localtime(&now);

sscanf(expireValue,"%3[^,], %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;

INT64 seconds= difftime(mktime(&timeInfo),mktime(&currentTime));

cout<<"The seconds are : "<<seconds;

return seconds;

}

KESHAV K
  • 63
  • 10