File indexing completed on 2025-01-18 09:30:38
0001 #ifndef _DATE_TIME_DATE_PARSING_HPP___
0002 #define _DATE_TIME_DATE_PARSING_HPP___
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <map>
0013 #include <string>
0014 #include <sstream>
0015 #include <iterator>
0016 #include <algorithm>
0017 #include <boost/tokenizer.hpp>
0018 #include <boost/lexical_cast.hpp>
0019 #include <boost/date_time/compiler_config.hpp>
0020 #include <boost/date_time/parse_format_base.hpp>
0021 #include <boost/date_time/period.hpp>
0022
0023 #if defined(BOOST_DATE_TIME_NO_LOCALE)
0024 #include <cctype> // ::tolower(int)
0025 #else
0026 #include <locale> // std::tolower(char, locale)
0027 #endif
0028
0029 namespace boost {
0030 namespace date_time {
0031
0032
0033
0034
0035
0036
0037
0038 inline
0039 std::string
0040 convert_to_lower(std::string inp)
0041 {
0042 #if !defined(BOOST_DATE_TIME_NO_LOCALE)
0043 const std::locale loc(std::locale::classic());
0044 #endif
0045 std::string::size_type i = 0, n = inp.length();
0046 for (; i < n; ++i) {
0047 inp[i] =
0048 #if defined(BOOST_DATE_TIME_NO_LOCALE)
0049 static_cast<char>(std::tolower(inp[i]));
0050 #else
0051
0052
0053 std::tolower(inp[i], loc);
0054 #endif
0055 }
0056 return inp;
0057 }
0058
0059
0060 template<class month_type>
0061 inline unsigned short
0062 month_str_to_ushort(std::string const& s) {
0063 if((s.at(0) >= '0') && (s.at(0) <= '9')) {
0064 return boost::lexical_cast<unsigned short>(s);
0065 }
0066 else {
0067 std::string str = convert_to_lower(s);
0068
0069 #if defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
0070 static std::map<std::string, unsigned short> month_map;
0071 typedef std::map<std::string, unsigned short>::value_type vtype;
0072 if( month_map.empty() ) {
0073 month_map.insert( vtype("jan", static_cast<unsigned short>(1)) );
0074 month_map.insert( vtype("january", static_cast<unsigned short>(1)) );
0075 month_map.insert( vtype("feb", static_cast<unsigned short>(2)) );
0076 month_map.insert( vtype("february", static_cast<unsigned short>(2)) );
0077 month_map.insert( vtype("mar", static_cast<unsigned short>(3)) );
0078 month_map.insert( vtype("march", static_cast<unsigned short>(3)) );
0079 month_map.insert( vtype("apr", static_cast<unsigned short>(4)) );
0080 month_map.insert( vtype("april", static_cast<unsigned short>(4)) );
0081 month_map.insert( vtype("may", static_cast<unsigned short>(5)) );
0082 month_map.insert( vtype("jun", static_cast<unsigned short>(6)) );
0083 month_map.insert( vtype("june", static_cast<unsigned short>(6)) );
0084 month_map.insert( vtype("jul", static_cast<unsigned short>(7)) );
0085 month_map.insert( vtype("july", static_cast<unsigned short>(7)) );
0086 month_map.insert( vtype("aug", static_cast<unsigned short>(8)) );
0087 month_map.insert( vtype("august", static_cast<unsigned short>(8)) );
0088 month_map.insert( vtype("sep", static_cast<unsigned short>(9)) );
0089 month_map.insert( vtype("september", static_cast<unsigned short>(9)) );
0090 month_map.insert( vtype("oct", static_cast<unsigned short>(10)) );
0091 month_map.insert( vtype("october", static_cast<unsigned short>(10)) );
0092 month_map.insert( vtype("nov", static_cast<unsigned short>(11)) );
0093 month_map.insert( vtype("november", static_cast<unsigned short>(11)) );
0094 month_map.insert( vtype("dec", static_cast<unsigned short>(12)) );
0095 month_map.insert( vtype("december", static_cast<unsigned short>(12)) );
0096 }
0097 #else
0098 static std::map<std::string, unsigned short> month_map =
0099 { { "jan", static_cast<unsigned short>(1) }, { "january", static_cast<unsigned short>(1) },
0100 { "feb", static_cast<unsigned short>(2) }, { "february", static_cast<unsigned short>(2) },
0101 { "mar", static_cast<unsigned short>(3) }, { "march", static_cast<unsigned short>(3) },
0102 { "apr", static_cast<unsigned short>(4) }, { "april", static_cast<unsigned short>(4) },
0103 { "may", static_cast<unsigned short>(5) },
0104 { "jun", static_cast<unsigned short>(6) }, { "june", static_cast<unsigned short>(6) },
0105 { "jul", static_cast<unsigned short>(7) }, { "july", static_cast<unsigned short>(7) },
0106 { "aug", static_cast<unsigned short>(8) }, { "august", static_cast<unsigned short>(8) },
0107 { "sep", static_cast<unsigned short>(9) }, { "september", static_cast<unsigned short>(9) },
0108 { "oct", static_cast<unsigned short>(10) }, { "october", static_cast<unsigned short>(10)},
0109 { "nov", static_cast<unsigned short>(11) }, { "november", static_cast<unsigned short>(11)},
0110 { "dec", static_cast<unsigned short>(12) }, { "december", static_cast<unsigned short>(12)}
0111 };
0112 #endif
0113 std::map<std::string, unsigned short>::const_iterator mitr = month_map.find( str );
0114 if ( mitr != month_map.end() ) {
0115 return mitr->second;
0116 }
0117 }
0118 return 13;
0119 }
0120
0121
0122
0123
0124
0125
0126
0127
0128 template<class date_type>
0129 date_type
0130 parse_date(const std::string& s, int order_spec = ymd_order_iso) {
0131 std::string spec_str;
0132 if(order_spec == ymd_order_iso) {
0133 spec_str = "ymd";
0134 }
0135 else if(order_spec == ymd_order_dmy) {
0136 spec_str = "dmy";
0137 }
0138 else {
0139 spec_str = "mdy";
0140 }
0141
0142 typedef typename date_type::month_type month_type;
0143 unsigned pos = 0;
0144 unsigned short year(0), month(0), day(0);
0145 typedef typename std::basic_string<char>::traits_type traits_type;
0146 typedef boost::char_separator<char, traits_type> char_separator_type;
0147 typedef boost::tokenizer<char_separator_type,
0148 std::basic_string<char>::const_iterator,
0149 std::basic_string<char> > tokenizer;
0150 typedef boost::tokenizer<char_separator_type,
0151 std::basic_string<char>::const_iterator,
0152 std::basic_string<char> >::iterator tokenizer_iterator;
0153
0154 const char sep_char[] = {',','-','.',' ','/','\0'};
0155 char_separator_type sep(sep_char);
0156 tokenizer tok(s,sep);
0157 for(tokenizer_iterator beg=tok.begin();
0158 beg!=tok.end() && pos < spec_str.size();
0159 ++beg, ++pos) {
0160 switch(spec_str.at(pos)) {
0161 case 'y':
0162 {
0163 year = boost::lexical_cast<unsigned short>(*beg);
0164 break;
0165 }
0166 case 'm':
0167 {
0168 month = month_str_to_ushort<month_type>(*beg);
0169 break;
0170 }
0171 case 'd':
0172 {
0173 day = boost::lexical_cast<unsigned short>(*beg);
0174 break;
0175 }
0176 default: break;
0177 }
0178 }
0179 return date_type(year, month, day);
0180 }
0181
0182
0183 template<class date_type>
0184 date_type
0185 parse_undelimited_date(const std::string& s) {
0186 int offsets[] = {4,2,2};
0187 int pos = 0;
0188
0189 unsigned short y = 0, m = 0, d = 0;
0190
0191
0192
0193
0194
0195
0196 boost::offset_separator osf(offsets, offsets+3, false, false);
0197
0198 typedef typename boost::tokenizer<boost::offset_separator,
0199 std::basic_string<char>::const_iterator,
0200 std::basic_string<char> > tokenizer_type;
0201 tokenizer_type tok(s, osf);
0202 for(typename tokenizer_type::iterator ti=tok.begin(); ti!=tok.end();++ti) {
0203 unsigned short i = boost::lexical_cast<unsigned short>(*ti);
0204 switch(pos) {
0205 case 0: y = i; break;
0206 case 1: m = i; break;
0207 case 2: d = i; break;
0208 default: break;
0209 }
0210 pos++;
0211 }
0212 return date_type(y,m,d);
0213 }
0214
0215
0216
0217
0218
0219 template<class date_type, class iterator_type>
0220 inline
0221 date_type
0222 from_stream_type(iterator_type& beg,
0223 iterator_type const& end,
0224 char)
0225 {
0226 std::ostringstream ss;
0227 while(beg != end) {
0228 ss << *beg++;
0229 }
0230 return parse_date<date_type>(ss.str());
0231 }
0232
0233
0234
0235
0236 template<class date_type, class iterator_type>
0237 inline
0238 date_type
0239 from_stream_type(iterator_type& beg,
0240 iterator_type const& ,
0241 std::string const&)
0242 {
0243 return parse_date<date_type>(*beg);
0244 }
0245
0246
0247
0248
0249
0250
0251
0252 template<class date_type, class iterator_type>
0253 inline
0254 date_type from_stream_type(iterator_type& beg,
0255 iterator_type const& end,
0256 wchar_t)
0257 {
0258 std::ostringstream ss;
0259 #if !defined(BOOST_DATE_TIME_NO_LOCALE)
0260 std::locale loc;
0261 std::ctype<wchar_t> const& fac = std::use_facet<std::ctype<wchar_t> >(loc);
0262 while(beg != end) {
0263 ss << fac.narrow(*beg++, 'X');
0264 }
0265 #else
0266 while(beg != end) {
0267 char c = 'X';
0268 const wchar_t wc = *beg++;
0269 if (wc >= 0 && wc <= 127)
0270 c = static_cast< char >(wc);
0271 ss << c;
0272 }
0273 #endif
0274 return parse_date<date_type>(ss.str());
0275 }
0276 #ifndef BOOST_NO_STD_WSTRING
0277
0278
0279
0280 template<class date_type, class iterator_type>
0281 inline
0282 date_type
0283 from_stream_type(iterator_type& beg,
0284 iterator_type const& ,
0285 std::wstring const&) {
0286 std::wstring ws = *beg;
0287 std::ostringstream ss;
0288 std::wstring::iterator wsb = ws.begin(), wse = ws.end();
0289 #if !defined(BOOST_DATE_TIME_NO_LOCALE)
0290 std::locale loc;
0291 std::ctype<wchar_t> const& fac = std::use_facet<std::ctype<wchar_t> >(loc);
0292 while(wsb != wse) {
0293 ss << fac.narrow(*wsb++, 'X');
0294 }
0295 #else
0296 while(wsb != wse) {
0297 char c = 'X';
0298 const wchar_t wc = *wsb++;
0299 if (wc >= 0 && wc <= 127)
0300 c = static_cast< char >(wc);
0301 ss << c;
0302 }
0303 #endif
0304 return parse_date<date_type>(ss.str());
0305 }
0306 #endif
0307 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
0308
0309 #else
0310
0311 template<class date_type, class charT>
0312 period<date_type, typename date_type::duration_type>
0313 from_simple_string_type(const std::basic_string<charT>& s){
0314 typedef typename std::basic_string<charT>::traits_type traits_type;
0315 typedef typename boost::char_separator<charT, traits_type> char_separator;
0316 typedef typename boost::tokenizer<char_separator,
0317 typename std::basic_string<charT>::const_iterator,
0318 std::basic_string<charT> > tokenizer;
0319 const charT sep_list[4] = {'[','/',']','\0'};
0320 char_separator sep(sep_list);
0321 tokenizer tokens(s, sep);
0322 typename tokenizer::iterator tok_it = tokens.begin();
0323 std::basic_string<charT> date_string = *tok_it;
0324
0325 typename std::basic_string<charT>::iterator date_string_start = date_string.begin(),
0326 date_string_end = date_string.end();
0327 typedef typename std::iterator_traits<typename std::basic_string<charT>::iterator>::value_type value_type;
0328 date_type d1 = from_stream_type<date_type>(date_string_start, date_string_end, value_type());
0329 date_string = *(++tok_it);
0330 date_string_start = date_string.begin(), date_string_end = date_string.end();
0331 date_type d2 = from_stream_type<date_type>(date_string_start, date_string_end, value_type());
0332 return period<date_type, typename date_type::duration_type>(d1, d2);
0333 }
0334 #endif
0335
0336 } }
0337
0338
0339
0340
0341 #endif
0342