Chris@16
|
1 #ifndef _DATE_TIME_DATE_PARSING_HPP___
|
Chris@16
|
2 #define _DATE_TIME_DATE_PARSING_HPP___
|
Chris@16
|
3
|
Chris@16
|
4 /* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc.
|
Chris@16
|
5 * Use, modification and distribution is subject to the
|
Chris@16
|
6 * Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
7 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 * Author: Jeff Garland, Bart Garst
|
Chris@101
|
9 * $Date$
|
Chris@16
|
10 */
|
Chris@16
|
11
|
Chris@16
|
12 #include <string>
|
Chris@16
|
13 #include <iterator>
|
Chris@16
|
14 #include <algorithm>
|
Chris@16
|
15 #include <boost/tokenizer.hpp>
|
Chris@16
|
16 #include <boost/lexical_cast.hpp>
|
Chris@16
|
17 #include <boost/date_time/compiler_config.hpp>
|
Chris@16
|
18 #include <boost/date_time/parse_format_base.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 #if defined(BOOST_DATE_TIME_NO_LOCALE)
|
Chris@16
|
21 #include <cctype> // ::tolower(int)
|
Chris@16
|
22 #else
|
Chris@16
|
23 #include <locale> // std::tolower(char, locale)
|
Chris@16
|
24 #endif
|
Chris@16
|
25
|
Chris@16
|
26 namespace boost {
|
Chris@16
|
27 namespace date_time {
|
Chris@16
|
28
|
Chris@16
|
29 //! A function to replace the std::transform( , , ,tolower) construct
|
Chris@16
|
30 /*! This function simply takes a string, and changes all the characters
|
Chris@16
|
31 * in that string to lowercase (according to the default system locale).
|
Chris@16
|
32 * In the event that a compiler does not support locales, the old
|
Chris@16
|
33 * C style tolower() is used.
|
Chris@16
|
34 */
|
Chris@16
|
35 inline
|
Chris@16
|
36 std::string
|
Chris@16
|
37 convert_to_lower(std::string inp)
|
Chris@16
|
38 {
|
Chris@16
|
39 #if !defined(BOOST_DATE_TIME_NO_LOCALE)
|
Chris@16
|
40 const std::locale loc(std::locale::classic());
|
Chris@16
|
41 #endif
|
Chris@16
|
42 std::string::size_type i = 0, n = inp.length();
|
Chris@16
|
43 for (; i < n; ++i) {
|
Chris@16
|
44 inp[i] =
|
Chris@16
|
45 #if defined(BOOST_DATE_TIME_NO_LOCALE)
|
Chris@16
|
46 static_cast<char>(std::tolower(inp[i]));
|
Chris@16
|
47 #else
|
Chris@16
|
48 // tolower and others were brought in to std for borland >= v564
|
Chris@16
|
49 // in compiler_config.hpp
|
Chris@16
|
50 std::tolower(inp[i], loc);
|
Chris@16
|
51 #endif
|
Chris@16
|
52 }
|
Chris@16
|
53 return inp;
|
Chris@16
|
54 }
|
Chris@16
|
55
|
Chris@16
|
56 //! Helper function for parse_date.
|
Chris@16
|
57 /* Used by-value parameter because we change the string and may
|
Chris@16
|
58 * want to preserve the original argument */
|
Chris@16
|
59 template<class month_type>
|
Chris@16
|
60 inline unsigned short
|
Chris@16
|
61 month_str_to_ushort(std::string const& s) {
|
Chris@16
|
62 if((s.at(0) >= '0') && (s.at(0) <= '9')) {
|
Chris@16
|
63 return boost::lexical_cast<unsigned short>(s);
|
Chris@16
|
64 }
|
Chris@16
|
65 else {
|
Chris@16
|
66 std::string str = convert_to_lower(s);
|
Chris@16
|
67 typename month_type::month_map_ptr_type ptr = month_type::get_month_map_ptr();
|
Chris@16
|
68 typename month_type::month_map_type::iterator iter = ptr->find(str);
|
Chris@16
|
69 if(iter != ptr->end()) { // required for STLport
|
Chris@16
|
70 return iter->second;
|
Chris@16
|
71 }
|
Chris@16
|
72 }
|
Chris@16
|
73 return 13; // intentionally out of range - name not found
|
Chris@16
|
74 }
|
Chris@16
|
75
|
Chris@16
|
76 //! Find index of a string in either of 2 arrays
|
Chris@16
|
77 /*! find_match searches both arrays for a match to 's'. Both arrays
|
Chris@16
|
78 * must contain 'size' elements. The index of the match is returned.
|
Chris@16
|
79 * If no match is found, 'size' is returned.
|
Chris@16
|
80 * Ex. "Jan" returns 0, "Dec" returns 11, "Tue" returns 2.
|
Chris@16
|
81 * 'size' can be sent in with: (greg_month::max)() (which 12),
|
Chris@16
|
82 * (greg_weekday::max)() + 1 (which is 7) or date_time::NumSpecialValues */
|
Chris@16
|
83 template<class charT>
|
Chris@16
|
84 short find_match(const charT* const* short_names,
|
Chris@16
|
85 const charT* const* long_names,
|
Chris@16
|
86 short size,
|
Chris@16
|
87 const std::basic_string<charT>& s) {
|
Chris@16
|
88 for(short i = 0; i < size; ++i){
|
Chris@16
|
89 if(short_names[i] == s || long_names[i] == s){
|
Chris@16
|
90 return i;
|
Chris@16
|
91 }
|
Chris@16
|
92 }
|
Chris@16
|
93 return size; // not-found, return a value out of range
|
Chris@16
|
94 }
|
Chris@16
|
95
|
Chris@16
|
96 //! Generic function to parse a delimited date (eg: 2002-02-10)
|
Chris@16
|
97 /*! Accepted formats are: "2003-02-10" or " 2003-Feb-10" or
|
Chris@16
|
98 * "2003-Feburary-10"
|
Chris@16
|
99 * The order in which the Month, Day, & Year appear in the argument
|
Chris@16
|
100 * string can be accomodated by passing in the appropriate ymd_order_spec
|
Chris@16
|
101 */
|
Chris@16
|
102 template<class date_type>
|
Chris@16
|
103 date_type
|
Chris@16
|
104 parse_date(const std::string& s, int order_spec = ymd_order_iso) {
|
Chris@16
|
105 std::string spec_str;
|
Chris@16
|
106 if(order_spec == ymd_order_iso) {
|
Chris@16
|
107 spec_str = "ymd";
|
Chris@16
|
108 }
|
Chris@16
|
109 else if(order_spec == ymd_order_dmy) {
|
Chris@16
|
110 spec_str = "dmy";
|
Chris@16
|
111 }
|
Chris@16
|
112 else { // (order_spec == ymd_order_us)
|
Chris@16
|
113 spec_str = "mdy";
|
Chris@16
|
114 }
|
Chris@16
|
115
|
Chris@16
|
116 typedef typename date_type::month_type month_type;
|
Chris@16
|
117 unsigned pos = 0;
|
Chris@16
|
118 unsigned short year(0), month(0), day(0);
|
Chris@16
|
119 typedef typename std::basic_string<char>::traits_type traits_type;
|
Chris@16
|
120 typedef boost::char_separator<char, traits_type> char_separator_type;
|
Chris@16
|
121 typedef boost::tokenizer<char_separator_type,
|
Chris@16
|
122 std::basic_string<char>::const_iterator,
|
Chris@16
|
123 std::basic_string<char> > tokenizer;
|
Chris@16
|
124 typedef boost::tokenizer<char_separator_type,
|
Chris@16
|
125 std::basic_string<char>::const_iterator,
|
Chris@16
|
126 std::basic_string<char> >::iterator tokenizer_iterator;
|
Chris@16
|
127 // may need more delimiters, these work for the regression tests
|
Chris@16
|
128 const char sep_char[] = {',','-','.',' ','/','\0'};
|
Chris@16
|
129 char_separator_type sep(sep_char);
|
Chris@16
|
130 tokenizer tok(s,sep);
|
Chris@16
|
131 for(tokenizer_iterator beg=tok.begin();
|
Chris@16
|
132 beg!=tok.end() && pos < spec_str.size();
|
Chris@16
|
133 ++beg, ++pos) {
|
Chris@16
|
134 switch(spec_str.at(pos)) {
|
Chris@16
|
135 case 'y':
|
Chris@16
|
136 {
|
Chris@16
|
137 year = boost::lexical_cast<unsigned short>(*beg);
|
Chris@16
|
138 break;
|
Chris@16
|
139 }
|
Chris@16
|
140 case 'm':
|
Chris@16
|
141 {
|
Chris@16
|
142 month = month_str_to_ushort<month_type>(*beg);
|
Chris@16
|
143 break;
|
Chris@16
|
144 }
|
Chris@16
|
145 case 'd':
|
Chris@16
|
146 {
|
Chris@16
|
147 day = boost::lexical_cast<unsigned short>(*beg);
|
Chris@16
|
148 break;
|
Chris@16
|
149 }
|
Chris@16
|
150 default: break;
|
Chris@16
|
151 } //switch
|
Chris@16
|
152 }
|
Chris@16
|
153 return date_type(year, month, day);
|
Chris@16
|
154 }
|
Chris@16
|
155
|
Chris@16
|
156 //! Generic function to parse undelimited date (eg: 20020201)
|
Chris@16
|
157 template<class date_type>
|
Chris@16
|
158 date_type
|
Chris@16
|
159 parse_undelimited_date(const std::string& s) {
|
Chris@16
|
160 int offsets[] = {4,2,2};
|
Chris@16
|
161 int pos = 0;
|
Chris@16
|
162 //typename date_type::ymd_type ymd((year_type::min)(),1,1);
|
Chris@16
|
163 unsigned short y = 0, m = 0, d = 0;
|
Chris@16
|
164
|
Chris@16
|
165 /* The two bool arguments state that parsing will not wrap
|
Chris@16
|
166 * (only the first 8 characters will be parsed) and partial
|
Chris@16
|
167 * strings will not be parsed.
|
Chris@16
|
168 * Ex:
|
Chris@16
|
169 * "2005121" will parse 2005 & 12, but not the "1" */
|
Chris@16
|
170 boost::offset_separator osf(offsets, offsets+3, false, false);
|
Chris@16
|
171
|
Chris@16
|
172 typedef typename boost::tokenizer<boost::offset_separator,
|
Chris@16
|
173 std::basic_string<char>::const_iterator,
|
Chris@16
|
174 std::basic_string<char> > tokenizer_type;
|
Chris@16
|
175 tokenizer_type tok(s, osf);
|
Chris@16
|
176 for(typename tokenizer_type::iterator ti=tok.begin(); ti!=tok.end();++ti) {
|
Chris@16
|
177 unsigned short i = boost::lexical_cast<unsigned short>(*ti);
|
Chris@16
|
178 switch(pos) {
|
Chris@16
|
179 case 0: y = i; break;
|
Chris@16
|
180 case 1: m = i; break;
|
Chris@16
|
181 case 2: d = i; break;
|
Chris@16
|
182 default: break;
|
Chris@16
|
183 }
|
Chris@16
|
184 pos++;
|
Chris@16
|
185 }
|
Chris@16
|
186 return date_type(y,m,d);
|
Chris@16
|
187 }
|
Chris@16
|
188
|
Chris@16
|
189 //! Helper function for 'date gregorian::from_stream()'
|
Chris@16
|
190 /*! Creates a string from the iterators that reference the
|
Chris@16
|
191 * begining & end of a char[] or string. All elements are
|
Chris@16
|
192 * used in output string */
|
Chris@16
|
193 template<class date_type, class iterator_type>
|
Chris@16
|
194 inline
|
Chris@16
|
195 date_type
|
Chris@16
|
196 from_stream_type(iterator_type& beg,
|
Chris@16
|
197 iterator_type const& end,
|
Chris@16
|
198 char)
|
Chris@16
|
199 {
|
Chris@16
|
200 std::ostringstream ss;
|
Chris@16
|
201 while(beg != end) {
|
Chris@16
|
202 ss << *beg++;
|
Chris@16
|
203 }
|
Chris@16
|
204 return parse_date<date_type>(ss.str());
|
Chris@16
|
205 }
|
Chris@16
|
206
|
Chris@16
|
207 //! Helper function for 'date gregorian::from_stream()'
|
Chris@16
|
208 /*! Returns the first string found in the stream referenced by the
|
Chris@16
|
209 * begining & end iterators */
|
Chris@16
|
210 template<class date_type, class iterator_type>
|
Chris@16
|
211 inline
|
Chris@16
|
212 date_type
|
Chris@16
|
213 from_stream_type(iterator_type& beg,
|
Chris@16
|
214 iterator_type const& /* end */,
|
Chris@16
|
215 std::string const&)
|
Chris@16
|
216 {
|
Chris@16
|
217 return parse_date<date_type>(*beg);
|
Chris@16
|
218 }
|
Chris@16
|
219
|
Chris@16
|
220 /* I believe the wchar stuff would be best elsewhere, perhaps in
|
Chris@16
|
221 * parse_date<>()? In the mean time this gets us started... */
|
Chris@16
|
222 //! Helper function for 'date gregorian::from_stream()'
|
Chris@16
|
223 /*! Creates a string from the iterators that reference the
|
Chris@16
|
224 * begining & end of a wstring. All elements are
|
Chris@16
|
225 * used in output string */
|
Chris@16
|
226 template<class date_type, class iterator_type>
|
Chris@16
|
227 inline
|
Chris@16
|
228 date_type from_stream_type(iterator_type& beg,
|
Chris@16
|
229 iterator_type const& end,
|
Chris@16
|
230 wchar_t)
|
Chris@16
|
231 {
|
Chris@16
|
232 std::ostringstream ss;
|
Chris@16
|
233 #if !defined(BOOST_DATE_TIME_NO_LOCALE)
|
Chris@16
|
234 std::locale loc;
|
Chris@16
|
235 std::ctype<wchar_t> const& fac = std::use_facet<std::ctype<wchar_t> >(loc);
|
Chris@16
|
236 while(beg != end) {
|
Chris@16
|
237 ss << fac.narrow(*beg++, 'X'); // 'X' will cause exception to be thrown
|
Chris@16
|
238 }
|
Chris@16
|
239 #else
|
Chris@16
|
240 while(beg != end) {
|
Chris@16
|
241 char c = 'X'; // 'X' will cause exception to be thrown
|
Chris@16
|
242 const wchar_t wc = *beg++;
|
Chris@16
|
243 if (wc >= 0 && wc <= 127)
|
Chris@16
|
244 c = static_cast< char >(wc);
|
Chris@16
|
245 ss << c;
|
Chris@16
|
246 }
|
Chris@16
|
247 #endif
|
Chris@16
|
248 return parse_date<date_type>(ss.str());
|
Chris@16
|
249 }
|
Chris@16
|
250 #ifndef BOOST_NO_STD_WSTRING
|
Chris@16
|
251 //! Helper function for 'date gregorian::from_stream()'
|
Chris@16
|
252 /*! Creates a string from the first wstring found in the stream
|
Chris@16
|
253 * referenced by the begining & end iterators */
|
Chris@16
|
254 template<class date_type, class iterator_type>
|
Chris@16
|
255 inline
|
Chris@16
|
256 date_type
|
Chris@16
|
257 from_stream_type(iterator_type& beg,
|
Chris@16
|
258 iterator_type const& /* end */,
|
Chris@16
|
259 std::wstring const&) {
|
Chris@16
|
260 std::wstring ws = *beg;
|
Chris@16
|
261 std::ostringstream ss;
|
Chris@16
|
262 std::wstring::iterator wsb = ws.begin(), wse = ws.end();
|
Chris@16
|
263 #if !defined(BOOST_DATE_TIME_NO_LOCALE)
|
Chris@16
|
264 std::locale loc;
|
Chris@16
|
265 std::ctype<wchar_t> const& fac = std::use_facet<std::ctype<wchar_t> >(loc);
|
Chris@16
|
266 while(wsb != wse) {
|
Chris@16
|
267 ss << fac.narrow(*wsb++, 'X'); // 'X' will cause exception to be thrown
|
Chris@16
|
268 }
|
Chris@16
|
269 #else
|
Chris@16
|
270 while(wsb != wse) {
|
Chris@16
|
271 char c = 'X'; // 'X' will cause exception to be thrown
|
Chris@16
|
272 const wchar_t wc = *wsb++;
|
Chris@16
|
273 if (wc >= 0 && wc <= 127)
|
Chris@16
|
274 c = static_cast< char >(wc);
|
Chris@16
|
275 ss << c;
|
Chris@16
|
276 }
|
Chris@16
|
277 #endif
|
Chris@16
|
278 return parse_date<date_type>(ss.str());
|
Chris@16
|
279 }
|
Chris@16
|
280 #endif // BOOST_NO_STD_WSTRING
|
Chris@16
|
281 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
|
Chris@16
|
282 // This function cannot be compiled with MSVC 6.0 due to internal compiler shorcomings
|
Chris@16
|
283 #else
|
Chris@16
|
284 //! function called by wrapper functions: date_period_from_(w)string()
|
Chris@16
|
285 template<class date_type, class charT>
|
Chris@16
|
286 period<date_type, typename date_type::duration_type>
|
Chris@16
|
287 from_simple_string_type(const std::basic_string<charT>& s){
|
Chris@16
|
288 typedef typename std::basic_string<charT>::traits_type traits_type;
|
Chris@16
|
289 typedef typename boost::char_separator<charT, traits_type> char_separator;
|
Chris@16
|
290 typedef typename boost::tokenizer<char_separator,
|
Chris@16
|
291 typename std::basic_string<charT>::const_iterator,
|
Chris@16
|
292 std::basic_string<charT> > tokenizer;
|
Chris@16
|
293 const charT sep_list[4] = {'[','/',']','\0'};
|
Chris@16
|
294 char_separator sep(sep_list);
|
Chris@16
|
295 tokenizer tokens(s, sep);
|
Chris@16
|
296 typename tokenizer::iterator tok_it = tokens.begin();
|
Chris@16
|
297 std::basic_string<charT> date_string = *tok_it;
|
Chris@16
|
298 // get 2 string iterators and generate a date from them
|
Chris@16
|
299 typename std::basic_string<charT>::iterator date_string_start = date_string.begin(),
|
Chris@16
|
300 date_string_end = date_string.end();
|
Chris@16
|
301 typedef typename std::iterator_traits<typename std::basic_string<charT>::iterator>::value_type value_type;
|
Chris@16
|
302 date_type d1 = from_stream_type<date_type>(date_string_start, date_string_end, value_type());
|
Chris@16
|
303 date_string = *(++tok_it); // next token
|
Chris@16
|
304 date_string_start = date_string.begin(), date_string_end = date_string.end();
|
Chris@16
|
305 date_type d2 = from_stream_type<date_type>(date_string_start, date_string_end, value_type());
|
Chris@16
|
306 return period<date_type, typename date_type::duration_type>(d1, d2);
|
Chris@16
|
307 }
|
Chris@16
|
308 #endif
|
Chris@16
|
309
|
Chris@16
|
310 } } //namespace date_time
|
Chris@16
|
311
|
Chris@16
|
312
|
Chris@16
|
313
|
Chris@16
|
314
|
Chris@16
|
315 #endif
|
Chris@16
|
316
|