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