我正在学习C++
并制作一个简单的应用程序,下面的代码将std::string
解析为ptime
ptimePointer CsvHelper::string_to_ptime(const std::string& s, const int mode,
timePointer pt)
{
const std::locale formats[] = {
std::locale(std::locale::classic(),new boost::posix_time::time_input_facet("%d/%m/%Y H:%M:%S")),
std::locale(std::locale::classic(),new boost::posix_time::time_input_facet("%H:%M"))
};
std::istringstream is(s);
is.imbue(formats[mode]);
is >> *pt;
}
return pt;
问题是,如果我传递一个字母"a"
,它将被创建一个具有特殊值“非日期时间”的ptime
。
好的,所以我检查了文档,并学习了如果我用默认构造函数创建一个ptime
,就会得到一个具有相同类型的ptime
。因此,在我的代码中,我使用以下代码验证了输入:
boost::posix_time::ptime ptime;
if (*this->myDate != ptime)
{
...
} else
{
treat the problem;
}
我觉得这不是一个明确的解决方案,有什么更好的方法来验证这个日期?
发布于 2014-05-15 05:46:41
ptime有一个is_not_a_date_time()函数。所以你可以
if(myDate.is_not_a_date_time())
do_something();
https://stackoverflow.com/questions/23679450
复制相似问题