]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/date_time/gregorian/greg_facet.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / date_time / gregorian / greg_facet.hpp
1 #ifndef GREGORIAN_FACET_HPP___
2 #define GREGORIAN_FACET_HPP___
3
4 /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
5 * Use, modification and distribution is subject to the
6 * Boost Software License, Version 1.0. (See accompanying
7 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
8 * Author: Jeff Garland, Bart Garst
9 * $Date$
10 */
11
12 #include <boost/date_time/compiler_config.hpp>
13 #include <boost/date_time/gregorian/gregorian_types.hpp>
14 #include <boost/date_time/date_formatting_locales.hpp> // sets BOOST_DATE_TIME_NO_LOCALE
15 #include <boost/date_time/gregorian/parsers.hpp>
16
17 //This file is basically commented out if locales are not supported
18 #ifndef BOOST_DATE_TIME_NO_LOCALE
19
20 #include <string>
21 #include <memory>
22 #include <locale>
23 #include <iostream>
24 #include <exception>
25
26 namespace boost {
27 namespace gregorian {
28
29 //! Configuration of the output facet template
30 struct BOOST_SYMBOL_VISIBLE greg_facet_config
31 {
32 typedef boost::gregorian::greg_month month_type;
33 typedef boost::date_time::special_values special_value_enum;
34 typedef boost::gregorian::months_of_year month_enum;
35 typedef boost::date_time::weekdays weekday_enum;
36 };
37
38 #if defined(USE_DATE_TIME_PRE_1_33_FACET_IO)
39 //! Create the base facet type for gregorian::date
40 typedef boost::date_time::date_names_put<greg_facet_config> greg_base_facet;
41
42 //! ostream operator for gregorian::date
43 /*! Uses the date facet to determine various output parameters including:
44 * - string values for the month (eg: Jan, Feb, Mar) (default: English)
45 * - string values for special values (eg: not-a-date-time) (default: English)
46 * - selection of long, short strings, or numerical month representation (default: short string)
47 * - month day year order (default yyyy-mmm-dd)
48 */
49 template <class charT, class traits>
50 inline
51 std::basic_ostream<charT, traits>&
52 operator<<(std::basic_ostream<charT, traits>& os, const date& d)
53 {
54 typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
55 typedef boost::date_time::ostream_date_formatter<date, facet_def, charT> greg_ostream_formatter;
56 greg_ostream_formatter::date_put(d, os);
57 return os;
58 }
59
60 //! operator<< for gregorian::greg_month typically streaming: Jan, Feb, Mar...
61 /*! Uses the date facet to determine output string as well as selection of long or short strings.
62 * Default if no facet is installed is to output a 2 wide numeric value for the month
63 * eg: 01 == Jan, 02 == Feb, ... 12 == Dec.
64 */
65 template <class charT, class traits>
66 inline
67 std::basic_ostream<charT, traits>&
68 operator<<(std::basic_ostream<charT, traits>& os, const greg_month& m)
69 {
70 typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
71 typedef boost::date_time::ostream_month_formatter<facet_def, charT> greg_month_formatter;
72 std::locale locale = os.getloc();
73 if (std::has_facet<facet_def>(locale)) {
74 const facet_def& f = std::use_facet<facet_def>(locale);
75 greg_month_formatter::format_month(m, os, f);
76
77 }
78 else { //default to numeric
79 charT fill_char = '0';
80 os << std::setw(2) << std::setfill(fill_char) << m.as_number();
81 }
82
83 return os;
84 }
85
86 //! operator<< for gregorian::greg_weekday typically streaming: Sun, Mon, Tue, ...
87 /*! Uses the date facet to determine output string as well as selection of long or short string.
88 * Default if no facet is installed is to output a 3 char english string for the
89 * day of the week.
90 */
91 template <class charT, class traits>
92 inline
93 std::basic_ostream<charT, traits>&
94 operator<<(std::basic_ostream<charT, traits>& os, const greg_weekday& wd)
95 {
96 typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
97 typedef boost::date_time::ostream_weekday_formatter<greg_weekday, facet_def, charT> greg_weekday_formatter;
98 std::locale locale = os.getloc();
99 if (std::has_facet<facet_def>(locale)) {
100 const facet_def& f = std::use_facet<facet_def>(locale);
101 greg_weekday_formatter::format_weekday(wd, os, f, true);
102 }
103 else { //default to short English string eg: Sun, Mon, Tue, Wed...
104 os << wd.as_short_string();
105 }
106
107 return os;
108 }
109
110 //! operator<< for gregorian::date_period typical output: [2002-Jan-01/2002-Jan-31]
111 /*! Uses the date facet to determine output string as well as selection of long
112 * or short string fr dates.
113 * Default if no facet is installed is to output a 3 char english string for the
114 * day of the week.
115 */
116 template <class charT, class traits>
117 inline
118 std::basic_ostream<charT, traits>&
119 operator<<(std::basic_ostream<charT, traits>& os, const date_period& dp)
120 {
121 os << '['; //TODO: facet or manipulator for periods?
122 os << dp.begin();
123 os << '/'; //TODO: facet or manipulator for periods?
124 os << dp.last();
125 os << ']';
126 return os;
127 }
128
129 template <class charT, class traits>
130 inline
131 std::basic_ostream<charT, traits>&
132 operator<<(std::basic_ostream<charT, traits>& os, const date_duration& dd)
133 {
134 //os << dd.days();
135 os << dd.get_rep();
136 return os;
137 }
138
139 //! operator<< for gregorian::partial_date. Output: "Jan 1"
140 template <class charT, class traits>
141 inline
142 std::basic_ostream<charT, traits>&
143 operator<<(std::basic_ostream<charT, traits>& os, const partial_date& pd)
144 {
145 os << std::setw(2) << std::setfill('0') << pd.day() << ' '
146 << pd.month().as_short_string() ;
147 return os;
148 }
149
150 //! operator<< for gregorian::nth_kday_of_month. Output: "first Mon of Jun"
151 template <class charT, class traits>
152 inline
153 std::basic_ostream<charT, traits>&
154 operator<<(std::basic_ostream<charT, traits>& os,
155 const nth_kday_of_month& nkd)
156 {
157 os << nkd.nth_week_as_str() << ' '
158 << nkd.day_of_week() << " of "
159 << nkd.month().as_short_string() ;
160 return os;
161 }
162
163 //! operator<< for gregorian::first_kday_of_month. Output: "first Mon of Jun"
164 template <class charT, class traits>
165 inline
166 std::basic_ostream<charT, traits>&
167 operator<<(std::basic_ostream<charT, traits>& os,
168 const first_kday_of_month& fkd)
169 {
170 os << "first " << fkd.day_of_week() << " of "
171 << fkd.month().as_short_string() ;
172 return os;
173 }
174
175 //! operator<< for gregorian::last_kday_of_month. Output: "last Mon of Jun"
176 template <class charT, class traits>
177 inline
178 std::basic_ostream<charT, traits>&
179 operator<<(std::basic_ostream<charT, traits>& os,
180 const last_kday_of_month& lkd)
181 {
182 os << "last " << lkd.day_of_week() << " of "
183 << lkd.month().as_short_string() ;
184 return os;
185 }
186
187 //! operator<< for gregorian::first_kday_after. Output: "first Mon after"
188 template <class charT, class traits>
189 inline
190 std::basic_ostream<charT, traits>&
191 operator<<(std::basic_ostream<charT, traits>& os,
192 const first_kday_after& fka)
193 {
194 os << fka.day_of_week() << " after";
195 return os;
196 }
197
198 //! operator<< for gregorian::first_kday_before. Output: "first Mon before"
199 template <class charT, class traits>
200 inline
201 std::basic_ostream<charT, traits>&
202 operator<<(std::basic_ostream<charT, traits>& os,
203 const first_kday_before& fkb)
204 {
205 os << fkb.day_of_week() << " before";
206 return os;
207 }
208 #endif // USE_DATE_TIME_PRE_1_33_FACET_IO
209 /**************** Input Streaming ******************/
210
211 #if !defined(BOOST_NO_STD_ITERATOR_TRAITS)
212 //! operator>> for gregorian::date
213 template<class charT>
214 inline
215 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is, date& d)
216 {
217 std::istream_iterator<std::basic_string<charT>, charT> beg(is), eos;
218 d = from_stream(beg, eos);
219 return is;
220 }
221 #endif // BOOST_NO_STD_ITERATOR_TRAITS
222
223 //! operator>> for gregorian::date_duration
224 template<class charT>
225 inline
226 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,
227 date_duration& dd)
228 {
229 long v;
230 is >> v;
231 dd = date_duration(v);
232 return is;
233 }
234
235 //! operator>> for gregorian::date_period
236 template<class charT>
237 inline
238 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,
239 date_period& dp)
240 {
241 std::basic_string<charT> s;
242 is >> s;
243 dp = date_time::from_simple_string_type<date>(s);
244 return is;
245 }
246
247 //! generates a locale with the set of gregorian name-strings of type char*
248 BOOST_DATE_TIME_DECL std::locale generate_locale(std::locale& loc, char type);
249
250 //! Returns a pointer to a facet with a default set of names (English)
251 /* Necessary in the event an exception is thrown from op>> for
252 * weekday or month. See comments in those functions for more info */
253 BOOST_DATE_TIME_DECL boost::date_time::all_date_names_put<greg_facet_config, char>* create_facet_def(char type);
254
255 #ifndef BOOST_NO_STD_WSTRING
256 //! generates a locale with the set of gregorian name-strings of type wchar_t*
257 BOOST_DATE_TIME_DECL std::locale generate_locale(std::locale& loc, wchar_t type);
258 //! Returns a pointer to a facet with a default set of names (English)
259 /* Necessary in the event an exception is thrown from op>> for
260 * weekday or month. See comments in those functions for more info */
261 BOOST_DATE_TIME_DECL boost::date_time::all_date_names_put<greg_facet_config, wchar_t>* create_facet_def(wchar_t type);
262 #endif // BOOST_NO_STD_WSTRING
263
264 //! operator>> for gregorian::greg_month - throws exception if invalid month given
265 template<class charT>
266 inline
267 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_month& m)
268 {
269 typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
270
271 std::basic_string<charT> s;
272 is >> s;
273
274 if(!std::has_facet<facet_def>(is.getloc())) {
275 std::locale loc = is.getloc();
276 charT a = '\0';
277 is.imbue(generate_locale(loc, a));
278 }
279
280 short num = 0;
281
282 try{
283 const facet_def& f = std::use_facet<facet_def>(is.getloc());
284 num = date_time::find_match(f.get_short_month_names(),
285 f.get_long_month_names(),
286 (greg_month::max)(), s); // greg_month spans 1..12, so max returns the array size,
287 // which is needed by find_match
288 }
289 /* bad_cast will be thrown if the desired facet is not accessible
290 * so we can generate the facet. This has the drawback of using english
291 * names as a default. */
292 catch(std::bad_cast&){
293 charT a = '\0';
294
295 #if defined(BOOST_NO_CXX11_SMART_PTR)
296
297 std::auto_ptr< const facet_def > f(create_facet_def(a));
298
299 #else
300
301 std::unique_ptr< const facet_def > f(create_facet_def(a));
302
303 #endif
304
305 num = date_time::find_match(f->get_short_month_names(),
306 f->get_long_month_names(),
307 (greg_month::max)(), s); // greg_month spans 1..12, so max returns the array size,
308 // which is needed by find_match
309 }
310
311 ++num; // months numbered 1-12
312 m = greg_month(num);
313
314 return is;
315 }
316
317 //! operator>> for gregorian::greg_weekday - throws exception if invalid weekday given
318 template<class charT>
319 inline
320 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_weekday& wd)
321 {
322 typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
323
324 std::basic_string<charT> s;
325 is >> s;
326
327 if(!std::has_facet<facet_def>(is.getloc())) {
328 std::locale loc = is.getloc();
329 charT a = '\0';
330 is.imbue(generate_locale(loc, a));
331 }
332
333 short num = 0;
334 try{
335 const facet_def& f = std::use_facet<facet_def>(is.getloc());
336 num = date_time::find_match(f.get_short_weekday_names(),
337 f.get_long_weekday_names(),
338 (greg_weekday::max)() + 1, s); // greg_weekday spans 0..6, so increment is needed
339 // to form the array size which is needed by find_match
340 }
341 /* bad_cast will be thrown if the desired facet is not accessible
342 * so we can generate the facet. This has the drawback of using english
343 * names as a default. */
344 catch(std::bad_cast&){
345 charT a = '\0';
346
347 #if defined(BOOST_NO_CXX11_SMART_PTR)
348
349 std::auto_ptr< const facet_def > f(create_facet_def(a));
350
351 #else
352
353 std::unique_ptr< const facet_def > f(create_facet_def(a));
354
355 #endif
356
357 num = date_time::find_match(f->get_short_weekday_names(),
358 f->get_long_weekday_names(),
359 (greg_weekday::max)() + 1, s); // greg_weekday spans 0..6, so increment is needed
360 // to form the array size which is needed by find_match
361 }
362
363 wd = greg_weekday(num); // weekdays numbered 0-6
364 return is;
365 }
366
367 } } //namespace gregorian
368
369 #endif
370
371 #endif
372