[Boost-bugs] [Boost C++ Libraries] #3967: Parsing dates using date_input_facet accepts wrong input

Subject: [Boost-bugs] [Boost C++ Libraries] #3967: Parsing dates using date_input_facet accepts wrong input
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2010-03-01 13:17:42


#3967: Parsing dates using date_input_facet accepts wrong input
--------------------------+-------------------------------------------------
 Reporter: anonymous | Owner: az_sw_dude
     Type: Bugs | Status: new
Milestone: Boost 1.43.0 | Component: date_time
  Version: Boost 1.37.0 | Severity: Showstopper
 Keywords: |
--------------------------+-------------------------------------------------
 Hi!

 I got no answer on the ML, but I consider this a bug.
 With the code below I try to automagically determine the date
 format found in input files. I'd expect the code below to find the
 date format "%d.%m.%Y" for "05.02.2008", but I got surprised by
 obtaining "%m/%d/%Y".
 It seems like date accepts '.' where I said "expect '/'", so I think
 this should be changed.


 #include <boost/assign/list_of.hpp>

 #include <boost/date_time/posix_time/posix_time.hpp>
 #include <boost/date_time/gregorian/gregorian.hpp>
 #include <boost/date_time/local_time/local_time.hpp>
 #include <boost/foreach.hpp>
 #include <boost/algorithm/string.hpp>

 #include <string>
 #include <list>

 inline std::string determine_date_format(std::string const & s)
 {
     using namespace boost::assign;
     using namespace boost::gregorian;

     std::list<std::string> possible_formats = // TODO: add others here!
         list_of ("%Y-%m-%d")("%Y/%m/%d")("%m/%d/%Y")("%d.%m.%Y");

     bool inform_user = false;

     BOOST_FOREACH(std::string const & format, possible_formats)
     {
         if (inform_user)
         {
             std::cout << "Trying format '" << format << "' ..." <<
 std::endl;
         }

         try
         {
             date_input_facet * input_facet =
                 new date_input_facet(format.c_str());
             std::istringstream iss(s);
             iss.imbue(std::locale(iss.getloc(), input_facet));
             date d(not_a_date_time);

             iss >> d;
             if (!iss.fail() && (!d.is_not_a_date()))
             {
                 return format;
             }

             std::cout << "WARNING: date format '" << format
                       << "' does not match '" << s << "'." << std::endl;
             inform_user = true;
         }
         catch(...)
         {
         }
     }

     std::cout << "WARNING: date format not recognized. "
               << "Please reconfigure your measurement equipment "
               << "to ISO standard output!"
               << std::endl;

     return "";
 }

 int main()
 {
     std::string the_date = "05.02.2008";
     std::string format = determine_date_format(the_date);
     std::cout << the_date << " has date-time-format "
               << format << std::endl;
     return 0;
 }

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/3967>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:02 UTC