std :: get_time在Visual C ++ 2017上失败

时间:2018-06-18 21:18:58

标签: c++ visual-c++

我试图在Windows上使用std::get_time解析一个简单的日期时间字符串,但它不起作用,我不知道为什么。

当我调试时,我可以看到只有小时,分钟和秒被正确解析,而不是其余的。

拨打' str_stream.fail()'总是返回真实。

我想避免使用boost::date_time并尝试解决此问题。

从引用中读取详细信息,是否与std::time_get facets的std :: locale有关?如果是这样,如何正确设置?

我想将该日期和时间解析为UTC。

我在Windows 10下使用Visual C ++ 2017。

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

int main() {
    std::string tmp("20180102 12:24:33");
    std::stringstream str_stream(tmp);      
    tm data;
    str_stream >> std::get_time(&data, "%Y%m%d %H:%M:%S");
    if (str_stream.fail())  {
        std::cout << "failed" << std::endl;
    }
    else {
        std::cout << "success" << std::endl;
    }
}

0 个答案:

没有答案