c ++中两个日期时间之间的差异

时间:2016-03-02 14:05:34

标签: linux datetime c++11

我有一个字符串格式的两个日期时间。例如

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

现在,我需要两个日期时间之间的差异大于24小时。

我已经尝试了以下问题

我解析了这两个字符串并按如下方式进行了解释:

#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;
}

但执行输出后什么都没有。 任何人都可以帮助我如何得到正确的答案。 一旦我得到第二个差异。然后我会转换成几小时。

谢谢,

4 个答案:

答案 0 :(得分:3)

在解除引用之前,必须使用有效缓冲区初始化指针。

#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;
}

或者你可以只使用没有指针变量的结构。

#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;
}

答案 1 :(得分:2)

如R_Kapp所示,您需要分配内存或仅定义变量。

修改后的程序:

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

答案 2 :(得分:1)

您可以使用Boost.Date_Time

Examples

答案 3 :(得分:1)

使用Howard Hinnant's free, open-source header-only datetime库可以更轻松地完成此操作:

#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';
}

输出:

Diff: 5s