Chris@16
|
1
|
Chris@16
|
2 #ifndef DATE_TIME_FORMAT_DATE_PARSER_HPP__
|
Chris@16
|
3 #define DATE_TIME_FORMAT_DATE_PARSER_HPP__
|
Chris@16
|
4
|
Chris@16
|
5 /* Copyright (c) 2004-2005 CrystalClear Software, Inc.
|
Chris@16
|
6 * Use, modification and distribution is subject to the
|
Chris@16
|
7 * Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 * Author: Jeff Garland, Bart Garst
|
Chris@101
|
10 * $Date$
|
Chris@16
|
11 */
|
Chris@16
|
12
|
Chris@16
|
13
|
Chris@16
|
14 #include "boost/lexical_cast.hpp"
|
Chris@16
|
15 #include "boost/date_time/string_parse_tree.hpp"
|
Chris@16
|
16 #include "boost/date_time/strings_from_facet.hpp"
|
Chris@16
|
17 #include "boost/date_time/special_values_parser.hpp"
|
Chris@16
|
18 #include <string>
|
Chris@16
|
19 #include <vector>
|
Chris@16
|
20 #include <sstream>
|
Chris@16
|
21 #include <iterator>
|
Chris@16
|
22 #ifndef BOOST_NO_STDC_NAMESPACE
|
Chris@16
|
23 # include <cctype>
|
Chris@16
|
24 #else
|
Chris@16
|
25 # include <ctype.h>
|
Chris@16
|
26 #endif
|
Chris@16
|
27
|
Chris@16
|
28 #ifdef BOOST_NO_STDC_NAMESPACE
|
Chris@16
|
29 namespace std {
|
Chris@16
|
30 using ::isspace;
|
Chris@16
|
31 using ::isdigit;
|
Chris@16
|
32 }
|
Chris@16
|
33 #endif
|
Chris@16
|
34 namespace boost { namespace date_time {
|
Chris@16
|
35
|
Chris@16
|
36 //! Helper function for parsing fixed length strings into integers
|
Chris@16
|
37 /*! Will consume 'length' number of characters from stream. Consumed
|
Chris@16
|
38 * character are transfered to parse_match_result struct.
|
Chris@16
|
39 * Returns '-1' if no number can be parsed or incorrect number of
|
Chris@16
|
40 * digits in stream. */
|
Chris@16
|
41 template<typename int_type, typename charT>
|
Chris@16
|
42 inline
|
Chris@16
|
43 int_type
|
Chris@16
|
44 fixed_string_to_int(std::istreambuf_iterator<charT>& itr,
|
Chris@16
|
45 std::istreambuf_iterator<charT>& stream_end,
|
Chris@16
|
46 parse_match_result<charT>& mr,
|
Chris@16
|
47 unsigned int length,
|
Chris@16
|
48 const charT& fill_char)
|
Chris@16
|
49 {
|
Chris@16
|
50 //typedef std::basic_string<charT> string_type;
|
Chris@16
|
51 unsigned int j = 0;
|
Chris@16
|
52 //string_type s;
|
Chris@16
|
53 while (j < length && itr != stream_end &&
|
Chris@16
|
54 (std::isdigit(*itr) || *itr == fill_char)) {
|
Chris@16
|
55 if(*itr == fill_char) {
|
Chris@16
|
56 /* Since a fill_char can be anything, we convert it to a zero.
|
Chris@16
|
57 * lexical_cast will behave predictably when zero is used as fill. */
|
Chris@16
|
58 mr.cache += ('0');
|
Chris@16
|
59 }
|
Chris@16
|
60 else {
|
Chris@16
|
61 mr.cache += (*itr);
|
Chris@16
|
62 }
|
Chris@16
|
63 itr++;
|
Chris@16
|
64 j++;
|
Chris@16
|
65 }
|
Chris@101
|
66 int_type i = static_cast<int_type>(-1);
|
Chris@16
|
67 // mr.cache will hold leading zeros. size() tells us when input is too short.
|
Chris@16
|
68 if(mr.cache.size() < length) {
|
Chris@16
|
69 return i;
|
Chris@16
|
70 }
|
Chris@16
|
71 try {
|
Chris@16
|
72 i = boost::lexical_cast<int_type>(mr.cache);
|
Chris@16
|
73 }catch(bad_lexical_cast&){
|
Chris@16
|
74 // we want to return -1 if the cast fails so nothing to do here
|
Chris@16
|
75 }
|
Chris@16
|
76 return i;
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 //! Helper function for parsing fixed length strings into integers
|
Chris@16
|
80 /*! Will consume 'length' number of characters from stream. Consumed
|
Chris@16
|
81 * character are transfered to parse_match_result struct.
|
Chris@16
|
82 * Returns '-1' if no number can be parsed or incorrect number of
|
Chris@16
|
83 * digits in stream. */
|
Chris@16
|
84 template<typename int_type, typename charT>
|
Chris@16
|
85 inline
|
Chris@16
|
86 int_type
|
Chris@16
|
87 fixed_string_to_int(std::istreambuf_iterator<charT>& itr,
|
Chris@16
|
88 std::istreambuf_iterator<charT>& stream_end,
|
Chris@16
|
89 parse_match_result<charT>& mr,
|
Chris@16
|
90 unsigned int length)
|
Chris@16
|
91 {
|
Chris@16
|
92 return fixed_string_to_int<int_type, charT>(itr, stream_end, mr, length, '0');
|
Chris@16
|
93 }
|
Chris@16
|
94
|
Chris@16
|
95 //! Helper function for parsing varied length strings into integers
|
Chris@16
|
96 /*! Will consume 'max_length' characters from stream only if those
|
Chris@16
|
97 * characters are digits. Returns '-1' if no number can be parsed.
|
Chris@16
|
98 * Will not parse a number preceeded by a '+' or '-'. */
|
Chris@16
|
99 template<typename int_type, typename charT>
|
Chris@16
|
100 inline
|
Chris@16
|
101 int_type
|
Chris@16
|
102 var_string_to_int(std::istreambuf_iterator<charT>& itr,
|
Chris@16
|
103 const std::istreambuf_iterator<charT>& stream_end,
|
Chris@16
|
104 unsigned int max_length)
|
Chris@16
|
105 {
|
Chris@16
|
106 typedef std::basic_string<charT> string_type;
|
Chris@16
|
107 unsigned int j = 0;
|
Chris@16
|
108 string_type s;
|
Chris@16
|
109 while (itr != stream_end && (j < max_length) && std::isdigit(*itr)) {
|
Chris@16
|
110 s += (*itr);
|
Chris@16
|
111 ++itr;
|
Chris@16
|
112 ++j;
|
Chris@16
|
113 }
|
Chris@101
|
114 int_type i = static_cast<int_type>(-1);
|
Chris@16
|
115 if(!s.empty()) {
|
Chris@16
|
116 i = boost::lexical_cast<int_type>(s);
|
Chris@16
|
117 }
|
Chris@16
|
118 return i;
|
Chris@16
|
119 }
|
Chris@16
|
120
|
Chris@16
|
121
|
Chris@16
|
122 //! Class with generic date parsing using a format string
|
Chris@16
|
123 /*! The following is the set of recognized format specifiers
|
Chris@16
|
124 - %a - Short weekday name
|
Chris@16
|
125 - %A - Long weekday name
|
Chris@16
|
126 - %b - Abbreviated month name
|
Chris@16
|
127 - %B - Full month name
|
Chris@16
|
128 - %d - Day of the month as decimal 01 to 31
|
Chris@16
|
129 - %j - Day of year as decimal from 001 to 366
|
Chris@16
|
130 - %m - Month name as a decimal 01 to 12
|
Chris@16
|
131 - %U - Week number 00 to 53 with first Sunday as the first day of week 1?
|
Chris@16
|
132 - %w - Weekday as decimal number 0 to 6 where Sunday == 0
|
Chris@16
|
133 - %W - Week number 00 to 53 where Monday is first day of week 1
|
Chris@16
|
134 - %x - facet default date representation
|
Chris@16
|
135 - %y - Year without the century - eg: 04 for 2004
|
Chris@16
|
136 - %Y - Year with century
|
Chris@16
|
137
|
Chris@16
|
138 The weekday specifiers (%a and %A) do not add to the date construction,
|
Chris@16
|
139 but they provide a way to skip over the weekday names for formats that
|
Chris@16
|
140 provide them.
|
Chris@16
|
141
|
Chris@16
|
142 todo -- Another interesting feature that this approach could provide is
|
Chris@16
|
143 an option to fill in any missing fields with the current values
|
Chris@16
|
144 from the clock. So if you have %m-%d the parser would detect
|
Chris@16
|
145 the missing year value and fill it in using the clock.
|
Chris@16
|
146
|
Chris@16
|
147 todo -- What to do with the %x. %x in the classic facet is just bad...
|
Chris@16
|
148
|
Chris@16
|
149 */
|
Chris@16
|
150 template<class date_type, typename charT>
|
Chris@16
|
151 class format_date_parser
|
Chris@16
|
152 {
|
Chris@16
|
153 public:
|
Chris@16
|
154 typedef std::basic_string<charT> string_type;
|
Chris@16
|
155 typedef std::basic_istringstream<charT> stringstream_type;
|
Chris@16
|
156 typedef std::istreambuf_iterator<charT> stream_itr_type;
|
Chris@16
|
157 typedef typename string_type::const_iterator const_itr;
|
Chris@16
|
158 typedef typename date_type::year_type year_type;
|
Chris@16
|
159 typedef typename date_type::month_type month_type;
|
Chris@16
|
160 typedef typename date_type::day_type day_type;
|
Chris@16
|
161 typedef typename date_type::duration_type duration_type;
|
Chris@16
|
162 typedef typename date_type::day_of_week_type day_of_week_type;
|
Chris@16
|
163 typedef typename date_type::day_of_year_type day_of_year_type;
|
Chris@16
|
164 typedef string_parse_tree<charT> parse_tree_type;
|
Chris@16
|
165 typedef typename parse_tree_type::parse_match_result_type match_results;
|
Chris@16
|
166 typedef std::vector<std::basic_string<charT> > input_collection_type;
|
Chris@16
|
167
|
Chris@16
|
168 // TODO sv_parser uses its default constructor - write the others
|
Chris@16
|
169
|
Chris@16
|
170 format_date_parser(const string_type& format_str,
|
Chris@16
|
171 const input_collection_type& month_short_names,
|
Chris@16
|
172 const input_collection_type& month_long_names,
|
Chris@16
|
173 const input_collection_type& weekday_short_names,
|
Chris@16
|
174 const input_collection_type& weekday_long_names) :
|
Chris@16
|
175 m_format(format_str),
|
Chris@16
|
176 m_month_short_names(month_short_names, 1),
|
Chris@16
|
177 m_month_long_names(month_long_names, 1),
|
Chris@16
|
178 m_weekday_short_names(weekday_short_names),
|
Chris@16
|
179 m_weekday_long_names(weekday_long_names)
|
Chris@16
|
180 {}
|
Chris@16
|
181
|
Chris@16
|
182 format_date_parser(const string_type& format_str,
|
Chris@16
|
183 const std::locale& locale) :
|
Chris@16
|
184 m_format(format_str),
|
Chris@16
|
185 m_month_short_names(gather_month_strings<charT>(locale), 1),
|
Chris@16
|
186 m_month_long_names(gather_month_strings<charT>(locale, false), 1),
|
Chris@16
|
187 m_weekday_short_names(gather_weekday_strings<charT>(locale)),
|
Chris@16
|
188 m_weekday_long_names(gather_weekday_strings<charT>(locale, false))
|
Chris@16
|
189 {}
|
Chris@16
|
190
|
Chris@16
|
191 format_date_parser(const format_date_parser<date_type,charT>& fdp)
|
Chris@16
|
192 {
|
Chris@16
|
193 this->m_format = fdp.m_format;
|
Chris@16
|
194 this->m_month_short_names = fdp.m_month_short_names;
|
Chris@16
|
195 this->m_month_long_names = fdp.m_month_long_names;
|
Chris@16
|
196 this->m_weekday_short_names = fdp.m_weekday_short_names;
|
Chris@16
|
197 this->m_weekday_long_names = fdp.m_weekday_long_names;
|
Chris@16
|
198 }
|
Chris@16
|
199
|
Chris@16
|
200 string_type format() const
|
Chris@16
|
201 {
|
Chris@16
|
202 return m_format;
|
Chris@16
|
203 }
|
Chris@16
|
204
|
Chris@16
|
205 void format(string_type format_str)
|
Chris@16
|
206 {
|
Chris@16
|
207 m_format = format_str;
|
Chris@16
|
208 }
|
Chris@16
|
209
|
Chris@16
|
210 void short_month_names(const input_collection_type& month_names)
|
Chris@16
|
211 {
|
Chris@16
|
212 m_month_short_names = parse_tree_type(month_names, 1);
|
Chris@16
|
213 }
|
Chris@16
|
214 void long_month_names(const input_collection_type& month_names)
|
Chris@16
|
215 {
|
Chris@16
|
216 m_month_long_names = parse_tree_type(month_names, 1);
|
Chris@16
|
217 }
|
Chris@16
|
218 void short_weekday_names(const input_collection_type& weekday_names)
|
Chris@16
|
219 {
|
Chris@16
|
220 m_weekday_short_names = parse_tree_type(weekday_names);
|
Chris@16
|
221 }
|
Chris@16
|
222 void long_weekday_names(const input_collection_type& weekday_names)
|
Chris@16
|
223 {
|
Chris@16
|
224 m_weekday_long_names = parse_tree_type(weekday_names);
|
Chris@16
|
225 }
|
Chris@16
|
226
|
Chris@16
|
227 date_type
|
Chris@16
|
228 parse_date(const string_type& value,
|
Chris@16
|
229 const string_type& format_str,
|
Chris@16
|
230 const special_values_parser<date_type,charT>& sv_parser) const
|
Chris@16
|
231 {
|
Chris@16
|
232 stringstream_type ss(value);
|
Chris@16
|
233 stream_itr_type sitr(ss);
|
Chris@16
|
234 stream_itr_type stream_end;
|
Chris@16
|
235 return parse_date(sitr, stream_end, format_str, sv_parser);
|
Chris@16
|
236 }
|
Chris@16
|
237
|
Chris@16
|
238 date_type
|
Chris@16
|
239 parse_date(std::istreambuf_iterator<charT>& sitr,
|
Chris@16
|
240 std::istreambuf_iterator<charT>& stream_end,
|
Chris@16
|
241 const special_values_parser<date_type,charT>& sv_parser) const
|
Chris@16
|
242 {
|
Chris@16
|
243 return parse_date(sitr, stream_end, m_format, sv_parser);
|
Chris@16
|
244 }
|
Chris@16
|
245
|
Chris@16
|
246 /*! Of all the objects that the format_date_parser can parse, only a
|
Chris@16
|
247 * date can be a special value. Therefore, only parse_date checks
|
Chris@16
|
248 * for special_values. */
|
Chris@16
|
249 date_type
|
Chris@16
|
250 parse_date(std::istreambuf_iterator<charT>& sitr,
|
Chris@16
|
251 std::istreambuf_iterator<charT>& stream_end,
|
Chris@16
|
252 string_type format_str,
|
Chris@16
|
253 const special_values_parser<date_type,charT>& sv_parser) const
|
Chris@16
|
254 {
|
Chris@16
|
255 bool use_current_char = false;
|
Chris@16
|
256
|
Chris@16
|
257 // skip leading whitespace
|
Chris@16
|
258 while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
|
Chris@16
|
259
|
Chris@16
|
260 short year(0), month(0), day(0), day_of_year(0);// wkday(0);
|
Chris@16
|
261 /* Initialized the following to their minimum values. These intermediate
|
Chris@16
|
262 * objects are used so we get specific exceptions when part of the input
|
Chris@16
|
263 * is unparsable.
|
Chris@16
|
264 * Ex: "205-Jan-15" will throw a bad_year, "2005-Jsn-15"- bad_month, etc.*/
|
Chris@16
|
265 year_type t_year(1400);
|
Chris@16
|
266 month_type t_month(1);
|
Chris@16
|
267 day_type t_day(1);
|
Chris@16
|
268 day_of_week_type wkday(0);
|
Chris@16
|
269
|
Chris@16
|
270
|
Chris@16
|
271 const_itr itr(format_str.begin());
|
Chris@16
|
272 while (itr != format_str.end() && (sitr != stream_end)) {
|
Chris@16
|
273 if (*itr == '%') {
|
Chris@16
|
274 if ( ++itr == format_str.end())
|
Chris@16
|
275 break;
|
Chris@16
|
276 if (*itr != '%') {
|
Chris@16
|
277 switch(*itr) {
|
Chris@16
|
278 case 'a':
|
Chris@16
|
279 {
|
Chris@16
|
280 //this value is just throw away. It could be used for
|
Chris@16
|
281 //error checking potentially, but it isn't helpful in
|
Chris@16
|
282 //actually constructing the date - we just need to get it
|
Chris@16
|
283 //out of the stream
|
Chris@16
|
284 match_results mr = m_weekday_short_names.match(sitr, stream_end);
|
Chris@16
|
285 if(mr.current_match == match_results::PARSE_ERROR) {
|
Chris@16
|
286 // check special_values
|
Chris@16
|
287 if(sv_parser.match(sitr, stream_end, mr)) {
|
Chris@16
|
288 return date_type(static_cast<special_values>(mr.current_match));
|
Chris@16
|
289 }
|
Chris@16
|
290 }
|
Chris@16
|
291 wkday = mr.current_match;
|
Chris@16
|
292 if (mr.has_remaining()) {
|
Chris@16
|
293 use_current_char = true;
|
Chris@16
|
294 }
|
Chris@16
|
295 break;
|
Chris@16
|
296 }
|
Chris@16
|
297 case 'A':
|
Chris@16
|
298 {
|
Chris@16
|
299 //this value is just throw away. It could be used for
|
Chris@16
|
300 //error checking potentially, but it isn't helpful in
|
Chris@16
|
301 //actually constructing the date - we just need to get it
|
Chris@16
|
302 //out of the stream
|
Chris@16
|
303 match_results mr = m_weekday_long_names.match(sitr, stream_end);
|
Chris@16
|
304 if(mr.current_match == match_results::PARSE_ERROR) {
|
Chris@16
|
305 // check special_values
|
Chris@16
|
306 if(sv_parser.match(sitr, stream_end, mr)) {
|
Chris@16
|
307 return date_type(static_cast<special_values>(mr.current_match));
|
Chris@16
|
308 }
|
Chris@16
|
309 }
|
Chris@16
|
310 wkday = mr.current_match;
|
Chris@16
|
311 if (mr.has_remaining()) {
|
Chris@16
|
312 use_current_char = true;
|
Chris@16
|
313 }
|
Chris@16
|
314 break;
|
Chris@16
|
315 }
|
Chris@16
|
316 case 'b':
|
Chris@16
|
317 {
|
Chris@16
|
318 match_results mr = m_month_short_names.match(sitr, stream_end);
|
Chris@16
|
319 if(mr.current_match == match_results::PARSE_ERROR) {
|
Chris@16
|
320 // check special_values
|
Chris@16
|
321 if(sv_parser.match(sitr, stream_end, mr)) {
|
Chris@16
|
322 return date_type(static_cast<special_values>(mr.current_match));
|
Chris@16
|
323 }
|
Chris@16
|
324 }
|
Chris@16
|
325 t_month = month_type(mr.current_match);
|
Chris@16
|
326 if (mr.has_remaining()) {
|
Chris@16
|
327 use_current_char = true;
|
Chris@16
|
328 }
|
Chris@16
|
329 break;
|
Chris@16
|
330 }
|
Chris@16
|
331 case 'B':
|
Chris@16
|
332 {
|
Chris@16
|
333 match_results mr = m_month_long_names.match(sitr, stream_end);
|
Chris@16
|
334 if(mr.current_match == match_results::PARSE_ERROR) {
|
Chris@16
|
335 // check special_values
|
Chris@16
|
336 if(sv_parser.match(sitr, stream_end, mr)) {
|
Chris@16
|
337 return date_type(static_cast<special_values>(mr.current_match));
|
Chris@16
|
338 }
|
Chris@16
|
339 }
|
Chris@16
|
340 t_month = month_type(mr.current_match);
|
Chris@16
|
341 if (mr.has_remaining()) {
|
Chris@16
|
342 use_current_char = true;
|
Chris@16
|
343 }
|
Chris@16
|
344 break;
|
Chris@16
|
345 }
|
Chris@16
|
346 case 'd':
|
Chris@16
|
347 {
|
Chris@16
|
348 match_results mr;
|
Chris@16
|
349 day = fixed_string_to_int<short, charT>(sitr, stream_end, mr, 2);
|
Chris@16
|
350 if(day == -1) {
|
Chris@16
|
351 if(sv_parser.match(sitr, stream_end, mr)) {
|
Chris@16
|
352 return date_type(static_cast<special_values>(mr.current_match));
|
Chris@16
|
353 }
|
Chris@16
|
354 }
|
Chris@16
|
355 t_day = day_type(day);
|
Chris@16
|
356 break;
|
Chris@16
|
357 }
|
Chris@16
|
358 case 'e':
|
Chris@16
|
359 {
|
Chris@16
|
360 match_results mr;
|
Chris@16
|
361 day = fixed_string_to_int<short, charT>(sitr, stream_end, mr, 2, ' ');
|
Chris@16
|
362 if(day == -1) {
|
Chris@16
|
363 if(sv_parser.match(sitr, stream_end, mr)) {
|
Chris@16
|
364 return date_type(static_cast<special_values>(mr.current_match));
|
Chris@16
|
365 }
|
Chris@16
|
366 }
|
Chris@16
|
367 t_day = day_type(day);
|
Chris@16
|
368 break;
|
Chris@16
|
369 }
|
Chris@16
|
370 case 'j':
|
Chris@16
|
371 {
|
Chris@16
|
372 match_results mr;
|
Chris@16
|
373 day_of_year = fixed_string_to_int<short, charT>(sitr, stream_end, mr, 3);
|
Chris@16
|
374 if(day_of_year == -1) {
|
Chris@16
|
375 if(sv_parser.match(sitr, stream_end, mr)) {
|
Chris@16
|
376 return date_type(static_cast<special_values>(mr.current_match));
|
Chris@16
|
377 }
|
Chris@16
|
378 }
|
Chris@16
|
379 // these next two lines are so we get an exception with bad input
|
Chris@16
|
380 day_of_year_type t_day_of_year(1);
|
Chris@16
|
381 t_day_of_year = day_of_year_type(day_of_year);
|
Chris@16
|
382 break;
|
Chris@16
|
383 }
|
Chris@16
|
384 case 'm':
|
Chris@16
|
385 {
|
Chris@16
|
386 match_results mr;
|
Chris@16
|
387 month = fixed_string_to_int<short, charT>(sitr, stream_end, mr, 2);
|
Chris@16
|
388 if(month == -1) {
|
Chris@16
|
389 if(sv_parser.match(sitr, stream_end, mr)) {
|
Chris@16
|
390 return date_type(static_cast<special_values>(mr.current_match));
|
Chris@16
|
391 }
|
Chris@16
|
392 }
|
Chris@16
|
393 t_month = month_type(month);
|
Chris@16
|
394 break;
|
Chris@16
|
395 }
|
Chris@16
|
396 case 'Y':
|
Chris@16
|
397 {
|
Chris@16
|
398 match_results mr;
|
Chris@16
|
399 year = fixed_string_to_int<short, charT>(sitr, stream_end, mr, 4);
|
Chris@16
|
400 if(year == -1) {
|
Chris@16
|
401 if(sv_parser.match(sitr, stream_end, mr)) {
|
Chris@16
|
402 return date_type(static_cast<special_values>(mr.current_match));
|
Chris@16
|
403 }
|
Chris@16
|
404 }
|
Chris@16
|
405 t_year = year_type(year);
|
Chris@16
|
406 break;
|
Chris@16
|
407 }
|
Chris@16
|
408 case 'y':
|
Chris@16
|
409 {
|
Chris@16
|
410 match_results mr;
|
Chris@16
|
411 year = fixed_string_to_int<short, charT>(sitr, stream_end, mr, 2);
|
Chris@16
|
412 if(year == -1) {
|
Chris@16
|
413 if(sv_parser.match(sitr, stream_end, mr)) {
|
Chris@16
|
414 return date_type(static_cast<special_values>(mr.current_match));
|
Chris@16
|
415 }
|
Chris@16
|
416 }
|
Chris@16
|
417 year += 2000; //make 2 digit years in this century
|
Chris@16
|
418 t_year = year_type(year);
|
Chris@16
|
419 break;
|
Chris@16
|
420 }
|
Chris@16
|
421 default:
|
Chris@16
|
422 {} //ignore those we don't understand
|
Chris@16
|
423
|
Chris@16
|
424 }//switch
|
Chris@16
|
425
|
Chris@16
|
426 }
|
Chris@16
|
427 else { // itr == '%', second consecutive
|
Chris@16
|
428 sitr++;
|
Chris@16
|
429 }
|
Chris@16
|
430
|
Chris@16
|
431 itr++; //advance past format specifier
|
Chris@16
|
432 }
|
Chris@16
|
433 else { //skip past chars in format and in buffer
|
Chris@16
|
434 itr++;
|
Chris@16
|
435 if (use_current_char) {
|
Chris@16
|
436 use_current_char = false;
|
Chris@16
|
437 }
|
Chris@16
|
438 else {
|
Chris@16
|
439 sitr++;
|
Chris@16
|
440 }
|
Chris@16
|
441 }
|
Chris@16
|
442 }
|
Chris@16
|
443
|
Chris@16
|
444 if (day_of_year > 0) {
|
Chris@16
|
445 date_type d(static_cast<unsigned short>(year-1),12,31); //end of prior year
|
Chris@16
|
446 return d + duration_type(day_of_year);
|
Chris@16
|
447 }
|
Chris@16
|
448
|
Chris@16
|
449 return date_type(t_year, t_month, t_day); // exceptions were thrown earlier
|
Chris@16
|
450 // if input was no good
|
Chris@16
|
451 }
|
Chris@16
|
452
|
Chris@16
|
453 //! Throws bad_month if unable to parse
|
Chris@16
|
454 month_type
|
Chris@16
|
455 parse_month(std::istreambuf_iterator<charT>& sitr,
|
Chris@16
|
456 std::istreambuf_iterator<charT>& stream_end,
|
Chris@16
|
457 string_type format_str) const
|
Chris@16
|
458 {
|
Chris@16
|
459 match_results mr;
|
Chris@16
|
460 return parse_month(sitr, stream_end, format_str, mr);
|
Chris@16
|
461 }
|
Chris@16
|
462
|
Chris@16
|
463 //! Throws bad_month if unable to parse
|
Chris@16
|
464 month_type
|
Chris@16
|
465 parse_month(std::istreambuf_iterator<charT>& sitr,
|
Chris@16
|
466 std::istreambuf_iterator<charT>& stream_end,
|
Chris@16
|
467 string_type format_str,
|
Chris@16
|
468 match_results& mr) const
|
Chris@16
|
469 {
|
Chris@16
|
470 bool use_current_char = false;
|
Chris@16
|
471
|
Chris@16
|
472 // skip leading whitespace
|
Chris@16
|
473 while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
|
Chris@16
|
474
|
Chris@16
|
475 short month(0);
|
Chris@16
|
476
|
Chris@16
|
477 const_itr itr(format_str.begin());
|
Chris@16
|
478 while (itr != format_str.end() && (sitr != stream_end)) {
|
Chris@16
|
479 if (*itr == '%') {
|
Chris@16
|
480 if ( ++itr == format_str.end())
|
Chris@16
|
481 break;
|
Chris@16
|
482 if (*itr != '%') {
|
Chris@16
|
483 switch(*itr) {
|
Chris@16
|
484 case 'b':
|
Chris@16
|
485 {
|
Chris@16
|
486 mr = m_month_short_names.match(sitr, stream_end);
|
Chris@16
|
487 month = mr.current_match;
|
Chris@16
|
488 if (mr.has_remaining()) {
|
Chris@16
|
489 use_current_char = true;
|
Chris@16
|
490 }
|
Chris@16
|
491 break;
|
Chris@16
|
492 }
|
Chris@16
|
493 case 'B':
|
Chris@16
|
494 {
|
Chris@16
|
495 mr = m_month_long_names.match(sitr, stream_end);
|
Chris@16
|
496 month = mr.current_match;
|
Chris@16
|
497 if (mr.has_remaining()) {
|
Chris@16
|
498 use_current_char = true;
|
Chris@16
|
499 }
|
Chris@16
|
500 break;
|
Chris@16
|
501 }
|
Chris@16
|
502 case 'm':
|
Chris@16
|
503 {
|
Chris@16
|
504 month = var_string_to_int<short, charT>(sitr, stream_end, 2);
|
Chris@16
|
505 // var_string_to_int returns -1 if parse failed. That will
|
Chris@16
|
506 // cause a bad_month exception to be thrown so we do nothing here
|
Chris@16
|
507 break;
|
Chris@16
|
508 }
|
Chris@16
|
509 default:
|
Chris@16
|
510 {} //ignore those we don't understand
|
Chris@16
|
511
|
Chris@16
|
512 }//switch
|
Chris@16
|
513
|
Chris@16
|
514 }
|
Chris@16
|
515 else { // itr == '%', second consecutive
|
Chris@16
|
516 sitr++;
|
Chris@16
|
517 }
|
Chris@16
|
518
|
Chris@16
|
519 itr++; //advance past format specifier
|
Chris@16
|
520 }
|
Chris@16
|
521 else { //skip past chars in format and in buffer
|
Chris@16
|
522 itr++;
|
Chris@16
|
523 if (use_current_char) {
|
Chris@16
|
524 use_current_char = false;
|
Chris@16
|
525 }
|
Chris@16
|
526 else {
|
Chris@16
|
527 sitr++;
|
Chris@16
|
528 }
|
Chris@16
|
529 }
|
Chris@16
|
530 }
|
Chris@16
|
531
|
Chris@16
|
532 return month_type(month); // throws bad_month exception when values are zero
|
Chris@16
|
533 }
|
Chris@16
|
534
|
Chris@16
|
535 //! Expects 1 or 2 digits 1-31. Throws bad_day_of_month if unable to parse
|
Chris@16
|
536 day_type
|
Chris@16
|
537 parse_var_day_of_month(std::istreambuf_iterator<charT>& sitr,
|
Chris@16
|
538 std::istreambuf_iterator<charT>& stream_end) const
|
Chris@16
|
539 {
|
Chris@16
|
540 // skip leading whitespace
|
Chris@16
|
541 while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
|
Chris@16
|
542
|
Chris@16
|
543 return day_type(var_string_to_int<short, charT>(sitr, stream_end, 2));
|
Chris@16
|
544 }
|
Chris@16
|
545 //! Expects 2 digits 01-31. Throws bad_day_of_month if unable to parse
|
Chris@16
|
546 day_type
|
Chris@16
|
547 parse_day_of_month(std::istreambuf_iterator<charT>& sitr,
|
Chris@16
|
548 std::istreambuf_iterator<charT>& stream_end) const
|
Chris@16
|
549 {
|
Chris@16
|
550 // skip leading whitespace
|
Chris@16
|
551 while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
|
Chris@16
|
552
|
Chris@16
|
553 //return day_type(var_string_to_int<short, charT>(sitr, stream_end, 2));
|
Chris@16
|
554 match_results mr;
|
Chris@16
|
555 return day_type(fixed_string_to_int<short, charT>(sitr, stream_end, mr, 2));
|
Chris@16
|
556 }
|
Chris@16
|
557
|
Chris@16
|
558 day_of_week_type
|
Chris@16
|
559 parse_weekday(std::istreambuf_iterator<charT>& sitr,
|
Chris@16
|
560 std::istreambuf_iterator<charT>& stream_end,
|
Chris@16
|
561 string_type format_str) const
|
Chris@16
|
562 {
|
Chris@16
|
563 match_results mr;
|
Chris@16
|
564 return parse_weekday(sitr, stream_end, format_str, mr);
|
Chris@16
|
565 }
|
Chris@16
|
566 day_of_week_type
|
Chris@16
|
567 parse_weekday(std::istreambuf_iterator<charT>& sitr,
|
Chris@16
|
568 std::istreambuf_iterator<charT>& stream_end,
|
Chris@16
|
569 string_type format_str,
|
Chris@16
|
570 match_results& mr) const
|
Chris@16
|
571 {
|
Chris@16
|
572 bool use_current_char = false;
|
Chris@16
|
573
|
Chris@16
|
574 // skip leading whitespace
|
Chris@16
|
575 while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
|
Chris@16
|
576
|
Chris@16
|
577 short wkday(0);
|
Chris@16
|
578
|
Chris@16
|
579 const_itr itr(format_str.begin());
|
Chris@16
|
580 while (itr != format_str.end() && (sitr != stream_end)) {
|
Chris@16
|
581 if (*itr == '%') {
|
Chris@16
|
582 if ( ++itr == format_str.end())
|
Chris@16
|
583 break;
|
Chris@16
|
584 if (*itr != '%') {
|
Chris@16
|
585 switch(*itr) {
|
Chris@16
|
586 case 'a':
|
Chris@16
|
587 {
|
Chris@16
|
588 //this value is just throw away. It could be used for
|
Chris@16
|
589 //error checking potentially, but it isn't helpful in
|
Chris@16
|
590 //actually constructing the date - we just need to get it
|
Chris@16
|
591 //out of the stream
|
Chris@16
|
592 mr = m_weekday_short_names.match(sitr, stream_end);
|
Chris@16
|
593 wkday = mr.current_match;
|
Chris@16
|
594 if (mr.has_remaining()) {
|
Chris@16
|
595 use_current_char = true;
|
Chris@16
|
596 }
|
Chris@16
|
597 break;
|
Chris@16
|
598 }
|
Chris@16
|
599 case 'A':
|
Chris@16
|
600 {
|
Chris@16
|
601 //this value is just throw away. It could be used for
|
Chris@16
|
602 //error checking potentially, but it isn't helpful in
|
Chris@16
|
603 //actually constructing the date - we just need to get it
|
Chris@16
|
604 //out of the stream
|
Chris@16
|
605 mr = m_weekday_long_names.match(sitr, stream_end);
|
Chris@16
|
606 wkday = mr.current_match;
|
Chris@16
|
607 if (mr.has_remaining()) {
|
Chris@16
|
608 use_current_char = true;
|
Chris@16
|
609 }
|
Chris@16
|
610 break;
|
Chris@16
|
611 }
|
Chris@16
|
612 case 'w':
|
Chris@16
|
613 {
|
Chris@16
|
614 // weekday as number 0-6, Sunday == 0
|
Chris@16
|
615 wkday = var_string_to_int<short, charT>(sitr, stream_end, 2);
|
Chris@16
|
616 break;
|
Chris@16
|
617 }
|
Chris@16
|
618 default:
|
Chris@16
|
619 {} //ignore those we don't understand
|
Chris@16
|
620
|
Chris@16
|
621 }//switch
|
Chris@16
|
622
|
Chris@16
|
623 }
|
Chris@16
|
624 else { // itr == '%', second consecutive
|
Chris@16
|
625 sitr++;
|
Chris@16
|
626 }
|
Chris@16
|
627
|
Chris@16
|
628 itr++; //advance past format specifier
|
Chris@16
|
629 }
|
Chris@16
|
630 else { //skip past chars in format and in buffer
|
Chris@16
|
631 itr++;
|
Chris@16
|
632 if (use_current_char) {
|
Chris@16
|
633 use_current_char = false;
|
Chris@16
|
634 }
|
Chris@16
|
635 else {
|
Chris@16
|
636 sitr++;
|
Chris@16
|
637 }
|
Chris@16
|
638 }
|
Chris@16
|
639 }
|
Chris@16
|
640
|
Chris@16
|
641 return day_of_week_type(wkday); // throws bad_day_of_month exception
|
Chris@16
|
642 // when values are zero
|
Chris@16
|
643 }
|
Chris@16
|
644
|
Chris@16
|
645 //! throws bad_year if unable to parse
|
Chris@16
|
646 year_type
|
Chris@16
|
647 parse_year(std::istreambuf_iterator<charT>& sitr,
|
Chris@16
|
648 std::istreambuf_iterator<charT>& stream_end,
|
Chris@16
|
649 string_type format_str) const
|
Chris@16
|
650 {
|
Chris@16
|
651 match_results mr;
|
Chris@16
|
652 return parse_year(sitr, stream_end, format_str, mr);
|
Chris@16
|
653 }
|
Chris@16
|
654
|
Chris@16
|
655 //! throws bad_year if unable to parse
|
Chris@16
|
656 year_type
|
Chris@16
|
657 parse_year(std::istreambuf_iterator<charT>& sitr,
|
Chris@16
|
658 std::istreambuf_iterator<charT>& stream_end,
|
Chris@16
|
659 string_type format_str,
|
Chris@16
|
660 match_results& mr) const
|
Chris@16
|
661 {
|
Chris@16
|
662 bool use_current_char = false;
|
Chris@16
|
663
|
Chris@16
|
664 // skip leading whitespace
|
Chris@16
|
665 while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
|
Chris@16
|
666
|
Chris@16
|
667 unsigned short year(0);
|
Chris@16
|
668
|
Chris@16
|
669 const_itr itr(format_str.begin());
|
Chris@16
|
670 while (itr != format_str.end() && (sitr != stream_end)) {
|
Chris@16
|
671 if (*itr == '%') {
|
Chris@16
|
672 if ( ++itr == format_str.end())
|
Chris@16
|
673 break;
|
Chris@16
|
674 if (*itr != '%') {
|
Chris@16
|
675 //match_results mr;
|
Chris@16
|
676 switch(*itr) {
|
Chris@16
|
677 case 'Y':
|
Chris@16
|
678 {
|
Chris@16
|
679 // year from 4 digit string
|
Chris@16
|
680 year = fixed_string_to_int<short, charT>(sitr, stream_end, mr, 4);
|
Chris@16
|
681 break;
|
Chris@16
|
682 }
|
Chris@16
|
683 case 'y':
|
Chris@16
|
684 {
|
Chris@16
|
685 // year from 2 digit string (no century)
|
Chris@16
|
686 year = fixed_string_to_int<short, charT>(sitr, stream_end, mr, 2);
|
Chris@16
|
687 year += 2000; //make 2 digit years in this century
|
Chris@16
|
688 break;
|
Chris@16
|
689 }
|
Chris@16
|
690 default:
|
Chris@16
|
691 {} //ignore those we don't understand
|
Chris@16
|
692
|
Chris@16
|
693 }//switch
|
Chris@16
|
694
|
Chris@16
|
695 }
|
Chris@16
|
696 else { // itr == '%', second consecutive
|
Chris@16
|
697 sitr++;
|
Chris@16
|
698 }
|
Chris@16
|
699
|
Chris@16
|
700 itr++; //advance past format specifier
|
Chris@16
|
701 }
|
Chris@16
|
702 else { //skip past chars in format and in buffer
|
Chris@16
|
703 itr++;
|
Chris@16
|
704 if (use_current_char) {
|
Chris@16
|
705 use_current_char = false;
|
Chris@16
|
706 }
|
Chris@16
|
707 else {
|
Chris@16
|
708 sitr++;
|
Chris@16
|
709 }
|
Chris@16
|
710 }
|
Chris@16
|
711 }
|
Chris@16
|
712
|
Chris@16
|
713 return year_type(year); // throws bad_year exception when values are zero
|
Chris@16
|
714 }
|
Chris@16
|
715
|
Chris@16
|
716
|
Chris@16
|
717 private:
|
Chris@16
|
718 string_type m_format;
|
Chris@16
|
719 parse_tree_type m_month_short_names;
|
Chris@16
|
720 parse_tree_type m_month_long_names;
|
Chris@16
|
721 parse_tree_type m_weekday_short_names;
|
Chris@16
|
722 parse_tree_type m_weekday_long_names;
|
Chris@16
|
723
|
Chris@16
|
724 };
|
Chris@16
|
725
|
Chris@16
|
726 } } //namespace
|
Chris@16
|
727
|
Chris@16
|
728 #endif
|
Chris@16
|
729
|
Chris@16
|
730
|
Chris@16
|
731
|