结果与get_time不一致

问题描述

程序:

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

int main()
{
    std::string time_str = "2017-01-03 09:30:00";
    for(int i=0; i<5; i++)
    {
        std::istringstream ss(time_str);
        std::tm tm;
        ss >> std::get_time(&tm,"%Y-%m-%d %H:%M:%s");
        time_t t = std::mktime(&tm);
        std::cout << (int)t << " " << tm.tm_hour << std::endl;
    }
}

该程序的行为不一致。在装有MinGW 8.1.0的本地计算机上,它会打印

1483453800 8
1483457400 9
1483457400 9
1483457400 9
1483457400 9 

在Ideone上,在其Clang和GCC编译器上均会打印

-1 9
-1 9
-1 9
-1 9
-1 9

此行为是怎么回事? Ideone输出(对于time_t返回“ -1”)表明我在某个地方搞砸了。但是,MinGW的输出甚至更奇怪,因为循环的第一次迭代与所有其他迭代的结果不同。我的编译器和标志在注释中。

其他一些奇怪的行为是,如果我将time_str更改为“ 1970-01-01 00:00:00”,我会得到

18000 23
21600 0
21600 0
21600 0
21600 0

这真的很奇怪,这是由于不一致以及因为纪元存储为18000或21600秒而不是0。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)