سؤال

I'm learning C++ and making a simple application, and I have the following code that parses a std::string to a 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;

The problem is that if I pass a letter "a", it will be created a ptime with a special value "not-a-date-time".

Ok, so I check the docs and learned the if I create a ptime with the default constructor I get a ptime with the same kind. So, in my code I validated the input with the following code:

boost::posix_time::ptime ptime;
if (*this->myDate != ptime)
{
    ...
} else 
{
    treat the problem;
}

I feel that this is not a clear solution, what is a better way to validate this date?

هل كانت مفيدة؟

المحلول

ptime has a is_not_a_date_time() function. so you could do

if(myDate.is_not_a_date_time())
   do_something();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top