Boost datetime posix_time的time_input_facet无法解析单数字日期
Boost datetime posix_time组件time_input_facet单数字日期解析缺陷说明
使用Boost datetime posix_time组件的time_input_facet时,存在无法解析单数字月份日期的问题。最初提交问题时未查询到该缺陷的相关记录,已采纳的回答指出该问题已在Boost官方date_time项目的issue 106中记录。
#include <boost/date_time/posix_time/posix_time.hpp> #include <vector> void date_from_string(const std::string& format, const std::string& date_str, boost::posix_time::ptime& date) { auto* facet = new boost::posix_time::time_input_facet; facet->format(format.c_str()); std::stringstream ss(date_str); ss.imbue(std::locale(std::locale::classic(), facet)); ss >> date; // delete facet; > "Your program has UB. delete facet; leads to double-free." } int main() { std::cout << "Boost Version: " << BOOST_VERSION << std::endl; // 107500 // Boost Version: 107500 for (auto& i : std::vector<std::pair<std::string,std::string>>{ {"%a, %d %b %Y %H:%M:%S %ZP","Thu, 17 Oct 2021 15:00:00 GMT"}, // 2021-Oct-17 15:00:00 from str: Thu, 17 Oct 2021 15:00:00 GMT using: %a, %d %b %Y %H:%M:%S %ZP {"%a, %d %b %Y %H:%M:%S %ZP","Thu, 7 Oct 2021 15:00:00 GMT"}, // not-a-date-time from str: Thu, 7 Oct 2021 15:00:00 GMT using: %a, %d %b %Y %H:%M:%S %ZP {"%d", "7"}, // not-a-date-time from str: 7 using: %d {"%d", "07"} // 1400-Jan-07 00:00:00 from str: 07 using: %d }) { boost::posix_time::ptime m_date; date_from_string(i.first, i.second,m_date); std::cout << boost::posix_time::to_simple_string(m_date) << " from str: " << i.second << " using: " << i.first << std::endl; } }
%d 格式符表示十进制月份日期,取值范围01到31。用于输入解析时,前导零是可选的。(标注为提问者添加)
格式标记的官方说明可参考Boost 1.75.0版本的date_time_io相关文档。
内容的提问来源于stack exchange,提问作者modestmerit
相关产品推荐
相关产品推荐

