annotate DEPENDENCIES/generic/include/boost/date_time/date_parsing.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
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@16 9 * $Date: 2012-09-30 16:25:22 -0700 (Sun, 30 Sep 2012) $
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::year_type year_type;
Chris@16 117 typedef typename date_type::month_type month_type;
Chris@16 118 unsigned pos = 0;
Chris@16 119 unsigned short year(0), month(0), day(0);
Chris@16 120 typedef typename std::basic_string<char>::traits_type traits_type;
Chris@16 121 typedef boost::char_separator<char, traits_type> char_separator_type;
Chris@16 122 typedef boost::tokenizer<char_separator_type,
Chris@16 123 std::basic_string<char>::const_iterator,
Chris@16 124 std::basic_string<char> > tokenizer;
Chris@16 125 typedef boost::tokenizer<char_separator_type,
Chris@16 126 std::basic_string<char>::const_iterator,
Chris@16 127 std::basic_string<char> >::iterator tokenizer_iterator;
Chris@16 128 // may need more delimiters, these work for the regression tests
Chris@16 129 const char sep_char[] = {',','-','.',' ','/','\0'};
Chris@16 130 char_separator_type sep(sep_char);
Chris@16 131 tokenizer tok(s,sep);
Chris@16 132 for(tokenizer_iterator beg=tok.begin();
Chris@16 133 beg!=tok.end() && pos < spec_str.size();
Chris@16 134 ++beg, ++pos) {
Chris@16 135 switch(spec_str.at(pos)) {
Chris@16 136 case 'y':
Chris@16 137 {
Chris@16 138 year = boost::lexical_cast<unsigned short>(*beg);
Chris@16 139 break;
Chris@16 140 }
Chris@16 141 case 'm':
Chris@16 142 {
Chris@16 143 month = month_str_to_ushort<month_type>(*beg);
Chris@16 144 break;
Chris@16 145 }
Chris@16 146 case 'd':
Chris@16 147 {
Chris@16 148 day = boost::lexical_cast<unsigned short>(*beg);
Chris@16 149 break;
Chris@16 150 }
Chris@16 151 default: break;
Chris@16 152 } //switch
Chris@16 153 }
Chris@16 154 return date_type(year, month, day);
Chris@16 155 }
Chris@16 156
Chris@16 157 //! Generic function to parse undelimited date (eg: 20020201)
Chris@16 158 template<class date_type>
Chris@16 159 date_type
Chris@16 160 parse_undelimited_date(const std::string& s) {
Chris@16 161 int offsets[] = {4,2,2};
Chris@16 162 int pos = 0;
Chris@16 163 typedef typename date_type::year_type year_type;
Chris@16 164 //typename date_type::ymd_type ymd((year_type::min)(),1,1);
Chris@16 165 unsigned short y = 0, m = 0, d = 0;
Chris@16 166
Chris@16 167 /* The two bool arguments state that parsing will not wrap
Chris@16 168 * (only the first 8 characters will be parsed) and partial
Chris@16 169 * strings will not be parsed.
Chris@16 170 * Ex:
Chris@16 171 * "2005121" will parse 2005 & 12, but not the "1" */
Chris@16 172 boost::offset_separator osf(offsets, offsets+3, false, false);
Chris@16 173
Chris@16 174 typedef typename boost::tokenizer<boost::offset_separator,
Chris@16 175 std::basic_string<char>::const_iterator,
Chris@16 176 std::basic_string<char> > tokenizer_type;
Chris@16 177 tokenizer_type tok(s, osf);
Chris@16 178 for(typename tokenizer_type::iterator ti=tok.begin(); ti!=tok.end();++ti) {
Chris@16 179 unsigned short i = boost::lexical_cast<unsigned short>(*ti);
Chris@16 180 switch(pos) {
Chris@16 181 case 0: y = i; break;
Chris@16 182 case 1: m = i; break;
Chris@16 183 case 2: d = i; break;
Chris@16 184 default: break;
Chris@16 185 }
Chris@16 186 pos++;
Chris@16 187 }
Chris@16 188 return date_type(y,m,d);
Chris@16 189 }
Chris@16 190
Chris@16 191 //! Helper function for 'date gregorian::from_stream()'
Chris@16 192 /*! Creates a string from the iterators that reference the
Chris@16 193 * begining & end of a char[] or string. All elements are
Chris@16 194 * used in output string */
Chris@16 195 template<class date_type, class iterator_type>
Chris@16 196 inline
Chris@16 197 date_type
Chris@16 198 from_stream_type(iterator_type& beg,
Chris@16 199 iterator_type const& end,
Chris@16 200 char)
Chris@16 201 {
Chris@16 202 std::ostringstream ss;
Chris@16 203 while(beg != end) {
Chris@16 204 ss << *beg++;
Chris@16 205 }
Chris@16 206 return parse_date<date_type>(ss.str());
Chris@16 207 }
Chris@16 208
Chris@16 209 //! Helper function for 'date gregorian::from_stream()'
Chris@16 210 /*! Returns the first string found in the stream referenced by the
Chris@16 211 * begining & end iterators */
Chris@16 212 template<class date_type, class iterator_type>
Chris@16 213 inline
Chris@16 214 date_type
Chris@16 215 from_stream_type(iterator_type& beg,
Chris@16 216 iterator_type const& /* end */,
Chris@16 217 std::string const&)
Chris@16 218 {
Chris@16 219 return parse_date<date_type>(*beg);
Chris@16 220 }
Chris@16 221
Chris@16 222 /* I believe the wchar stuff would be best elsewhere, perhaps in
Chris@16 223 * parse_date<>()? In the mean time this gets us started... */
Chris@16 224 //! Helper function for 'date gregorian::from_stream()'
Chris@16 225 /*! Creates a string from the iterators that reference the
Chris@16 226 * begining & end of a wstring. All elements are
Chris@16 227 * used in output string */
Chris@16 228 template<class date_type, class iterator_type>
Chris@16 229 inline
Chris@16 230 date_type from_stream_type(iterator_type& beg,
Chris@16 231 iterator_type const& end,
Chris@16 232 wchar_t)
Chris@16 233 {
Chris@16 234 std::ostringstream ss;
Chris@16 235 #if !defined(BOOST_DATE_TIME_NO_LOCALE)
Chris@16 236 std::locale loc;
Chris@16 237 std::ctype<wchar_t> const& fac = std::use_facet<std::ctype<wchar_t> >(loc);
Chris@16 238 while(beg != end) {
Chris@16 239 ss << fac.narrow(*beg++, 'X'); // 'X' will cause exception to be thrown
Chris@16 240 }
Chris@16 241 #else
Chris@16 242 while(beg != end) {
Chris@16 243 char c = 'X'; // 'X' will cause exception to be thrown
Chris@16 244 const wchar_t wc = *beg++;
Chris@16 245 if (wc >= 0 && wc <= 127)
Chris@16 246 c = static_cast< char >(wc);
Chris@16 247 ss << c;
Chris@16 248 }
Chris@16 249 #endif
Chris@16 250 return parse_date<date_type>(ss.str());
Chris@16 251 }
Chris@16 252 #ifndef BOOST_NO_STD_WSTRING
Chris@16 253 //! Helper function for 'date gregorian::from_stream()'
Chris@16 254 /*! Creates a string from the first wstring found in the stream
Chris@16 255 * referenced by the begining & end iterators */
Chris@16 256 template<class date_type, class iterator_type>
Chris@16 257 inline
Chris@16 258 date_type
Chris@16 259 from_stream_type(iterator_type& beg,
Chris@16 260 iterator_type const& /* end */,
Chris@16 261 std::wstring const&) {
Chris@16 262 std::wstring ws = *beg;
Chris@16 263 std::ostringstream ss;
Chris@16 264 std::wstring::iterator wsb = ws.begin(), wse = ws.end();
Chris@16 265 #if !defined(BOOST_DATE_TIME_NO_LOCALE)
Chris@16 266 std::locale loc;
Chris@16 267 std::ctype<wchar_t> const& fac = std::use_facet<std::ctype<wchar_t> >(loc);
Chris@16 268 while(wsb != wse) {
Chris@16 269 ss << fac.narrow(*wsb++, 'X'); // 'X' will cause exception to be thrown
Chris@16 270 }
Chris@16 271 #else
Chris@16 272 while(wsb != wse) {
Chris@16 273 char c = 'X'; // 'X' will cause exception to be thrown
Chris@16 274 const wchar_t wc = *wsb++;
Chris@16 275 if (wc >= 0 && wc <= 127)
Chris@16 276 c = static_cast< char >(wc);
Chris@16 277 ss << c;
Chris@16 278 }
Chris@16 279 #endif
Chris@16 280 return parse_date<date_type>(ss.str());
Chris@16 281 }
Chris@16 282 #endif // BOOST_NO_STD_WSTRING
Chris@16 283 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
Chris@16 284 // This function cannot be compiled with MSVC 6.0 due to internal compiler shorcomings
Chris@16 285 #else
Chris@16 286 //! function called by wrapper functions: date_period_from_(w)string()
Chris@16 287 template<class date_type, class charT>
Chris@16 288 period<date_type, typename date_type::duration_type>
Chris@16 289 from_simple_string_type(const std::basic_string<charT>& s){
Chris@16 290 typedef typename std::basic_string<charT>::traits_type traits_type;
Chris@16 291 typedef typename boost::char_separator<charT, traits_type> char_separator;
Chris@16 292 typedef typename boost::tokenizer<char_separator,
Chris@16 293 typename std::basic_string<charT>::const_iterator,
Chris@16 294 std::basic_string<charT> > tokenizer;
Chris@16 295 const charT sep_list[4] = {'[','/',']','\0'};
Chris@16 296 char_separator sep(sep_list);
Chris@16 297 tokenizer tokens(s, sep);
Chris@16 298 typename tokenizer::iterator tok_it = tokens.begin();
Chris@16 299 std::basic_string<charT> date_string = *tok_it;
Chris@16 300 // get 2 string iterators and generate a date from them
Chris@16 301 typename std::basic_string<charT>::iterator date_string_start = date_string.begin(),
Chris@16 302 date_string_end = date_string.end();
Chris@16 303 typedef typename std::iterator_traits<typename std::basic_string<charT>::iterator>::value_type value_type;
Chris@16 304 date_type d1 = from_stream_type<date_type>(date_string_start, date_string_end, value_type());
Chris@16 305 date_string = *(++tok_it); // next token
Chris@16 306 date_string_start = date_string.begin(), date_string_end = date_string.end();
Chris@16 307 date_type d2 = from_stream_type<date_type>(date_string_start, date_string_end, value_type());
Chris@16 308 return period<date_type, typename date_type::duration_type>(d1, d2);
Chris@16 309 }
Chris@16 310 #endif
Chris@16 311
Chris@16 312 } } //namespace date_time
Chris@16 313
Chris@16 314
Chris@16 315
Chris@16 316
Chris@16 317 #endif
Chris@16 318