// g++ -std=c++11 -o example.x -lboost_date_time main.cpp #include #include using std::locale; using boost::date_time::not_a_date_time; using boost::gregorian::date; using boost::posix_time::seconds; using boost::posix_time::milliseconds; using namespace boost::local_time; std::string to_string(local_date_time dt, const locale& locale) { std::ostringstream stream; stream.imbue(locale); stream << dt; return stream.str(); } local_date_time to_local_date_time(const std::string& string, const locale& locale) { local_date_time dt(not_a_date_time); std::istringstream stream(string); stream.imbue(locale); stream >> dt; return dt; } void test_symmetry(local_date_time time, const locale& locale) { std::cout << "Original time, with default format: " << time << "\n"; std::cout << "Original time, with our format: " << to_string(time, locale) << "\n"; auto time2 = to_local_date_time(to_string(time, locale), locale); std::cout << "New time, with default format: " << time2 << "\n"; std::cout << "New time, with our format: " << to_string(time2, locale) << "\n\n"; } int main() { const locale DOT_LOCALE( locale( locale::classic(), new local_time_facet("blabla %s") ), new local_time_input_facet("blabla %s") ); struct comma_numpunct : std::numpunct { char do_decimal_point() const override { return ','; } }; const locale COMMA_LOCALE( DOT_LOCALE, new comma_numpunct() ); const local_date_time TIME( date(min_date_time), seconds(1) + milliseconds(234), nullptr, local_date_time::NOT_DATE_TIME_ON_ERROR ); test_symmetry(TIME, DOT_LOCALE); test_symmetry(TIME, COMMA_LOCALE); return 0; }