CrazyCoder
CrazyCoder

Reputation: 780

Difference between two datetime in c++

I have a two datetime in string format. for example

string str1 = "20160302101710"; //YYYYMMDDHHMMSS
string str2 = "20160302101715"; //same as above

Now, i need difference between two datetime is greater that 24Hours.

I have tried my problem as follows

I parsed the both string and put as follows:

#include <iostream>
using namespace std;

int main() {

time_t rawtime, rawtime1;
struct tm * timeinfo, *timeinfo1;

timeinfo->tm_year = 2016 ;
timeinfo->tm_mon = 03;
timeinfo->tm_mday = 02;
timeinfo->tm_hour = 10;
timeinfo->tm_min = 17;
timeinfo->tm_sec = 10;
rawtime = mktime(timeinfo); 


timeinfo1->tm_year = 2016 ;
timeinfo1->tm_mon = 03;
timeinfo1->tm_mday = 02;
timeinfo1->tm_hour = 10;
timeinfo1->tm_min = 17;
timeinfo1->tm_sec = 15;
rawtime1 =  mktime(timeinfo1);

cout<<"Diff: "<<     difftime(rawtime1,rawtime);

return 0;
}

But After execution output is nothing. Can anyone help me how to get the correct answer. Once I will get the diff in second. then i will convert into hours.

Thanks,

Upvotes: 0

Views: 3518

Answers (4)

Howard Hinnant
Howard Hinnant

Reputation: 218750

This can be done more easily with Howard Hinnant's free, open-source header-only datetime library:

#include "date/date.h"
#include <string>
#include <iostream>
#include <sstream>

int
main()
{
    using namespace std;
    using namespace std::chrono;
    using namespace date;
    string str1 = "20160302101710"; //YYYYMMDDHHMMSS
    string str2 = "20160302101715"; //same as above
    istringstream in{str1 + ' ' + str2};
    sys_seconds t1, t2;
    string fmt = " %Y%m%d%H%M%S";
    in >> parse(fmt, t1) >> parse(fmt, t2);
    cout << "Diff: " << t2 - t1 << '\n';
}

Output:

Diff: 5s

Upvotes: 1

mustafagonul
mustafagonul

Reputation: 1209

You can use Boost.Date_Time.

Examples

Upvotes: 1

MikeCAT
MikeCAT

Reputation: 75062

You have to initialize the pointer with valid buffer before dereferencing them.

#include <iostream>
using namespace std;

int main() {

    time_t rawtime, rawtime1;
    struct tm buf1, buf2;
    struct tm * timeinfo = &buf1, *timeinfo1 = &buf2;

    timeinfo->tm_year = 2016 ;
    timeinfo->tm_mon = 03;
    timeinfo->tm_mday = 02;
    timeinfo->tm_hour = 10;
    timeinfo->tm_min = 17;
    timeinfo->tm_sec = 10;
    rawtime = mktime(timeinfo); 


    timeinfo1->tm_year = 2016 ;
    timeinfo1->tm_mon = 03;
    timeinfo1->tm_mday = 02;
    timeinfo1->tm_hour = 10;
    timeinfo1->tm_min = 17;
    timeinfo1->tm_sec = 15;
    rawtime1 =  mktime(timeinfo1);

    cout<<"Diff: "<<     difftime(rawtime1,rawtime);

    return 0;
}

Or you may just use structs without pointer variables.

#include <iostream>
using namespace std;

int main() {

    time_t rawtime, rawtime1;
    struct tm timeinfo, timeinfo1;

    timeinfo.tm_year = 2016 ;
    timeinfo.tm_mon = 03;
    timeinfo.tm_mday = 02;
    timeinfo.tm_hour = 10;
    timeinfo.tm_min = 17;
    timeinfo.tm_sec = 10;
    rawtime = mktime(&timeinfo); 


    timeinfo1.tm_year = 2016 ;
    timeinfo1.tm_mon = 03;
    timeinfo1.tm_mday = 02;
    timeinfo1.tm_hour = 10;
    timeinfo1.tm_min = 17;
    timeinfo1.tm_sec = 15;
    rawtime1 =  mktime(&timeinfo1);

    cout<<"Diff: "<<     difftime(rawtime1,rawtime);

    return 0;
}

Upvotes: 4

Umamahesh P
Umamahesh P

Reputation: 1244

As indicated by R_Kapp, you need to allocate memory or just define a variable.

Modified program:

int main() {
time_t rawtime, rawtime1;
struct tm timeinfo, timeinfo1;

timeinfo.tm_year = 2016 ;
timeinfo.tm_mon = 03;
timeinfo.tm_mday = 02;
timeinfo.tm_hour = 10;
timeinfo.tm_min = 17;
timeinfo.tm_sec = 10;
rawtime = mktime(&timeinfo);


timeinfo1.tm_year = 2016 ;
timeinfo1.tm_mon = 03;
timeinfo1.tm_mday = 02;
timeinfo1.tm_hour = 10;
timeinfo1.tm_min = 17;
timeinfo1.tm_sec = 15;
rawtime1 =  mktime(&timeinfo1);

cout<<"Diff: "<<     difftime(rawtime1,rawtime);

return 0;
}

Upvotes: 2

Related Questions