【发布时间】:2016-03-25 22:33:52
【问题描述】:
日期时间解析存在一些问题,我不知道有什么好的解决方法。
考虑以下代码:
#include <iostream>
#include <string>
#include <boost/date_time/gregorian/gregorian.hpp>
namespace dt = boost::gregorian;
dt::date parse_date(const std::string& msg, const std::string& format)
{
std::stringstream s(msg);
dt::date_input_facet * f = new dt::date_input_facet();
if (format.empty())
{
f->set_iso_format();
}
else
{
f->format(format.c_str());
}
std::locale l(std::locale(), f);
s.imbue(l);
dt::date result;
s >> result;
return result;
}
int main()
{
const std::string v = "0229";
auto p = parse_date(v, "%m%d");
std::cout << p << std::endl;
}
问题是,日期时间解析器中的默认年份是1400,这不是闰年,所以今年没有 2 月 29 日。主要问题当然是为什么 1400 是默认年份而不是闰年,无论如何我需要一些好的解决方法,有什么想法吗?
【问题讨论】:
标签: c++ boost boost-date-time