1 | #ifndef DATE_TIME_GREGORIAN_IO_HPP__
|
---|
2 | #define DATE_TIME_GREGORIAN_IO_HPP__
|
---|
3 |
|
---|
4 | /* Copyright (c) 2004-2005 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 or http://www.boost.org/LICENSE-1.0)
|
---|
8 | * Author: Jeff Garland, Bart Garst
|
---|
9 | * $Date: 2005/06/28 13:11:45 $
|
---|
10 | */
|
---|
11 |
|
---|
12 | #include "boost/date_time/date_facet.hpp"
|
---|
13 | #include "boost/io/ios_state.hpp"
|
---|
14 | #include <iostream>
|
---|
15 | #include <locale>
|
---|
16 |
|
---|
17 | namespace boost {
|
---|
18 | namespace gregorian {
|
---|
19 |
|
---|
20 |
|
---|
21 | typedef boost::date_time::period_formatter<wchar_t> wperiod_formatter;
|
---|
22 | typedef boost::date_time::period_formatter<char> period_formatter;
|
---|
23 |
|
---|
24 | typedef boost::date_time::date_facet<date,wchar_t> wdate_facet;
|
---|
25 | typedef boost::date_time::date_facet<date,char> date_facet;
|
---|
26 |
|
---|
27 | typedef boost::date_time::period_parser<date,char> period_parser;
|
---|
28 | typedef boost::date_time::period_parser<date,wchar_t> wperiod_parser;
|
---|
29 |
|
---|
30 | typedef boost::date_time::special_values_formatter<char> special_values_formatter;
|
---|
31 | typedef boost::date_time::special_values_formatter<wchar_t> wspecial_values_formatter;
|
---|
32 |
|
---|
33 | typedef boost::date_time::special_values_parser<date,char> special_values_parser;
|
---|
34 | typedef boost::date_time::special_values_parser<date,wchar_t> wspecial_values_parser;
|
---|
35 |
|
---|
36 | typedef boost::date_time::date_input_facet<date,char> date_input_facet;
|
---|
37 | typedef boost::date_time::date_input_facet<date,wchar_t> wdate_input_facet;
|
---|
38 |
|
---|
39 | template <class CharT, class TraitsT>
|
---|
40 | inline std::basic_ostream<CharT, TraitsT>&
|
---|
41 | operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::date& d) {
|
---|
42 | boost::io::ios_flags_saver iflags(os);
|
---|
43 | typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
|
---|
44 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
45 | if (std::has_facet<custom_date_facet>(os.getloc()))
|
---|
46 | std::use_facet<custom_date_facet>(os.getloc()).put(oitr, os, os.fill(), d);
|
---|
47 | else {
|
---|
48 | //instantiate a custom facet for dealing with dates since the user
|
---|
49 | //has not put one in the stream so far. This is for efficiency
|
---|
50 | //since we would always need to reconstruct for every date
|
---|
51 | //if the locale did not already exist. Of course this will be overridden
|
---|
52 | //if the user imbues at some later point. With the default settings
|
---|
53 | //for the facet the resulting format will be the same as the
|
---|
54 | //std::time_facet settings.
|
---|
55 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
56 | custom_date_facet* f = new custom_date_facet();
|
---|
57 | std::locale l = std::locale(os.getloc(), f);
|
---|
58 | os.imbue(l);
|
---|
59 | f->put(oitr, os, os.fill(), d);
|
---|
60 | }
|
---|
61 | return os;
|
---|
62 | }
|
---|
63 |
|
---|
64 | //! input operator for date
|
---|
65 | template <class CharT, class Traits>
|
---|
66 | inline
|
---|
67 | std::basic_istream<CharT, Traits>&
|
---|
68 | operator>>(std::basic_istream<CharT, Traits>& is, date& d)
|
---|
69 | {
|
---|
70 | boost::io::ios_flags_saver iflags(is);
|
---|
71 | typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
|
---|
72 | if (strm_sentry) {
|
---|
73 | try {
|
---|
74 | typedef typename date_time::date_input_facet<date, CharT> date_input_facet;
|
---|
75 |
|
---|
76 | std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
|
---|
77 | if(std::has_facet<date_input_facet>(is.getloc())) {
|
---|
78 | std::use_facet<date_input_facet>(is.getloc()).get(sit, str_end, is, d);
|
---|
79 | }
|
---|
80 | else {
|
---|
81 | date_input_facet* f = new date_input_facet();
|
---|
82 | std::locale l = std::locale(is.getloc(), f);
|
---|
83 | is.imbue(l);
|
---|
84 | f->get(sit, str_end, is, d);
|
---|
85 | }
|
---|
86 | }
|
---|
87 | catch(...) {
|
---|
88 | // mask tells us what exceptions are turned on
|
---|
89 | std::ios_base::iostate exception_mask = is.exceptions();
|
---|
90 | // if the user wants exceptions on failbit, we'll rethrow our
|
---|
91 | // date_time exception & set the failbit
|
---|
92 | if(std::ios_base::failbit & exception_mask) {
|
---|
93 | try { is.setstate(std::ios_base::failbit); }
|
---|
94 | catch(std::ios_base::failure&) {} // ignore this one
|
---|
95 | throw; // rethrow original exception
|
---|
96 | }
|
---|
97 | else {
|
---|
98 | // if the user want's to fail quietly, we simply set the failbit
|
---|
99 | is.setstate(std::ios_base::failbit);
|
---|
100 | }
|
---|
101 |
|
---|
102 | }
|
---|
103 | }
|
---|
104 | return is;
|
---|
105 | }
|
---|
106 |
|
---|
107 | template <class CharT, class TraitsT>
|
---|
108 | inline std::basic_ostream<CharT, TraitsT>&
|
---|
109 | operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::date_duration& dd) {
|
---|
110 | boost::io::ios_flags_saver iflags(os);
|
---|
111 | typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
|
---|
112 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
113 | if (std::has_facet<custom_date_facet>(os.getloc()))
|
---|
114 | std::use_facet<custom_date_facet>(os.getloc()).put(oitr, os, os.fill(), dd);
|
---|
115 | else {
|
---|
116 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
117 | custom_date_facet* f = new custom_date_facet();
|
---|
118 | std::locale l = std::locale(os.getloc(), f);
|
---|
119 | os.imbue(l);
|
---|
120 | f->put(oitr, os, os.fill(), dd);
|
---|
121 |
|
---|
122 | }
|
---|
123 | return os;
|
---|
124 | }
|
---|
125 |
|
---|
126 | //! input operator for date_duration
|
---|
127 | template <class CharT, class Traits>
|
---|
128 | inline
|
---|
129 | std::basic_istream<CharT, Traits>&
|
---|
130 | operator>>(std::basic_istream<CharT, Traits>& is, date_duration& dd)
|
---|
131 | {
|
---|
132 | boost::io::ios_flags_saver iflags(is);
|
---|
133 | typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
|
---|
134 | if (strm_sentry) {
|
---|
135 | try {
|
---|
136 | typedef typename date_time::date_input_facet<date, CharT> date_input_facet;
|
---|
137 |
|
---|
138 | std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
|
---|
139 | if(std::has_facet<date_input_facet>(is.getloc())) {
|
---|
140 | std::use_facet<date_input_facet>(is.getloc()).get(sit, str_end, is, dd);
|
---|
141 | }
|
---|
142 | else {
|
---|
143 | date_input_facet* f = new date_input_facet();
|
---|
144 | std::locale l = std::locale(is.getloc(), f);
|
---|
145 | is.imbue(l);
|
---|
146 | f->get(sit, str_end, is, dd);
|
---|
147 | }
|
---|
148 | }
|
---|
149 | catch(...) {
|
---|
150 | std::ios_base::iostate exception_mask = is.exceptions();
|
---|
151 | if(std::ios_base::failbit & exception_mask) {
|
---|
152 | try { is.setstate(std::ios_base::failbit); }
|
---|
153 | catch(std::ios_base::failure&) {}
|
---|
154 | throw; // rethrow original exception
|
---|
155 | }
|
---|
156 | else {
|
---|
157 | is.setstate(std::ios_base::failbit);
|
---|
158 | }
|
---|
159 |
|
---|
160 | }
|
---|
161 | }
|
---|
162 | return is;
|
---|
163 | }
|
---|
164 |
|
---|
165 | template <class CharT, class TraitsT>
|
---|
166 | inline std::basic_ostream<CharT, TraitsT>&
|
---|
167 | operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::date_period& dp) {
|
---|
168 | boost::io::ios_flags_saver iflags(os);
|
---|
169 | typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
|
---|
170 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
171 | if (std::has_facet<custom_date_facet>(os.getloc()))
|
---|
172 | std::use_facet<custom_date_facet>(os.getloc()).put(oitr, os, os.fill(), dp);
|
---|
173 | else {
|
---|
174 | //instantiate a custom facet for dealing with date periods since the user
|
---|
175 | //has not put one in the stream so far. This is for efficiency
|
---|
176 | //since we would always need to reconstruct for every time period
|
---|
177 | //if the local did not already exist. Of course this will be overridden
|
---|
178 | //if the user imbues at some later point. With the default settings
|
---|
179 | //for the facet the resulting format will be the same as the
|
---|
180 | //std::time_facet settings.
|
---|
181 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
182 | custom_date_facet* f = new custom_date_facet();
|
---|
183 | std::locale l = std::locale(os.getloc(), f);
|
---|
184 | os.imbue(l);
|
---|
185 | f->put(oitr, os, os.fill(), dp);
|
---|
186 |
|
---|
187 | }
|
---|
188 | return os;
|
---|
189 | }
|
---|
190 |
|
---|
191 | //! input operator for date_period
|
---|
192 | template <class CharT, class Traits>
|
---|
193 | inline
|
---|
194 | std::basic_istream<CharT, Traits>&
|
---|
195 | operator>>(std::basic_istream<CharT, Traits>& is, date_period& dp)
|
---|
196 | {
|
---|
197 | boost::io::ios_flags_saver iflags(is);
|
---|
198 | typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
|
---|
199 | if (strm_sentry) {
|
---|
200 | try {
|
---|
201 | typedef typename date_time::date_input_facet<date, CharT> date_input_facet;
|
---|
202 |
|
---|
203 | std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
|
---|
204 | if(std::has_facet<date_input_facet>(is.getloc())) {
|
---|
205 | std::use_facet<date_input_facet>(is.getloc()).get(sit, str_end, is, dp);
|
---|
206 | }
|
---|
207 | else {
|
---|
208 | date_input_facet* f = new date_input_facet();
|
---|
209 | std::locale l = std::locale(is.getloc(), f);
|
---|
210 | is.imbue(l);
|
---|
211 | f->get(sit, str_end, is, dp);
|
---|
212 | }
|
---|
213 | }
|
---|
214 | catch(...) {
|
---|
215 | std::ios_base::iostate exception_mask = is.exceptions();
|
---|
216 | if(std::ios_base::failbit & exception_mask) {
|
---|
217 | try { is.setstate(std::ios_base::failbit); }
|
---|
218 | catch(std::ios_base::failure&) {}
|
---|
219 | throw; // rethrow original exception
|
---|
220 | }
|
---|
221 | else {
|
---|
222 | is.setstate(std::ios_base::failbit);
|
---|
223 | }
|
---|
224 |
|
---|
225 | }
|
---|
226 | }
|
---|
227 | return is;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /********** small gregorian types **********/
|
---|
231 |
|
---|
232 | template <class CharT, class TraitsT>
|
---|
233 | inline std::basic_ostream<CharT, TraitsT>&
|
---|
234 | operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::greg_month& gm) {
|
---|
235 | boost::io::ios_flags_saver iflags(os);
|
---|
236 | typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
|
---|
237 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
238 | if (std::has_facet<custom_date_facet>(os.getloc()))
|
---|
239 | std::use_facet<custom_date_facet>(os.getloc()).put(oitr, os, os.fill(), gm);
|
---|
240 | else {
|
---|
241 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
242 | custom_date_facet* f = new custom_date_facet();//-> 10/1074199752/32 because year & day not initialized in put(...)
|
---|
243 | //custom_date_facet* f = new custom_date_facet("%B");
|
---|
244 | std::locale l = std::locale(os.getloc(), f);
|
---|
245 | os.imbue(l);
|
---|
246 | f->put(oitr, os, os.fill(), gm);
|
---|
247 | }
|
---|
248 | return os;
|
---|
249 | }
|
---|
250 |
|
---|
251 | //! input operator for greg_month
|
---|
252 | template <class CharT, class Traits>
|
---|
253 | inline
|
---|
254 | std::basic_istream<CharT, Traits>&
|
---|
255 | operator>>(std::basic_istream<CharT, Traits>& is, greg_month& m)
|
---|
256 | {
|
---|
257 | boost::io::ios_flags_saver iflags(is);
|
---|
258 | typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
|
---|
259 | if (strm_sentry) {
|
---|
260 | try {
|
---|
261 | typedef typename date_time::date_input_facet<date, CharT> date_input_facet;
|
---|
262 |
|
---|
263 | std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
|
---|
264 | if(std::has_facet<date_input_facet>(is.getloc())) {
|
---|
265 | std::use_facet<date_input_facet>(is.getloc()).get(sit, str_end, is, m);
|
---|
266 | }
|
---|
267 | else {
|
---|
268 | date_input_facet* f = new date_input_facet();
|
---|
269 | std::locale l = std::locale(is.getloc(), f);
|
---|
270 | is.imbue(l);
|
---|
271 | f->get(sit, str_end, is, m);
|
---|
272 | }
|
---|
273 | }
|
---|
274 | catch(...) {
|
---|
275 | std::ios_base::iostate exception_mask = is.exceptions();
|
---|
276 | if(std::ios_base::failbit & exception_mask) {
|
---|
277 | try { is.setstate(std::ios_base::failbit); }
|
---|
278 | catch(std::ios_base::failure&) {}
|
---|
279 | throw; // rethrow original exception
|
---|
280 | }
|
---|
281 | else {
|
---|
282 | is.setstate(std::ios_base::failbit);
|
---|
283 | }
|
---|
284 |
|
---|
285 | }
|
---|
286 | }
|
---|
287 | return is;
|
---|
288 | }
|
---|
289 |
|
---|
290 |
|
---|
291 | template <class CharT, class TraitsT>
|
---|
292 | inline std::basic_ostream<CharT, TraitsT>&
|
---|
293 | operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::greg_weekday& gw) {
|
---|
294 | boost::io::ios_flags_saver iflags(os);
|
---|
295 | typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
|
---|
296 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
297 | if (std::has_facet<custom_date_facet>(os.getloc()))
|
---|
298 | std::use_facet<custom_date_facet>(os.getloc()).put(oitr, os, os.fill(), gw);
|
---|
299 | else {
|
---|
300 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
301 | custom_date_facet* f = new custom_date_facet();
|
---|
302 | std::locale l = std::locale(os.getloc(), f);
|
---|
303 | os.imbue(l);
|
---|
304 | f->put(oitr, os, os.fill(), gw);
|
---|
305 | }
|
---|
306 | return os;
|
---|
307 | }
|
---|
308 |
|
---|
309 | //! input operator for greg_weekday
|
---|
310 | template <class CharT, class Traits>
|
---|
311 | inline
|
---|
312 | std::basic_istream<CharT, Traits>&
|
---|
313 | operator>>(std::basic_istream<CharT, Traits>& is, greg_weekday& wd)
|
---|
314 | {
|
---|
315 | boost::io::ios_flags_saver iflags(is);
|
---|
316 | typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
|
---|
317 | if (strm_sentry) {
|
---|
318 | try {
|
---|
319 | typedef typename date_time::date_input_facet<date, CharT> date_input_facet;
|
---|
320 |
|
---|
321 | std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
|
---|
322 | if(std::has_facet<date_input_facet>(is.getloc())) {
|
---|
323 | std::use_facet<date_input_facet>(is.getloc()).get(sit, str_end, is, wd);
|
---|
324 | }
|
---|
325 | else {
|
---|
326 | date_input_facet* f = new date_input_facet();
|
---|
327 | std::locale l = std::locale(is.getloc(), f);
|
---|
328 | is.imbue(l);
|
---|
329 | f->get(sit, str_end, is, wd);
|
---|
330 | }
|
---|
331 | }
|
---|
332 | catch(...) {
|
---|
333 | std::ios_base::iostate exception_mask = is.exceptions();
|
---|
334 | if(std::ios_base::failbit & exception_mask) {
|
---|
335 | try { is.setstate(std::ios_base::failbit); }
|
---|
336 | catch(std::ios_base::failure&) {}
|
---|
337 | throw; // rethrow original exception
|
---|
338 | }
|
---|
339 | else {
|
---|
340 | is.setstate(std::ios_base::failbit);
|
---|
341 | }
|
---|
342 |
|
---|
343 | }
|
---|
344 | }
|
---|
345 | return is;
|
---|
346 | }
|
---|
347 |
|
---|
348 | //NOTE: output operator for greg_day was not necessary
|
---|
349 |
|
---|
350 | //! input operator for greg_day
|
---|
351 | template <class CharT, class Traits>
|
---|
352 | inline
|
---|
353 | std::basic_istream<CharT, Traits>&
|
---|
354 | operator>>(std::basic_istream<CharT, Traits>& is, greg_day& gd)
|
---|
355 | {
|
---|
356 | boost::io::ios_flags_saver iflags(is);
|
---|
357 | typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
|
---|
358 | if (strm_sentry) {
|
---|
359 | try {
|
---|
360 | typedef typename date_time::date_input_facet<date, CharT> date_input_facet;
|
---|
361 |
|
---|
362 | std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
|
---|
363 | if(std::has_facet<date_input_facet>(is.getloc())) {
|
---|
364 | std::use_facet<date_input_facet>(is.getloc()).get(sit, str_end, is, gd);
|
---|
365 | }
|
---|
366 | else {
|
---|
367 | date_input_facet* f = new date_input_facet();
|
---|
368 | std::locale l = std::locale(is.getloc(), f);
|
---|
369 | is.imbue(l);
|
---|
370 | f->get(sit, str_end, is, gd);
|
---|
371 | }
|
---|
372 | }
|
---|
373 | catch(...) {
|
---|
374 | std::ios_base::iostate exception_mask = is.exceptions();
|
---|
375 | if(std::ios_base::failbit & exception_mask) {
|
---|
376 | try { is.setstate(std::ios_base::failbit); }
|
---|
377 | catch(std::ios_base::failure&) {}
|
---|
378 | throw; // rethrow original exception
|
---|
379 | }
|
---|
380 | else {
|
---|
381 | is.setstate(std::ios_base::failbit);
|
---|
382 | }
|
---|
383 |
|
---|
384 | }
|
---|
385 | }
|
---|
386 | return is;
|
---|
387 | }
|
---|
388 |
|
---|
389 | //NOTE: output operator for greg_year was not necessary
|
---|
390 |
|
---|
391 | //! input operator for greg_year
|
---|
392 | template <class CharT, class Traits>
|
---|
393 | inline
|
---|
394 | std::basic_istream<CharT, Traits>&
|
---|
395 | operator>>(std::basic_istream<CharT, Traits>& is, greg_year& gy)
|
---|
396 | {
|
---|
397 | boost::io::ios_flags_saver iflags(is);
|
---|
398 | typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
|
---|
399 | if (strm_sentry) {
|
---|
400 | try {
|
---|
401 | typedef typename date_time::date_input_facet<date, CharT> date_input_facet;
|
---|
402 |
|
---|
403 | std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
|
---|
404 | if(std::has_facet<date_input_facet>(is.getloc())) {
|
---|
405 | std::use_facet<date_input_facet>(is.getloc()).get(sit, str_end, is, gy);
|
---|
406 | }
|
---|
407 | else {
|
---|
408 | date_input_facet* f = new date_input_facet();
|
---|
409 | std::locale l = std::locale(is.getloc(), f);
|
---|
410 | is.imbue(l);
|
---|
411 | f->get(sit, str_end, is, gy);
|
---|
412 | }
|
---|
413 | }
|
---|
414 | catch(...) {
|
---|
415 | std::ios_base::iostate exception_mask = is.exceptions();
|
---|
416 | if(std::ios_base::failbit & exception_mask) {
|
---|
417 | try { is.setstate(std::ios_base::failbit); }
|
---|
418 | catch(std::ios_base::failure&) {}
|
---|
419 | throw; // rethrow original exception
|
---|
420 | }
|
---|
421 | else {
|
---|
422 | is.setstate(std::ios_base::failbit);
|
---|
423 | }
|
---|
424 |
|
---|
425 | }
|
---|
426 | }
|
---|
427 | return is;
|
---|
428 | }
|
---|
429 |
|
---|
430 | /********** date generator types **********/
|
---|
431 |
|
---|
432 | template <class CharT, class TraitsT>
|
---|
433 | inline std::basic_ostream<CharT, TraitsT>&
|
---|
434 | operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::partial_date& pd) {
|
---|
435 | boost::io::ios_flags_saver iflags(os);
|
---|
436 | typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
|
---|
437 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
438 | if (std::has_facet<custom_date_facet>(os.getloc()))
|
---|
439 | std::use_facet<custom_date_facet>(os.getloc()).put(oitr, os, os.fill(), pd);
|
---|
440 | else {
|
---|
441 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
442 | custom_date_facet* f = new custom_date_facet();
|
---|
443 | std::locale l = std::locale(os.getloc(), f);
|
---|
444 | os.imbue(l);
|
---|
445 | f->put(oitr, os, os.fill(), pd);
|
---|
446 | }
|
---|
447 | return os;
|
---|
448 | }
|
---|
449 |
|
---|
450 | //! input operator for partial_date
|
---|
451 | template <class CharT, class Traits>
|
---|
452 | inline
|
---|
453 | std::basic_istream<CharT, Traits>&
|
---|
454 | operator>>(std::basic_istream<CharT, Traits>& is, partial_date& pd)
|
---|
455 | {
|
---|
456 | boost::io::ios_flags_saver iflags(is);
|
---|
457 | typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
|
---|
458 | if (strm_sentry) {
|
---|
459 | try {
|
---|
460 | typedef typename date_time::date_input_facet<date, CharT> date_input_facet;
|
---|
461 |
|
---|
462 | std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
|
---|
463 | if(std::has_facet<date_input_facet>(is.getloc())) {
|
---|
464 | std::use_facet<date_input_facet>(is.getloc()).get(sit, str_end, is, pd);
|
---|
465 | }
|
---|
466 | else {
|
---|
467 | date_input_facet* f = new date_input_facet();
|
---|
468 | std::locale l = std::locale(is.getloc(), f);
|
---|
469 | is.imbue(l);
|
---|
470 | f->get(sit, str_end, is, pd);
|
---|
471 | }
|
---|
472 | }
|
---|
473 | catch(...) {
|
---|
474 | std::ios_base::iostate exception_mask = is.exceptions();
|
---|
475 | if(std::ios_base::failbit & exception_mask) {
|
---|
476 | try { is.setstate(std::ios_base::failbit); }
|
---|
477 | catch(std::ios_base::failure&) {}
|
---|
478 | throw; // rethrow original exception
|
---|
479 | }
|
---|
480 | else {
|
---|
481 | is.setstate(std::ios_base::failbit);
|
---|
482 | }
|
---|
483 |
|
---|
484 | }
|
---|
485 | }
|
---|
486 | return is;
|
---|
487 | }
|
---|
488 |
|
---|
489 | template <class CharT, class TraitsT>
|
---|
490 | inline std::basic_ostream<CharT, TraitsT>&
|
---|
491 | operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::nth_day_of_the_week_in_month& nkd) {
|
---|
492 | boost::io::ios_flags_saver iflags(os);
|
---|
493 | typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
|
---|
494 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
495 | if (std::has_facet<custom_date_facet>(os.getloc()))
|
---|
496 | std::use_facet<custom_date_facet>(os.getloc()).put(oitr, os, os.fill(), nkd);
|
---|
497 | else {
|
---|
498 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
499 | custom_date_facet* f = new custom_date_facet();
|
---|
500 | std::locale l = std::locale(os.getloc(), f);
|
---|
501 | os.imbue(l);
|
---|
502 | f->put(oitr, os, os.fill(), nkd);
|
---|
503 | }
|
---|
504 | return os;
|
---|
505 | }
|
---|
506 |
|
---|
507 | //! input operator for nth_day_of_the_week_in_month
|
---|
508 | template <class CharT, class Traits>
|
---|
509 | inline
|
---|
510 | std::basic_istream<CharT, Traits>&
|
---|
511 | operator>>(std::basic_istream<CharT, Traits>& is,
|
---|
512 | nth_day_of_the_week_in_month& nday)
|
---|
513 | {
|
---|
514 | boost::io::ios_flags_saver iflags(is);
|
---|
515 | typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
|
---|
516 | if (strm_sentry) {
|
---|
517 | try {
|
---|
518 | typedef typename date_time::date_input_facet<date, CharT> date_input_facet;
|
---|
519 |
|
---|
520 | std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
|
---|
521 | if(std::has_facet<date_input_facet>(is.getloc())) {
|
---|
522 | std::use_facet<date_input_facet>(is.getloc()).get(sit, str_end, is, nday);
|
---|
523 | }
|
---|
524 | else {
|
---|
525 | date_input_facet* f = new date_input_facet();
|
---|
526 | std::locale l = std::locale(is.getloc(), f);
|
---|
527 | is.imbue(l);
|
---|
528 | f->get(sit, str_end, is, nday);
|
---|
529 | }
|
---|
530 | }
|
---|
531 | catch(...) {
|
---|
532 | std::ios_base::iostate exception_mask = is.exceptions();
|
---|
533 | if(std::ios_base::failbit & exception_mask) {
|
---|
534 | try { is.setstate(std::ios_base::failbit); }
|
---|
535 | catch(std::ios_base::failure&) {}
|
---|
536 | throw; // rethrow original exception
|
---|
537 | }
|
---|
538 | else {
|
---|
539 | is.setstate(std::ios_base::failbit);
|
---|
540 | }
|
---|
541 |
|
---|
542 | }
|
---|
543 | }
|
---|
544 | return is;
|
---|
545 | }
|
---|
546 |
|
---|
547 |
|
---|
548 | template <class CharT, class TraitsT>
|
---|
549 | inline std::basic_ostream<CharT, TraitsT>&
|
---|
550 | operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::first_day_of_the_week_in_month& fkd) {
|
---|
551 | boost::io::ios_flags_saver iflags(os);
|
---|
552 | typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
|
---|
553 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
554 | if (std::has_facet<custom_date_facet>(os.getloc()))
|
---|
555 | std::use_facet<custom_date_facet>(os.getloc()).put(oitr, os, os.fill(), fkd);
|
---|
556 | else {
|
---|
557 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
558 | custom_date_facet* f = new custom_date_facet();
|
---|
559 | std::locale l = std::locale(os.getloc(), f);
|
---|
560 | os.imbue(l);
|
---|
561 | f->put(oitr, os, os.fill(), fkd);
|
---|
562 | }
|
---|
563 | return os;
|
---|
564 | }
|
---|
565 |
|
---|
566 | //! input operator for first_day_of_the_week_in_month
|
---|
567 | template <class CharT, class Traits>
|
---|
568 | inline
|
---|
569 | std::basic_istream<CharT, Traits>&
|
---|
570 | operator>>(std::basic_istream<CharT, Traits>& is,
|
---|
571 | first_day_of_the_week_in_month& fkd)
|
---|
572 | {
|
---|
573 | boost::io::ios_flags_saver iflags(is);
|
---|
574 | typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
|
---|
575 | if (strm_sentry) {
|
---|
576 | try {
|
---|
577 | typedef typename date_time::date_input_facet<date, CharT> date_input_facet;
|
---|
578 |
|
---|
579 | std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
|
---|
580 | if(std::has_facet<date_input_facet>(is.getloc())) {
|
---|
581 | std::use_facet<date_input_facet>(is.getloc()).get(sit, str_end, is, fkd);
|
---|
582 | }
|
---|
583 | else {
|
---|
584 | date_input_facet* f = new date_input_facet();
|
---|
585 | std::locale l = std::locale(is.getloc(), f);
|
---|
586 | is.imbue(l);
|
---|
587 | f->get(sit, str_end, is, fkd);
|
---|
588 | }
|
---|
589 | }
|
---|
590 | catch(...) {
|
---|
591 | std::ios_base::iostate exception_mask = is.exceptions();
|
---|
592 | if(std::ios_base::failbit & exception_mask) {
|
---|
593 | try { is.setstate(std::ios_base::failbit); }
|
---|
594 | catch(std::ios_base::failure&) {}
|
---|
595 | throw; // rethrow original exception
|
---|
596 | }
|
---|
597 | else {
|
---|
598 | is.setstate(std::ios_base::failbit);
|
---|
599 | }
|
---|
600 |
|
---|
601 | }
|
---|
602 | }
|
---|
603 | return is;
|
---|
604 | }
|
---|
605 |
|
---|
606 |
|
---|
607 | template <class CharT, class TraitsT>
|
---|
608 | inline std::basic_ostream<CharT, TraitsT>&
|
---|
609 | operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::last_day_of_the_week_in_month& lkd) {
|
---|
610 | boost::io::ios_flags_saver iflags(os);
|
---|
611 | typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
|
---|
612 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
613 | if (std::has_facet<custom_date_facet>(os.getloc()))
|
---|
614 | std::use_facet<custom_date_facet>(os.getloc()).put(oitr, os, os.fill(), lkd);
|
---|
615 | else {
|
---|
616 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
617 | custom_date_facet* f = new custom_date_facet();
|
---|
618 | std::locale l = std::locale(os.getloc(), f);
|
---|
619 | os.imbue(l);
|
---|
620 | f->put(oitr, os, os.fill(), lkd);
|
---|
621 | }
|
---|
622 | return os;
|
---|
623 | }
|
---|
624 |
|
---|
625 | //! input operator for last_day_of_the_week_in_month
|
---|
626 | template <class CharT, class Traits>
|
---|
627 | inline
|
---|
628 | std::basic_istream<CharT, Traits>&
|
---|
629 | operator>>(std::basic_istream<CharT, Traits>& is,
|
---|
630 | last_day_of_the_week_in_month& lkd)
|
---|
631 | {
|
---|
632 | boost::io::ios_flags_saver iflags(is);
|
---|
633 | typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
|
---|
634 | if (strm_sentry) {
|
---|
635 | try {
|
---|
636 | typedef typename date_time::date_input_facet<date, CharT> date_input_facet;
|
---|
637 |
|
---|
638 | std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
|
---|
639 | if(std::has_facet<date_input_facet>(is.getloc())) {
|
---|
640 | std::use_facet<date_input_facet>(is.getloc()).get(sit, str_end, is, lkd);
|
---|
641 | }
|
---|
642 | else {
|
---|
643 | date_input_facet* f = new date_input_facet();
|
---|
644 | std::locale l = std::locale(is.getloc(), f);
|
---|
645 | is.imbue(l);
|
---|
646 | f->get(sit, str_end, is, lkd);
|
---|
647 | }
|
---|
648 | }
|
---|
649 | catch(...) {
|
---|
650 | std::ios_base::iostate exception_mask = is.exceptions();
|
---|
651 | if(std::ios_base::failbit & exception_mask) {
|
---|
652 | try { is.setstate(std::ios_base::failbit); }
|
---|
653 | catch(std::ios_base::failure&) {}
|
---|
654 | throw; // rethrow original exception
|
---|
655 | }
|
---|
656 | else {
|
---|
657 | is.setstate(std::ios_base::failbit);
|
---|
658 | }
|
---|
659 |
|
---|
660 | }
|
---|
661 | }
|
---|
662 | return is;
|
---|
663 | }
|
---|
664 |
|
---|
665 |
|
---|
666 | template <class CharT, class TraitsT>
|
---|
667 | inline std::basic_ostream<CharT, TraitsT>&
|
---|
668 | operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::first_day_of_the_week_after& fda) {
|
---|
669 | boost::io::ios_flags_saver iflags(os);
|
---|
670 | typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
|
---|
671 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
672 | if (std::has_facet<custom_date_facet>(os.getloc())) {
|
---|
673 | std::use_facet<custom_date_facet>(os.getloc()).put(oitr, os, os.fill(), fda);
|
---|
674 | }
|
---|
675 | else {
|
---|
676 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
677 | custom_date_facet* f = new custom_date_facet();
|
---|
678 | std::locale l = std::locale(os.getloc(), f);
|
---|
679 | os.imbue(l);
|
---|
680 | f->put(oitr, os, os.fill(), fda);
|
---|
681 | }
|
---|
682 | return os;
|
---|
683 | }
|
---|
684 |
|
---|
685 | //! input operator for first_day_of_the_week_after
|
---|
686 | template <class CharT, class Traits>
|
---|
687 | inline
|
---|
688 | std::basic_istream<CharT, Traits>&
|
---|
689 | operator>>(std::basic_istream<CharT, Traits>& is,
|
---|
690 | first_day_of_the_week_after& fka)
|
---|
691 | {
|
---|
692 | boost::io::ios_flags_saver iflags(is);
|
---|
693 | typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
|
---|
694 | if (strm_sentry) {
|
---|
695 | try {
|
---|
696 | typedef typename date_time::date_input_facet<date, CharT> date_input_facet;
|
---|
697 |
|
---|
698 | std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
|
---|
699 | if(std::has_facet<date_input_facet>(is.getloc())) {
|
---|
700 | std::use_facet<date_input_facet>(is.getloc()).get(sit, str_end, is, fka);
|
---|
701 | }
|
---|
702 | else {
|
---|
703 | date_input_facet* f = new date_input_facet();
|
---|
704 | std::locale l = std::locale(is.getloc(), f);
|
---|
705 | is.imbue(l);
|
---|
706 | f->get(sit, str_end, is, fka);
|
---|
707 | }
|
---|
708 | }
|
---|
709 | catch(...) {
|
---|
710 | std::ios_base::iostate exception_mask = is.exceptions();
|
---|
711 | if(std::ios_base::failbit & exception_mask) {
|
---|
712 | try { is.setstate(std::ios_base::failbit); }
|
---|
713 | catch(std::ios_base::failure&) {}
|
---|
714 | throw; // rethrow original exception
|
---|
715 | }
|
---|
716 | else {
|
---|
717 | is.setstate(std::ios_base::failbit);
|
---|
718 | }
|
---|
719 |
|
---|
720 | }
|
---|
721 | }
|
---|
722 | return is;
|
---|
723 | }
|
---|
724 |
|
---|
725 |
|
---|
726 | template <class CharT, class TraitsT>
|
---|
727 | inline std::basic_ostream<CharT, TraitsT>&
|
---|
728 | operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::first_day_of_the_week_before& fdb) {
|
---|
729 | boost::io::ios_flags_saver iflags(os);
|
---|
730 | typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
|
---|
731 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
732 | if (std::has_facet<custom_date_facet>(os.getloc())) {
|
---|
733 | std::use_facet<custom_date_facet>(os.getloc()).put(oitr, os, os.fill(), fdb);
|
---|
734 | }
|
---|
735 | else {
|
---|
736 | std::ostreambuf_iterator<CharT> oitr(os);
|
---|
737 | custom_date_facet* f = new custom_date_facet();
|
---|
738 | std::locale l = std::locale(os.getloc(), f);
|
---|
739 | os.imbue(l);
|
---|
740 | f->put(oitr, os, os.fill(), fdb);
|
---|
741 | }
|
---|
742 | return os;
|
---|
743 | }
|
---|
744 |
|
---|
745 | //! input operator for first_day_of_the_week_before
|
---|
746 | template <class CharT, class Traits>
|
---|
747 | inline
|
---|
748 | std::basic_istream<CharT, Traits>&
|
---|
749 | operator>>(std::basic_istream<CharT, Traits>& is,
|
---|
750 | first_day_of_the_week_before& fkb)
|
---|
751 | {
|
---|
752 | boost::io::ios_flags_saver iflags(is);
|
---|
753 | typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
|
---|
754 | if (strm_sentry) {
|
---|
755 | try {
|
---|
756 | typedef typename date_time::date_input_facet<date, CharT> date_input_facet;
|
---|
757 |
|
---|
758 | std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
|
---|
759 | if(std::has_facet<date_input_facet>(is.getloc())) {
|
---|
760 | std::use_facet<date_input_facet>(is.getloc()).get(sit, str_end, is, fkb);
|
---|
761 | }
|
---|
762 | else {
|
---|
763 | date_input_facet* f = new date_input_facet();
|
---|
764 | std::locale l = std::locale(is.getloc(), f);
|
---|
765 | is.imbue(l);
|
---|
766 | f->get(sit, str_end, is, fkb);
|
---|
767 | }
|
---|
768 | }
|
---|
769 | catch(...) {
|
---|
770 | std::ios_base::iostate exception_mask = is.exceptions();
|
---|
771 | if(std::ios_base::failbit & exception_mask) {
|
---|
772 | try { is.setstate(std::ios_base::failbit); }
|
---|
773 | catch(std::ios_base::failure&) {}
|
---|
774 | throw; // rethrow original exception
|
---|
775 | }
|
---|
776 | else {
|
---|
777 | is.setstate(std::ios_base::failbit);
|
---|
778 | }
|
---|
779 |
|
---|
780 | }
|
---|
781 | }
|
---|
782 | return is;
|
---|
783 | }
|
---|
784 |
|
---|
785 |
|
---|
786 | } } // namespaces
|
---|
787 |
|
---|
788 | #endif // DATE_TIME_GREGORIAN_IO_HPP__
|
---|