1
日期時間解析存在一些問題,我不知道有什麼好的解決方法。加速日期解析29FEB
考慮下面的代碼:
#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
年是默認的,而不是任何閏年,無論如何,我需要一些很好的解決方法,任何想法?
有一個partial_date,但可悲的是它不尊重他使用的格式字符串。如果「2902」而不是「0229」可以使用,他可以使用[this](http://coliru.stacked-crooked.com/a/8c53d94f3c5e1f44)。 – llonesmiz
好找,@cv_and_he。我想知道'partial_date'對於其他任務是多麼有用(有一天會看)。這裏是[X3版本](http://coliru.stacked-crooked.com/a/752c3ab1fdd5b4e3)的完整性 – sehe
有趣。當然,我只想增加一年,但我不喜歡這種方式,但可能是最好的。這是非常有趣的解決方案,使用partial_date。 – ForEveR