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