Boost C++ Libraries: Ticket #3967: Parsing dates using date_input_facet accepts wrong input https://svn.boost.org/trac10/ticket/3967 <p> Hi! </p> <p> 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. </p> <p> #include &lt;boost/assign/list_of.hpp&gt; </p> <p> #include &lt;boost/date_time/posix_time/posix_time.hpp&gt; #include &lt;boost/date_time/gregorian/gregorian.hpp&gt; #include &lt;boost/date_time/local_time/local_time.hpp&gt; #include &lt;boost/foreach.hpp&gt; #include &lt;boost/algorithm/string.hpp&gt; </p> <p> #include &lt;string&gt; #include &lt;list&gt; </p> <p> inline std::string determine_date_format(std::string const &amp; s) { </p> <blockquote> <p> using namespace boost::assign; using namespace boost::gregorian; </p> </blockquote> <blockquote> <p> std::list&lt;std::string&gt; possible_formats = <em> TODO: add others here! </em></p> <blockquote> <p> list_of ("%Y-%m-%d")("%Y/%m/%d")("%m/%d/%Y")("%d.%m.%Y"); </p> </blockquote> </blockquote> <blockquote> <p> bool inform_user = false; </p> </blockquote> <blockquote> <p> BOOST_FOREACH(std::string const &amp; format, possible_formats) { </p> <blockquote> <p> if (inform_user) { </p> <blockquote> <p> std::cout &lt;&lt; "Trying format '" &lt;&lt; format &lt;&lt; "' ..." &lt;&lt; std::endl; </p> </blockquote> <p> } </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> try { </p> <blockquote> <p> date_input_facet * input_facet = </p> <blockquote> <p> new date_input_facet(format.c_str()); </p> </blockquote> <p> std::istringstream iss(s); iss.imbue(std::locale(iss.getloc(), input_facet)); date d(not_a_date_time); </p> </blockquote> </blockquote> </blockquote> <blockquote> <blockquote> <blockquote> <p> iss &gt;&gt; d; if (!iss.fail() &amp;&amp; (!d.is_not_a_date())) { </p> <blockquote> <p> return format; </p> </blockquote> <p> } </p> </blockquote> </blockquote> </blockquote> <blockquote> <blockquote> <blockquote> <p> std::cout &lt;&lt; "WARNING: date format '" &lt;&lt; format </p> <blockquote> <p> &lt;&lt; "' does not match '" &lt;&lt; s &lt;&lt; "'." &lt;&lt; std::endl; </p> </blockquote> <p> inform_user = true; </p> </blockquote> <p> } catch(...) { } </p> </blockquote> <p> } </p> </blockquote> <blockquote> <p> std::cout &lt;&lt; "WARNING: date format not recognized. " </p> <blockquote> <p> &lt;&lt; "Please reconfigure your measurement equipment " &lt;&lt; "to ISO standard output!" &lt;&lt; std::endl; </p> </blockquote> </blockquote> <blockquote> <p> return ""; </p> </blockquote> <p> } </p> <p> int main() { </p> <blockquote> <p> std::string the_date = "05.02.2008"; std::string format = determine_date_format(the_date); std::cout &lt;&lt; the_date &lt;&lt; " has date-time-format " </p> <blockquote> <p> &lt;&lt; format &lt;&lt; std::endl; </p> </blockquote> <p> return 0; </p> </blockquote> <p> } </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3967 Trac 1.4.3 anonymous Sat, 08 Jun 2013 19:41:16 GMT <link>https://svn.boost.org/trac10/ticket/3967#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3967#comment:1</guid> <description> <p> Hello! </p> <p> I can confirm the problem in boost 1.49 under debian linux. </p> <p> boost </p> </description> <category>Ticket</category> </item> </channel> </rss>