std :: get_time没有解析日期 [英] std::get_time did not parse day

查看:129
本文介绍了std :: get_time没有解析日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试将日期设置为 tm std :: get_time ,则什么也没有发生,但是输入流处于失败状态,这意味着已发生解析错误。

If I try to set the day in a tm with std::get_time nothing happens but, input stream is in a fail state, which means that a parse error has been occured.

以下代码有什么问题?

{   // 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


推荐答案

您需要在当天传递前导零:

You need to pass a leading zero to the day:

std :: istringstream ss( 02);

编辑:现在我请注意,根据 cppreference

Now I notice that according to cppreference:


将月份中的日期解析为十进制数字(范围[01,31]),
前导零允许但不是必需

也许是个错误?

编辑:在此处发现错误报告

这篇关于std :: get_time没有解析日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆