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 <boost/assign/list_of.hpp>
</p>
<p>
#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>
</p>
<p>
#include <string>
#include <list>
</p>
<p>
inline std::string determine_date_format(std::string const & s)
{
</p>
<blockquote>
<p>
using namespace boost::assign;
using namespace boost::gregorian;
</p>
</blockquote>
<blockquote>
<p>
std::list<std::string> 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 & format, possible_formats)
{
</p>
<blockquote>
<p>
if (inform_user)
{
</p>
<blockquote>
<p>
std::cout << "Trying format '" << format << "' ..." << 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 >> d;
if (!iss.fail() && (!d.is_not_a_date()))
{
</p>
<blockquote>
<p>
return format;
</p>
</blockquote>
<p>
}
</p>
</blockquote>
</blockquote>
</blockquote>
<blockquote>
<blockquote>
<blockquote>
<p>
std::cout << "WARNING: date format '" << format
</p>
<blockquote>
<p>
<< "' does not match '" << s << "'." << std::endl;
</p>
</blockquote>
<p>
inform_user = true;
</p>
</blockquote>
<p>
}
catch(...)
{
}
</p>
</blockquote>
<p>
}
</p>
</blockquote>
<blockquote>
<p>
std::cout << "WARNING: date format not recognized. "
</p>
<blockquote>
<p>
<< "Please reconfigure your measurement equipment "
<< "to ISO standard output!"
<< 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 << the_date << " has date-time-format "
</p>
<blockquote>
<p>
<< format << 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>