Opened 13 years ago
Last modified 9 years ago
#3967 new Bugs
Parsing dates using date_input_facet accepts wrong input
Reported by: | anonymous | Owned by: | az_sw_dude |
---|---|---|---|
Milestone: | Boost 1.43.0 | Component: | date_time |
Version: | Boost 1.37.0 | Severity: | Showstopper |
Keywords: | Cc: |
Description
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;
}
Hello!
I can confirm the problem in boost 1.49 under debian linux.
boost