Boost logo

Boost Users :

From: Christian Henning (chhenning_at_[hidden])
Date: 2006-03-21 16:22:32


Hi Dean, thanks for the answer.

Since, I know the format of the date strings I could also use
boost::regex for the general format and then feeding a date object
with the values. If I get an exception during the date object
contruction I know that the user provided me with garbage.

Here is some code:

   // date format: mm-dd-yyyy
   boost::regex oDateReg( "\\d{2}-\\d{2}-\\d{4}" );
   std::string strDate( "11-33-1997" );

   if( boost::regex_match( strDate
                         , oDateReg ))
   {
      try
      {
         std::string strMonth = strDate.substr( 0, 2 );
         std::string strDay = strDate.substr( 3, 2 );
         std::string strYear = strDate.substr( 6, 4 );

         unsigned int nYear = boost::lexical_cast<unsigned int>(strYear);
         unsigned int nMonth = boost::lexical_cast<unsigned int>(strMonth);
         unsigned int nDay = boost::lexical_cast<unsigned int>(strDay);

         date d( nYear, nMonth, nDay );

         date::ymd_type ymd = d.year_month_day();
      }
      catch( std::out_of_range oEx )
      {
         std::cout << "Error: " << oEx.what() << std::endl;
      }
   }

Any thoughts on that?

Thanks again,
Christian


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net