gpt4 book ai didi

c++ - std::get_time 没有解析日期

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:33:54 24 4
gpt4 key购买 nike

如果我尝试使用 std::get_timetm 中设置日期什么也没有发生,但是输入流处于失败状态,这意味着解析错误已经发生了。

下面的代码有什么问题?

{   // setting time works
std::tm t{};
std::istringstream ss("01:02:03");
ss.imbue(std::locale("de_DE"));
ss >> std::get_time(&t, "%H:%M:%S");
std::cout << ss.fail() << std::endl;
std::cout << std::put_time(&t, "%c") << '\n';
}

{ // setting day of month did not work
std::tm t{};
std::istringstream ss("2");
ss.imbue(std::locale("de_DE"));
ss >> std::get_time(&t, "%d");
std::cout << ss.fail() << std::endl;
std::cout << std::put_time(&t, "%c") << '\n';
}

输出:

0 
Sun Jan 0 01:02:03 1900
1
Sun Jan 0 00:00:00 1900

最佳答案

您需要将前导零传递给日期:

std::istringstream ss("02");

编辑:现在我注意到根据 cppreference :

parses the day of the month as a decimal number (range [01,31]), leading zeroes permitted but not required.

也许这是一个错误?

编辑:Bug report here

关于c++ - std::get_time 没有解析日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50579469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com