Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / chrono / example / french.cpp
1 //  french.cpp  ----------------------------------------------------------//
2
3 //  Copyright 2010 Howard Hinnant
4 //  Copyright 2011 Vicente J. Botet Escriba
5 //  Distributed under the Boost Software License, Version 1.0.
6 //  See http://www.boost.org/LICENSE_1_0.txt
7
8 // Adapted to Boost from the original Hawards's code
9
10
11 #include <boost/chrono/config.hpp>
12 #include <boost/chrono/chrono_io.hpp>
13 #include <boost/chrono/process_cpu_clocks.hpp>
14 #include <boost/chrono/thread_clock.hpp>
15 #include <iostream>
16 #include <locale>
17
18
19 #if BOOST_CHRONO_VERSION==2
20 #include <boost/chrono/io/duration_units.hpp>
21
22     using namespace boost;
23     using namespace boost::chrono;
24
25     template <typename CharT=char>
26     class duration_units_fr: public duration_units_default<CharT>
27     {
28     public:
29       typedef CharT char_type;
30
31       explicit duration_units_fr(size_t refs = 0) :
32         duration_units_default<CharT>(refs)
33       {
34       }
35     protected:
36
37       using duration_units_default<CharT>::do_get_unit;
38       std::size_t do_get_plural_form(boost::int_least64_t value) const
39       {
40         return (value == -1 || value == 0 || value == 1) ? 0 : 1;
41       }
42
43       std::basic_string<CharT> do_get_unit(duration_style style, ratio<1> , std::size_t pf) const
44       {
45         static const CharT t[] =
46         { 's' };
47         static const std::basic_string<CharT> symbol(t, t + sizeof (t) / sizeof (t[0]));
48         static const CharT u[] =
49         { 's', 'e', 'c', 'o', 'n', 'd', 'e' };
50         static const std::basic_string<CharT> singular(u, u + sizeof (u) / sizeof (u[0]));
51         static const CharT v[] =
52         { 's', 'e', 'c', 'o', 'n', 'd', 'e', 's' };
53         static const std::basic_string<CharT> plural(v, v + sizeof (v) / sizeof (v[0]));
54
55         if (style == duration_style::symbol) return symbol;
56         if (pf == 0) return singular;
57         if (pf == 1) return plural;
58         // assert
59         //throw "exception";
60         return "";
61       }
62
63       std::basic_string<CharT> do_get_unit(duration_style style, ratio<60> , std::size_t pf) const
64       {
65         static const CharT t[] =
66         { 'm', 'i', 'n' };
67         static const std::basic_string<CharT> symbol(t, t + sizeof (t) / sizeof (t[0]));
68
69         static const CharT u[] =
70         { 'm', 'i', 'n', 'u', 't', 'e' };
71         static const std::basic_string<CharT> singular(u, u + sizeof (u) / sizeof (u[0]));
72         static const CharT v[] =
73         { 'm', 'i', 'n', 'u', 't', 'e', 's' };
74         static const std::basic_string<CharT> plural(v, v + sizeof (v) / sizeof (v[0]));
75
76         if (style == duration_style::symbol) return symbol;
77         if (pf == 0) return singular;
78         if (pf == 1) return plural;
79         // assert
80         //throw "exception";
81         return "";
82       }
83
84       std::basic_string<CharT> do_get_unit(duration_style style, ratio<3600> , std::size_t pf) const
85       {
86         static const CharT t[] =
87         { 'h' };
88         static const std::basic_string<CharT> symbol(t, t + sizeof (t) / sizeof (t[0]));
89         static const CharT u[] =
90         { 'h', 'e', 'u', 'r', 'e' };
91         static const std::basic_string<CharT> singular(u, u + sizeof (u) / sizeof (u[0]));
92         static const CharT v[] =
93         { 'h', 'e', 'u', 'r', 'e', 's' };
94         static const std::basic_string<CharT> plural(v, v + sizeof (v) / sizeof (v[0]));
95
96         if (style == duration_style::symbol) return symbol;
97         if (pf == 0) return singular;
98         if (pf == 1) return plural;
99         // assert
100         //throw "exception";
101         return "";
102       }
103     };
104 #endif
105
106 int main()
107 {
108     using std::cout;
109     using std::locale;
110     using namespace boost;
111     using namespace boost::chrono;
112
113 #if BOOST_CHRONO_VERSION==2
114     cout.imbue(locale(locale(), new duration_units_fr<>()));
115 #else
116     cout.imbue(locale(locale(), new duration_punct<char>
117         (
118             duration_punct<char>::use_long,
119             "secondes", "minutes", "heures",
120             "s", "m", "h"
121         )));
122 #endif
123     hours h(5);
124     minutes m(45);
125     seconds s(15);
126     milliseconds ms(763);
127     cout << h << ", " << m << ", " << s << " et " << ms << '\n';
128     cout << hours(0) << ", " << minutes(0) << ", " << s << " et " << ms << '\n';
129     return 0;
130 }