1 | #ifndef _DATE_TIME_DATE_FACET__HPP___ |
---|
2 | #define _DATE_TIME_DATE_FACET__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.txt or http://www.boost.org/LICENSE_1_0.txt) |
---|
8 | * Author: Martin Andrian, Jeff Garland, Bart Garst |
---|
9 | * $Date: 2009-06-04 07:40:18 -0400 (Thu, 04 Jun 2009) $ |
---|
10 | */ |
---|
11 | |
---|
12 | #include <locale> |
---|
13 | #include <string> |
---|
14 | #include <vector> |
---|
15 | #include <iterator> // ostreambuf_iterator |
---|
16 | #include <boost/throw_exception.hpp> |
---|
17 | #include <boost/algorithm/string/replace.hpp> |
---|
18 | #include <boost/date_time/compiler_config.hpp> |
---|
19 | #include <boost/date_time/period.hpp> |
---|
20 | #include <boost/date_time/special_defs.hpp> |
---|
21 | #include <boost/date_time/special_values_formatter.hpp> |
---|
22 | #include <boost/date_time/period_formatter.hpp> |
---|
23 | #include <boost/date_time/period_parser.hpp> |
---|
24 | #include <boost/date_time/date_generator_formatter.hpp> |
---|
25 | #include <boost/date_time/date_generator_parser.hpp> |
---|
26 | #include <boost/date_time/format_date_parser.hpp> |
---|
27 | |
---|
28 | namespace boost { namespace date_time { |
---|
29 | |
---|
30 | |
---|
31 | /*! Class that provides format based I/O facet for date types. |
---|
32 | * |
---|
33 | * This class allows the formatting of dates by using format string. |
---|
34 | * Format strings are: |
---|
35 | * |
---|
36 | * - %A => long_weekday_format - Full name Ex: Tuesday |
---|
37 | * - %a => short_weekday_format - Three letter abbreviation Ex: Tue |
---|
38 | * - %B => long_month_format - Full name Ex: October |
---|
39 | * - %b => short_month_format - Three letter abbreviation Ex: Oct |
---|
40 | * - %x => standard_format_specifier - defined by the locale |
---|
41 | * - %Y-%b-%d => default_date_format - YYYY-Mon-dd |
---|
42 | * |
---|
43 | * Default month format == %b |
---|
44 | * Default weekday format == %a |
---|
45 | */ |
---|
46 | template <class date_type, |
---|
47 | class CharT, |
---|
48 | class OutItrT = std::ostreambuf_iterator<CharT, std::char_traits<CharT> > > |
---|
49 | class date_facet : public std::locale::facet { |
---|
50 | public: |
---|
51 | typedef typename date_type::duration_type duration_type; |
---|
52 | // greg_weekday is gregorian_calendar::day_of_week_type |
---|
53 | typedef typename date_type::day_of_week_type day_of_week_type; |
---|
54 | typedef typename date_type::day_type day_type; |
---|
55 | typedef typename date_type::month_type month_type; |
---|
56 | typedef boost::date_time::period<date_type,duration_type> period_type; |
---|
57 | typedef std::basic_string<CharT> string_type; |
---|
58 | typedef CharT char_type; |
---|
59 | typedef boost::date_time::period_formatter<CharT> period_formatter_type; |
---|
60 | typedef boost::date_time::special_values_formatter<CharT> special_values_formatter_type; |
---|
61 | typedef std::vector<std::basic_string<CharT> > input_collection_type; |
---|
62 | // used for the output of the date_generators |
---|
63 | typedef date_generator_formatter<date_type, CharT> date_gen_formatter_type; |
---|
64 | typedef partial_date<date_type> partial_date_type; |
---|
65 | typedef nth_kday_of_month<date_type> nth_kday_type; |
---|
66 | typedef first_kday_of_month<date_type> first_kday_type; |
---|
67 | typedef last_kday_of_month<date_type> last_kday_type; |
---|
68 | typedef first_kday_after<date_type> kday_after_type; |
---|
69 | typedef first_kday_before<date_type> kday_before_type; |
---|
70 | static const char_type long_weekday_format[3]; |
---|
71 | static const char_type short_weekday_format[3]; |
---|
72 | static const char_type long_month_format[3]; |
---|
73 | static const char_type short_month_format[3]; |
---|
74 | static const char_type default_period_separator[4]; |
---|
75 | static const char_type standard_format_specifier[3]; |
---|
76 | static const char_type iso_format_specifier[7]; |
---|
77 | static const char_type iso_format_extended_specifier[9]; |
---|
78 | static const char_type default_date_format[9]; // YYYY-Mon-DD |
---|
79 | static std::locale::id id; |
---|
80 | |
---|
81 | #if defined (__SUNPRO_CC) && defined (_RWSTD_VER) |
---|
82 | std::locale::id& __get_id (void) const { return id; } |
---|
83 | #endif |
---|
84 | |
---|
85 | explicit date_facet(::size_t a_ref = 0) |
---|
86 | : std::locale::facet(a_ref), |
---|
87 | //m_format(standard_format_specifier) |
---|
88 | m_format(default_date_format), |
---|
89 | m_month_format(short_month_format), |
---|
90 | m_weekday_format(short_weekday_format) |
---|
91 | {} |
---|
92 | |
---|
93 | explicit date_facet(const char_type* format_str, |
---|
94 | const input_collection_type& short_names, |
---|
95 | ::size_t ref_count = 0) |
---|
96 | : std::locale::facet(ref_count), |
---|
97 | m_format(format_str), |
---|
98 | m_month_format(short_month_format), |
---|
99 | m_weekday_format(short_weekday_format), |
---|
100 | m_month_short_names(short_names) |
---|
101 | {} |
---|
102 | |
---|
103 | |
---|
104 | explicit date_facet(const char_type* format_str, |
---|
105 | period_formatter_type per_formatter = period_formatter_type(), |
---|
106 | special_values_formatter_type sv_formatter = special_values_formatter_type(), |
---|
107 | date_gen_formatter_type dg_formatter = date_gen_formatter_type(), |
---|
108 | ::size_t ref_count = 0) |
---|
109 | : std::locale::facet(ref_count), |
---|
110 | m_format(format_str), |
---|
111 | m_month_format(short_month_format), |
---|
112 | m_weekday_format(short_weekday_format), |
---|
113 | m_period_formatter(per_formatter), |
---|
114 | m_date_gen_formatter(dg_formatter), |
---|
115 | m_special_values_formatter(sv_formatter) |
---|
116 | {} |
---|
117 | void format(const char_type* const format_str) { |
---|
118 | m_format = format_str; |
---|
119 | } |
---|
120 | virtual void set_iso_format() |
---|
121 | { |
---|
122 | m_format = iso_format_specifier; |
---|
123 | } |
---|
124 | virtual void set_iso_extended_format() |
---|
125 | { |
---|
126 | m_format = iso_format_extended_specifier; |
---|
127 | } |
---|
128 | void month_format(const char_type* const format_str) { |
---|
129 | m_month_format = format_str; |
---|
130 | } |
---|
131 | void weekday_format(const char_type* const format_str) { |
---|
132 | m_weekday_format = format_str; |
---|
133 | } |
---|
134 | |
---|
135 | void period_formatter(period_formatter_type per_formatter) { |
---|
136 | m_period_formatter= per_formatter; |
---|
137 | } |
---|
138 | void special_values_formatter(const special_values_formatter_type& svf) |
---|
139 | { |
---|
140 | m_special_values_formatter = svf; |
---|
141 | } |
---|
142 | void short_weekday_names(const input_collection_type& short_names) |
---|
143 | { |
---|
144 | m_weekday_short_names = short_names; |
---|
145 | } |
---|
146 | void long_weekday_names(const input_collection_type& long_names) |
---|
147 | { |
---|
148 | m_weekday_long_names = long_names; |
---|
149 | } |
---|
150 | |
---|
151 | void short_month_names(const input_collection_type& short_names) |
---|
152 | { |
---|
153 | m_month_short_names = short_names; |
---|
154 | } |
---|
155 | |
---|
156 | void long_month_names(const input_collection_type& long_names) |
---|
157 | { |
---|
158 | m_month_long_names = long_names; |
---|
159 | } |
---|
160 | |
---|
161 | void date_gen_phrase_strings(const input_collection_type& new_strings, |
---|
162 | typename date_gen_formatter_type::phrase_elements beg_pos=date_gen_formatter_type::first) |
---|
163 | { |
---|
164 | m_date_gen_formatter.elements(new_strings, beg_pos); |
---|
165 | } |
---|
166 | |
---|
167 | OutItrT put(OutItrT next, |
---|
168 | std::ios_base& a_ios, |
---|
169 | char_type fill_char, |
---|
170 | const date_type& d) const |
---|
171 | { |
---|
172 | if (d.is_special()) { |
---|
173 | return do_put_special(next, a_ios, fill_char, d.as_special()); |
---|
174 | } |
---|
175 | //The following line of code required the date to support a to_tm function |
---|
176 | return do_put_tm(next, a_ios, fill_char, to_tm(d), m_format); |
---|
177 | } |
---|
178 | |
---|
179 | OutItrT put(OutItrT next, |
---|
180 | std::ios_base& a_ios, |
---|
181 | char_type fill_char, |
---|
182 | const duration_type& dd) const |
---|
183 | { |
---|
184 | if (dd.is_special()) { |
---|
185 | return do_put_special(next, a_ios, fill_char, dd.get_rep().as_special()); |
---|
186 | } |
---|
187 | |
---|
188 | typedef std::num_put<CharT, OutItrT> num_put; |
---|
189 | if (std::has_facet<num_put>(a_ios.getloc())) { |
---|
190 | return std::use_facet<num_put>(a_ios.getloc()).put(next, a_ios, fill_char, dd.get_rep().as_number()); |
---|
191 | } |
---|
192 | else { |
---|
193 | num_put* f = new num_put(); |
---|
194 | std::locale l = std::locale(a_ios.getloc(), f); |
---|
195 | a_ios.imbue(l); |
---|
196 | return f->put(next, a_ios, fill_char, dd.get_rep().as_number()); |
---|
197 | } |
---|
198 | |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | OutItrT put(OutItrT next, |
---|
203 | std::ios_base& a_ios, |
---|
204 | char_type fill_char, |
---|
205 | const month_type& m) const |
---|
206 | { |
---|
207 | //if (d.is_special()) { |
---|
208 | // return do_put_special(next, a_ios, fill_char, d.as_special()); |
---|
209 | //} |
---|
210 | //The following line of code required the date to support a to_tm function |
---|
211 | std::tm dtm = {}; |
---|
212 | dtm.tm_mon = m - 1; |
---|
213 | return do_put_tm(next, a_ios, fill_char, dtm, m_month_format); |
---|
214 | } |
---|
215 | |
---|
216 | //! puts the day of month |
---|
217 | OutItrT put(OutItrT next, |
---|
218 | std::ios_base& a_ios, |
---|
219 | char_type fill_char, |
---|
220 | const day_type& day) const |
---|
221 | { |
---|
222 | std::tm dtm = {}; |
---|
223 | dtm.tm_mday = day.as_number(); |
---|
224 | char_type tmp[3] = {'%','d'}; |
---|
225 | string_type temp_format(tmp); |
---|
226 | return do_put_tm(next, a_ios, fill_char, dtm, temp_format); |
---|
227 | } |
---|
228 | |
---|
229 | OutItrT put(OutItrT next, |
---|
230 | std::ios_base& a_ios, |
---|
231 | char_type fill_char, |
---|
232 | const day_of_week_type& dow) const |
---|
233 | { |
---|
234 | //if (d.is_special()) { |
---|
235 | // return do_put_special(next, a_ios, fill_char, d.as_special()); |
---|
236 | //} |
---|
237 | //The following line of code required the date to support a to_tm function |
---|
238 | std::tm dtm = {}; |
---|
239 | dtm.tm_wday = dow; |
---|
240 | return do_put_tm(next, a_ios, fill_char, dtm, m_weekday_format); |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | OutItrT put(OutItrT next, |
---|
245 | std::ios_base& a_ios, |
---|
246 | char_type fill_char, |
---|
247 | const period_type& p) const |
---|
248 | { |
---|
249 | return m_period_formatter.put_period(next, a_ios, fill_char, p, *this); |
---|
250 | } |
---|
251 | |
---|
252 | OutItrT put(OutItrT next, |
---|
253 | std::ios_base& a_ios, |
---|
254 | char_type fill_char, |
---|
255 | const partial_date_type& pd) const |
---|
256 | { |
---|
257 | return m_date_gen_formatter.put_partial_date(next, a_ios, fill_char, pd, *this); |
---|
258 | } |
---|
259 | |
---|
260 | OutItrT put(OutItrT next, |
---|
261 | std::ios_base& a_ios, |
---|
262 | char_type fill_char, |
---|
263 | const nth_kday_type& nkd) const |
---|
264 | { |
---|
265 | return m_date_gen_formatter.put_nth_kday(next, a_ios, fill_char, nkd, *this); |
---|
266 | } |
---|
267 | |
---|
268 | OutItrT put(OutItrT next, |
---|
269 | std::ios_base& a_ios, |
---|
270 | char_type fill_char, |
---|
271 | const first_kday_type& fkd) const |
---|
272 | { |
---|
273 | return m_date_gen_formatter.put_first_kday(next, a_ios, fill_char, fkd, *this); |
---|
274 | } |
---|
275 | |
---|
276 | OutItrT put(OutItrT next, |
---|
277 | std::ios_base& a_ios, |
---|
278 | char_type fill_char, |
---|
279 | const last_kday_type& lkd) const |
---|
280 | { |
---|
281 | return m_date_gen_formatter.put_last_kday(next, a_ios, fill_char, lkd, *this); |
---|
282 | } |
---|
283 | |
---|
284 | OutItrT put(OutItrT next, |
---|
285 | std::ios_base& a_ios, |
---|
286 | char_type fill_char, |
---|
287 | const kday_before_type& fkb) const |
---|
288 | { |
---|
289 | return m_date_gen_formatter.put_kday_before(next, a_ios, fill_char, fkb, *this); |
---|
290 | } |
---|
291 | |
---|
292 | OutItrT put(OutItrT next, |
---|
293 | std::ios_base& a_ios, |
---|
294 | char_type fill_char, |
---|
295 | const kday_after_type& fka) const |
---|
296 | { |
---|
297 | return m_date_gen_formatter.put_kday_after(next, a_ios, fill_char, fka, *this); |
---|
298 | } |
---|
299 | |
---|
300 | protected: |
---|
301 | virtual OutItrT do_put_special(OutItrT next, |
---|
302 | std::ios_base& /*a_ios*/, |
---|
303 | char_type /*fill_char*/, |
---|
304 | const boost::date_time::special_values sv) const |
---|
305 | { |
---|
306 | m_special_values_formatter.put_special(next, sv); |
---|
307 | return next; |
---|
308 | } |
---|
309 | virtual OutItrT do_put_tm(OutItrT next, |
---|
310 | std::ios_base& a_ios, |
---|
311 | char_type fill_char, |
---|
312 | const tm& tm_value, |
---|
313 | string_type a_format) const |
---|
314 | { |
---|
315 | // update format string with custom names |
---|
316 | if (m_weekday_long_names.size()) { |
---|
317 | boost::algorithm::replace_all(a_format, |
---|
318 | long_weekday_format, |
---|
319 | m_weekday_long_names[tm_value.tm_wday]); |
---|
320 | } |
---|
321 | if (m_weekday_short_names.size()) { |
---|
322 | boost::algorithm::replace_all(a_format, |
---|
323 | short_weekday_format, |
---|
324 | m_weekday_short_names[tm_value.tm_wday]); |
---|
325 | |
---|
326 | } |
---|
327 | if (m_month_long_names.size()) { |
---|
328 | boost::algorithm::replace_all(a_format, |
---|
329 | long_month_format, |
---|
330 | m_month_long_names[tm_value.tm_mon]); |
---|
331 | } |
---|
332 | if (m_month_short_names.size()) { |
---|
333 | boost::algorithm::replace_all(a_format, |
---|
334 | short_month_format, |
---|
335 | m_month_short_names[tm_value.tm_mon]); |
---|
336 | } |
---|
337 | // use time_put facet to create final string |
---|
338 | const char_type* p_format = a_format.c_str(); |
---|
339 | return std::use_facet<std::time_put<CharT> >(a_ios.getloc()).put(next, a_ios, |
---|
340 | fill_char, |
---|
341 | &tm_value, |
---|
342 | p_format, |
---|
343 | p_format + a_format.size()); |
---|
344 | } |
---|
345 | protected: |
---|
346 | string_type m_format; |
---|
347 | string_type m_month_format; |
---|
348 | string_type m_weekday_format; |
---|
349 | period_formatter_type m_period_formatter; |
---|
350 | date_gen_formatter_type m_date_gen_formatter; |
---|
351 | special_values_formatter_type m_special_values_formatter; |
---|
352 | input_collection_type m_month_short_names; |
---|
353 | input_collection_type m_month_long_names; |
---|
354 | input_collection_type m_weekday_short_names; |
---|
355 | input_collection_type m_weekday_long_names; |
---|
356 | private: |
---|
357 | }; |
---|
358 | |
---|
359 | template <class date_type, class CharT, class OutItrT> |
---|
360 | std::locale::id date_facet<date_type, CharT, OutItrT>::id; |
---|
361 | |
---|
362 | template <class date_type, class CharT, class OutItrT> |
---|
363 | const typename date_facet<date_type, CharT, OutItrT>::char_type |
---|
364 | date_facet<date_type, CharT, OutItrT>::long_weekday_format[3] = {'%','A'}; |
---|
365 | |
---|
366 | template <class date_type, class CharT, class OutItrT> |
---|
367 | const typename date_facet<date_type, CharT, OutItrT>::char_type |
---|
368 | date_facet<date_type, CharT, OutItrT>::short_weekday_format[3] = {'%','a'}; |
---|
369 | |
---|
370 | template <class date_type, class CharT, class OutItrT> |
---|
371 | const typename date_facet<date_type, CharT, OutItrT>::char_type |
---|
372 | date_facet<date_type, CharT, OutItrT>::long_month_format[3] = {'%','B'}; |
---|
373 | |
---|
374 | template <class date_type, class CharT, class OutItrT> |
---|
375 | const typename date_facet<date_type, CharT, OutItrT>::char_type |
---|
376 | date_facet<date_type, CharT, OutItrT>::short_month_format[3] = {'%','b'}; |
---|
377 | |
---|
378 | template <class date_type, class CharT, class OutItrT> |
---|
379 | const typename date_facet<date_type, CharT, OutItrT>::char_type |
---|
380 | date_facet<date_type, CharT, OutItrT>::default_period_separator[4] = { ' ', '/', ' '}; |
---|
381 | |
---|
382 | template <class date_type, class CharT, class OutItrT> |
---|
383 | const typename date_facet<date_type, CharT, OutItrT>::char_type |
---|
384 | date_facet<date_type, CharT, OutItrT>::standard_format_specifier[3] = |
---|
385 | {'%', 'x' }; |
---|
386 | |
---|
387 | template <class date_type, class CharT, class OutItrT> |
---|
388 | const typename date_facet<date_type, CharT, OutItrT>::char_type |
---|
389 | date_facet<date_type, CharT, OutItrT>::iso_format_specifier[7] = |
---|
390 | {'%', 'Y', '%', 'm', '%', 'd' }; |
---|
391 | |
---|
392 | template <class date_type, class CharT, class OutItrT> |
---|
393 | const typename date_facet<date_type, CharT, OutItrT>::char_type |
---|
394 | date_facet<date_type, CharT, OutItrT>::iso_format_extended_specifier[9] = |
---|
395 | {'%', 'Y', '-', '%', 'm', '-', '%', 'd' }; |
---|
396 | |
---|
397 | template <class date_type, class CharT, class OutItrT> |
---|
398 | const typename date_facet<date_type, CharT, OutItrT>::char_type |
---|
399 | date_facet<date_type, CharT, OutItrT>::default_date_format[9] = |
---|
400 | {'%','Y','-','%','b','-','%','d'}; |
---|
401 | |
---|
402 | |
---|
403 | |
---|
404 | //! Input facet |
---|
405 | template <class date_type, |
---|
406 | class CharT, |
---|
407 | class InItrT = std::istreambuf_iterator<CharT, std::char_traits<CharT> > > |
---|
408 | class date_input_facet : public std::locale::facet { |
---|
409 | public: |
---|
410 | typedef typename date_type::duration_type duration_type; |
---|
411 | // greg_weekday is gregorian_calendar::day_of_week_type |
---|
412 | typedef typename date_type::day_of_week_type day_of_week_type; |
---|
413 | typedef typename date_type::day_type day_type; |
---|
414 | typedef typename date_type::month_type month_type; |
---|
415 | typedef typename date_type::year_type year_type; |
---|
416 | typedef boost::date_time::period<date_type,duration_type> period_type; |
---|
417 | typedef std::basic_string<CharT> string_type; |
---|
418 | typedef CharT char_type; |
---|
419 | typedef boost::date_time::period_parser<date_type, CharT> period_parser_type; |
---|
420 | typedef boost::date_time::special_values_parser<date_type,CharT> special_values_parser_type; |
---|
421 | typedef std::vector<std::basic_string<CharT> > input_collection_type; |
---|
422 | typedef format_date_parser<date_type, CharT> format_date_parser_type; |
---|
423 | // date_generators stuff goes here |
---|
424 | typedef date_generator_parser<date_type, CharT> date_gen_parser_type; |
---|
425 | typedef partial_date<date_type> partial_date_type; |
---|
426 | typedef nth_kday_of_month<date_type> nth_kday_type; |
---|
427 | typedef first_kday_of_month<date_type> first_kday_type; |
---|
428 | typedef last_kday_of_month<date_type> last_kday_type; |
---|
429 | typedef first_kday_after<date_type> kday_after_type; |
---|
430 | typedef first_kday_before<date_type> kday_before_type; |
---|
431 | |
---|
432 | static const char_type long_weekday_format[3]; |
---|
433 | static const char_type short_weekday_format[3]; |
---|
434 | static const char_type long_month_format[3]; |
---|
435 | static const char_type short_month_format[3]; |
---|
436 | static const char_type four_digit_year_format[3]; |
---|
437 | static const char_type two_digit_year_format[3]; |
---|
438 | static const char_type default_period_separator[4]; |
---|
439 | static const char_type standard_format_specifier[3]; |
---|
440 | static const char_type iso_format_specifier[7]; |
---|
441 | static const char_type iso_format_extended_specifier[9]; |
---|
442 | static const char_type default_date_format[9]; // YYYY-Mon-DD |
---|
443 | static std::locale::id id; |
---|
444 | |
---|
445 | explicit date_input_facet(::size_t a_ref = 0) |
---|
446 | : std::locale::facet(a_ref), |
---|
447 | m_format(default_date_format), |
---|
448 | m_month_format(short_month_format), |
---|
449 | m_weekday_format(short_weekday_format), |
---|
450 | m_year_format(four_digit_year_format), |
---|
451 | m_parser(m_format, std::locale::classic()) |
---|
452 | // default period_parser & special_values_parser used |
---|
453 | {} |
---|
454 | |
---|
455 | explicit date_input_facet(const string_type& format_str, |
---|
456 | ::size_t a_ref = 0) |
---|
457 | : std::locale::facet(a_ref), |
---|
458 | m_format(format_str), |
---|
459 | m_month_format(short_month_format), |
---|
460 | m_weekday_format(short_weekday_format), |
---|
461 | m_year_format(four_digit_year_format), |
---|
462 | m_parser(m_format, std::locale::classic()) |
---|
463 | // default period_parser & special_values_parser used |
---|
464 | {} |
---|
465 | |
---|
466 | explicit date_input_facet(const string_type& format_str, |
---|
467 | const format_date_parser_type& date_parser, |
---|
468 | const special_values_parser_type& sv_parser, |
---|
469 | const period_parser_type& per_parser, |
---|
470 | const date_gen_parser_type& date_gen_parser, |
---|
471 | ::size_t ref_count = 0) |
---|
472 | : std::locale::facet(ref_count), |
---|
473 | m_format(format_str), |
---|
474 | m_month_format(short_month_format), |
---|
475 | m_weekday_format(short_weekday_format), |
---|
476 | m_year_format(four_digit_year_format), |
---|
477 | m_parser(date_parser), |
---|
478 | m_date_gen_parser(date_gen_parser), |
---|
479 | m_period_parser(per_parser), |
---|
480 | m_sv_parser(sv_parser) |
---|
481 | {} |
---|
482 | |
---|
483 | |
---|
484 | void format(const char_type* const format_str) { |
---|
485 | m_format = format_str; |
---|
486 | } |
---|
487 | virtual void set_iso_format() |
---|
488 | { |
---|
489 | m_format = iso_format_specifier; |
---|
490 | } |
---|
491 | virtual void set_iso_extended_format() |
---|
492 | { |
---|
493 | m_format = iso_format_extended_specifier; |
---|
494 | } |
---|
495 | void month_format(const char_type* const format_str) { |
---|
496 | m_month_format = format_str; |
---|
497 | } |
---|
498 | void weekday_format(const char_type* const format_str) { |
---|
499 | m_weekday_format = format_str; |
---|
500 | } |
---|
501 | void year_format(const char_type* const format_str) { |
---|
502 | m_year_format = format_str; |
---|
503 | } |
---|
504 | |
---|
505 | void period_parser(period_parser_type per_parser) { |
---|
506 | m_period_parser = per_parser; |
---|
507 | } |
---|
508 | void short_weekday_names(const input_collection_type& weekday_names) |
---|
509 | { |
---|
510 | m_parser.short_weekday_names(weekday_names); |
---|
511 | } |
---|
512 | void long_weekday_names(const input_collection_type& weekday_names) |
---|
513 | { |
---|
514 | m_parser.long_weekday_names(weekday_names); |
---|
515 | } |
---|
516 | |
---|
517 | void short_month_names(const input_collection_type& month_names) |
---|
518 | { |
---|
519 | m_parser.short_month_names(month_names); |
---|
520 | } |
---|
521 | |
---|
522 | void long_month_names(const input_collection_type& month_names) |
---|
523 | { |
---|
524 | m_parser.long_month_names(month_names); |
---|
525 | } |
---|
526 | |
---|
527 | void date_gen_element_strings(const input_collection_type& col) |
---|
528 | { |
---|
529 | m_date_gen_parser.element_strings(col); |
---|
530 | } |
---|
531 | void date_gen_element_strings(const string_type& first, |
---|
532 | const string_type& second, |
---|
533 | const string_type& third, |
---|
534 | const string_type& fourth, |
---|
535 | const string_type& fifth, |
---|
536 | const string_type& last, |
---|
537 | const string_type& before, |
---|
538 | const string_type& after, |
---|
539 | const string_type& of) |
---|
540 | |
---|
541 | { |
---|
542 | m_date_gen_parser.element_strings(first,second,third,fourth,fifth,last,before,after,of); |
---|
543 | } |
---|
544 | |
---|
545 | void special_values_parser(special_values_parser_type sv_parser) |
---|
546 | { |
---|
547 | m_sv_parser = sv_parser; |
---|
548 | } |
---|
549 | |
---|
550 | InItrT get(InItrT& from, |
---|
551 | InItrT& to, |
---|
552 | std::ios_base& /*a_ios*/, |
---|
553 | date_type& d) const |
---|
554 | { |
---|
555 | d = m_parser.parse_date(from, to, m_format, m_sv_parser); |
---|
556 | return from; |
---|
557 | } |
---|
558 | InItrT get(InItrT& from, |
---|
559 | InItrT& to, |
---|
560 | std::ios_base& /*a_ios*/, |
---|
561 | month_type& m) const |
---|
562 | { |
---|
563 | m = m_parser.parse_month(from, to, m_month_format); |
---|
564 | return from; |
---|
565 | } |
---|
566 | InItrT get(InItrT& from, |
---|
567 | InItrT& to, |
---|
568 | std::ios_base& /*a_ios*/, |
---|
569 | day_of_week_type& wd) const |
---|
570 | { |
---|
571 | wd = m_parser.parse_weekday(from, to, m_weekday_format); |
---|
572 | return from; |
---|
573 | } |
---|
574 | //! Expects 1 or 2 digit day range: 1-31 |
---|
575 | InItrT get(InItrT& from, |
---|
576 | InItrT& to, |
---|
577 | std::ios_base& /*a_ios*/, |
---|
578 | day_type& d) const |
---|
579 | { |
---|
580 | d = m_parser.parse_var_day_of_month(from, to); |
---|
581 | return from; |
---|
582 | } |
---|
583 | InItrT get(InItrT& from, |
---|
584 | InItrT& to, |
---|
585 | std::ios_base& /*a_ios*/, |
---|
586 | year_type& y) const |
---|
587 | { |
---|
588 | y = m_parser.parse_year(from, to, m_year_format); |
---|
589 | return from; |
---|
590 | } |
---|
591 | InItrT get(InItrT& from, |
---|
592 | InItrT& to, |
---|
593 | std::ios_base& a_ios, |
---|
594 | duration_type& dd) const |
---|
595 | { |
---|
596 | // skip leading whitespace |
---|
597 | while(std::isspace(*from) && from != to) { ++from; } |
---|
598 | |
---|
599 | /* num_get.get() will always consume the first character if it |
---|
600 | * is a sign indicator (+/-). Special value strings may begin |
---|
601 | * with one of these signs so we'll need a copy of it |
---|
602 | * in case num_get.get() fails. */ |
---|
603 | char_type c = '\0'; |
---|
604 | // TODO Are these characters somewhere in the locale? |
---|
605 | if(*from == '-' || *from == '+') { |
---|
606 | c = *from; |
---|
607 | } |
---|
608 | typedef std::num_get<CharT, InItrT> num_get; |
---|
609 | typename duration_type::duration_rep_type val = 0; |
---|
610 | std::ios_base::iostate err = std::ios_base::goodbit; |
---|
611 | |
---|
612 | if (std::has_facet<num_get>(a_ios.getloc())) { |
---|
613 | from = std::use_facet<num_get>(a_ios.getloc()).get(from, to, a_ios, err, val); |
---|
614 | } |
---|
615 | else { |
---|
616 | num_get* ng = new num_get(); |
---|
617 | std::locale l = std::locale(a_ios.getloc(), ng); |
---|
618 | a_ios.imbue(l); |
---|
619 | from = ng->get(from, to, a_ios, err, val); |
---|
620 | } |
---|
621 | if(err & std::ios_base::failbit){ |
---|
622 | typedef typename special_values_parser_type::match_results match_results; |
---|
623 | match_results mr; |
---|
624 | if(c == '-' || c == '+') { // was the first character consumed? |
---|
625 | mr.cache += c; |
---|
626 | } |
---|
627 | m_sv_parser.match(from, to, mr); |
---|
628 | if(mr.current_match == match_results::PARSE_ERROR) { |
---|
629 | boost::throw_exception(std::ios_base::failure("Parse failed. No match found for '" + mr.cache + "'")); |
---|
630 | BOOST_DATE_TIME_UNREACHABLE_EXPRESSION(return from); // should never reach |
---|
631 | } |
---|
632 | dd = duration_type(static_cast<special_values>(mr.current_match)); |
---|
633 | } |
---|
634 | else { |
---|
635 | dd = duration_type(val); |
---|
636 | } |
---|
637 | return from; |
---|
638 | } |
---|
639 | InItrT get(InItrT& from, |
---|
640 | InItrT& to, |
---|
641 | std::ios_base& a_ios, |
---|
642 | period_type& p) const |
---|
643 | { |
---|
644 | p = m_period_parser.get_period(from, to, a_ios, p, duration_type::unit(), *this); |
---|
645 | return from; |
---|
646 | } |
---|
647 | InItrT get(InItrT& from, |
---|
648 | InItrT& to, |
---|
649 | std::ios_base& a_ios, |
---|
650 | nth_kday_type& nkd) const |
---|
651 | { |
---|
652 | nkd = m_date_gen_parser.get_nth_kday_type(from, to, a_ios, *this); |
---|
653 | return from; |
---|
654 | } |
---|
655 | InItrT get(InItrT& from, |
---|
656 | InItrT& to, |
---|
657 | std::ios_base& a_ios, |
---|
658 | partial_date_type& pd) const |
---|
659 | { |
---|
660 | |
---|
661 | pd = m_date_gen_parser.get_partial_date_type(from, to, a_ios, *this); |
---|
662 | return from; |
---|
663 | } |
---|
664 | InItrT get(InItrT& from, |
---|
665 | InItrT& to, |
---|
666 | std::ios_base& a_ios, |
---|
667 | first_kday_type& fkd) const |
---|
668 | { |
---|
669 | fkd = m_date_gen_parser.get_first_kday_type(from, to, a_ios, *this); |
---|
670 | return from; |
---|
671 | } |
---|
672 | InItrT get(InItrT& from, |
---|
673 | InItrT& to, |
---|
674 | std::ios_base& a_ios, |
---|
675 | last_kday_type& lkd) const |
---|
676 | { |
---|
677 | lkd = m_date_gen_parser.get_last_kday_type(from, to, a_ios, *this); |
---|
678 | return from; |
---|
679 | } |
---|
680 | InItrT get(InItrT& from, |
---|
681 | InItrT& to, |
---|
682 | std::ios_base& a_ios, |
---|
683 | kday_before_type& fkb) const |
---|
684 | { |
---|
685 | fkb = m_date_gen_parser.get_kday_before_type(from, to, a_ios, *this); |
---|
686 | return from; |
---|
687 | } |
---|
688 | InItrT get(InItrT& from, |
---|
689 | InItrT& to, |
---|
690 | std::ios_base& a_ios, |
---|
691 | kday_after_type& fka) const |
---|
692 | { |
---|
693 | fka = m_date_gen_parser.get_kday_after_type(from, to, a_ios, *this); |
---|
694 | return from; |
---|
695 | } |
---|
696 | |
---|
697 | protected: |
---|
698 | string_type m_format; |
---|
699 | string_type m_month_format; |
---|
700 | string_type m_weekday_format; |
---|
701 | string_type m_year_format; |
---|
702 | format_date_parser_type m_parser; |
---|
703 | date_gen_parser_type m_date_gen_parser; |
---|
704 | period_parser_type m_period_parser; |
---|
705 | special_values_parser_type m_sv_parser; |
---|
706 | private: |
---|
707 | }; |
---|
708 | |
---|
709 | |
---|
710 | template <class date_type, class CharT, class OutItrT> |
---|
711 | std::locale::id date_input_facet<date_type, CharT, OutItrT>::id; |
---|
712 | |
---|
713 | template <class date_type, class CharT, class OutItrT> |
---|
714 | const typename date_input_facet<date_type, CharT, OutItrT>::char_type |
---|
715 | date_input_facet<date_type, CharT, OutItrT>::long_weekday_format[3] = {'%','A'}; |
---|
716 | |
---|
717 | template <class date_type, class CharT, class OutItrT> |
---|
718 | const typename date_input_facet<date_type, CharT, OutItrT>::char_type |
---|
719 | date_input_facet<date_type, CharT, OutItrT>::short_weekday_format[3] = {'%','a'}; |
---|
720 | |
---|
721 | template <class date_type, class CharT, class OutItrT> |
---|
722 | const typename date_input_facet<date_type, CharT, OutItrT>::char_type |
---|
723 | date_input_facet<date_type, CharT, OutItrT>::long_month_format[3] = {'%','B'}; |
---|
724 | |
---|
725 | template <class date_type, class CharT, class OutItrT> |
---|
726 | const typename date_input_facet<date_type, CharT, OutItrT>::char_type |
---|
727 | date_input_facet<date_type, CharT, OutItrT>::short_month_format[3] = {'%','b'}; |
---|
728 | |
---|
729 | template <class date_type, class CharT, class OutItrT> |
---|
730 | const typename date_input_facet<date_type, CharT, OutItrT>::char_type |
---|
731 | date_input_facet<date_type, CharT, OutItrT>::four_digit_year_format[3] = {'%','Y'}; |
---|
732 | |
---|
733 | template <class date_type, class CharT, class OutItrT> |
---|
734 | const typename date_input_facet<date_type, CharT, OutItrT>::char_type |
---|
735 | date_input_facet<date_type, CharT, OutItrT>::two_digit_year_format[3] = {'%','y'}; |
---|
736 | |
---|
737 | template <class date_type, class CharT, class OutItrT> |
---|
738 | const typename date_input_facet<date_type, CharT, OutItrT>::char_type |
---|
739 | date_input_facet<date_type, CharT, OutItrT>::default_period_separator[4] = { ' ', '/', ' '}; |
---|
740 | |
---|
741 | template <class date_type, class CharT, class OutItrT> |
---|
742 | const typename date_input_facet<date_type, CharT, OutItrT>::char_type |
---|
743 | date_input_facet<date_type, CharT, OutItrT>::standard_format_specifier[3] = |
---|
744 | {'%', 'x' }; |
---|
745 | |
---|
746 | template <class date_type, class CharT, class OutItrT> |
---|
747 | const typename date_input_facet<date_type, CharT, OutItrT>::char_type |
---|
748 | date_input_facet<date_type, CharT, OutItrT>::iso_format_specifier[7] = |
---|
749 | {'%', 'Y', '%', 'm', '%', 'd' }; |
---|
750 | |
---|
751 | template <class date_type, class CharT, class OutItrT> |
---|
752 | const typename date_input_facet<date_type, CharT, OutItrT>::char_type |
---|
753 | date_input_facet<date_type, CharT, OutItrT>::iso_format_extended_specifier[9] = |
---|
754 | {'%', 'Y', '-', '%', 'm', '-', '%', 'd' }; |
---|
755 | |
---|
756 | template <class date_type, class CharT, class OutItrT> |
---|
757 | const typename date_input_facet<date_type, CharT, OutItrT>::char_type |
---|
758 | date_input_facet<date_type, CharT, OutItrT>::default_date_format[9] = |
---|
759 | {'%','Y','-','%','b','-','%','d'}; |
---|
760 | |
---|
761 | } } // namespaces |
---|
762 | |
---|
763 | |
---|
764 | #endif |
---|