1 | // g++ -std=c++11 -o example.x -lboost_date_time main.cpp
|
---|
2 | #include <locale>
|
---|
3 | #include <boost/date_time/local_time/local_time.hpp>
|
---|
4 |
|
---|
5 | using std::locale;
|
---|
6 | using boost::date_time::not_a_date_time;
|
---|
7 | using boost::gregorian::date;
|
---|
8 | using boost::posix_time::seconds;
|
---|
9 | using boost::posix_time::milliseconds;
|
---|
10 | using namespace boost::local_time;
|
---|
11 |
|
---|
12 |
|
---|
13 | std::string to_string(local_date_time dt, const locale& locale)
|
---|
14 | {
|
---|
15 | std::ostringstream stream;
|
---|
16 | stream.imbue(locale);
|
---|
17 | stream << dt;
|
---|
18 | return stream.str();
|
---|
19 | }
|
---|
20 |
|
---|
21 |
|
---|
22 | local_date_time to_local_date_time(const std::string& string, const locale& locale)
|
---|
23 | {
|
---|
24 | local_date_time dt(not_a_date_time);
|
---|
25 |
|
---|
26 | std::istringstream stream(string);
|
---|
27 | stream.imbue(locale);
|
---|
28 | stream >> dt;
|
---|
29 |
|
---|
30 | return dt;
|
---|
31 | }
|
---|
32 |
|
---|
33 |
|
---|
34 | void test_symmetry(local_date_time time, const locale& locale)
|
---|
35 | {
|
---|
36 | std::cout << "Original time, with default format: " << time << "\n";
|
---|
37 | std::cout << "Original time, with our format: " << to_string(time, locale) << "\n";
|
---|
38 |
|
---|
39 | auto time2 = to_local_date_time(to_string(time, locale), locale);
|
---|
40 |
|
---|
41 | std::cout << "New time, with default format: " << time2 << "\n";
|
---|
42 | std::cout << "New time, with our format: " << to_string(time2, locale) << "\n\n";
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | int main()
|
---|
47 | {
|
---|
48 | const locale DOT_LOCALE(
|
---|
49 | locale(
|
---|
50 | locale::classic(),
|
---|
51 | new local_time_facet("blabla %s")
|
---|
52 | ),
|
---|
53 | new local_time_input_facet("blabla %s")
|
---|
54 | );
|
---|
55 |
|
---|
56 | struct comma_numpunct : std::numpunct<char>
|
---|
57 | {
|
---|
58 | char do_decimal_point() const override { return ','; }
|
---|
59 | };
|
---|
60 |
|
---|
61 | const locale COMMA_LOCALE(
|
---|
62 | DOT_LOCALE,
|
---|
63 | new comma_numpunct()
|
---|
64 | );
|
---|
65 |
|
---|
66 | const local_date_time TIME(
|
---|
67 | date(min_date_time),
|
---|
68 | seconds(1) + milliseconds(234),
|
---|
69 | nullptr,
|
---|
70 | local_date_time::NOT_DATE_TIME_ON_ERROR
|
---|
71 | );
|
---|
72 |
|
---|
73 | test_symmetry(TIME, DOT_LOCALE);
|
---|
74 | test_symmetry(TIME, COMMA_LOCALE);
|
---|
75 | return 0;
|
---|
76 | }
|
---|