There is some problem with datetime parsing, that I have no idea about great workaround.
Consider following code:
#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;
}
Problem is, that default year in datetime parser is 1400
, that is not leap, so there is no 29 february in this year. The main question is of course why 1400
year is default and not any leap year, anyway I need some nice workaround, any ideas?