c++ - Locale does not recognized for custom facet parsing Date / Time -
i looking sample convert date seconds arbitrary epoch (dates minor standard 1970)
and found following sample (from answer to): how parse date/time string?
#define boost_all_no_lib #include <iostream> #include <sstream> #include <locale> #include <boost/date_time.hpp> namespace bt = boost::posix_time; const std::locale formats[] = { std::locale(std::locale::classic(),new bt::time_input_facet("%y-%m-%d %h:%m:%s")), std::locale(std::locale::classic(),new bt::time_input_facet("%y/%m/%d %h:%m:%s")), std::locale(std::locale::classic(),new bt::time_input_facet("%d.%m.%y %h:%m:%s")), std::locale(std::locale::classic(),new bt::time_input_facet("%y-%m-%d"))}; const size_t formats_n = sizeof(formats)/sizeof(formats[0]); std::time_t pt_to_time_t(const bt::ptime& pt) { bt::ptime timet_start(boost::gregorian::date(1970,1,1)); bt::time_duration diff = pt - timet_start; return diff.ticks()/bt::time_duration::rep_type::ticks_per_second; } void seconds_from_epoch(const std::string& s) { bt::ptime pt; for(size_t i=0; i<formats_n; ++i) { std::istringstream is(s); is.imbue(formats[i]); >> pt; if(pt != bt::ptime()) break; } std::cout << " ptime " << pt << '\n'; std::cout << " seconds epoch " << pt_to_time_t(pt) << '\n'; } int main() { seconds_from_epoch("2004-03-21 12:45:33"); seconds_from_epoch("2004/03/21 12:45:33"); seconds_from_epoch("23.09.2004 04:12:21"); seconds_from_epoch("2003-02-11"); } problem is, apparently doesn't work in vc++ 2013 / boost 1.59, dates print "not date" (the condition in loop "if(pt != bt::ptime())" never true).
digging bit in debugger "is >> pt", seems msvc stl looks locale in global table , not find it.
is code missing something?
update: seems msvc specific, in linux open suse 13.2 gcc 4.9 , boost 1.59, program works damn fine.
Comments
Post a Comment