Back to home page

EIC code displayed by LXR

 
 

    


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 /* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc.
0005  * Use, modification and distribution is subject to the
0006  * Boost Software License, Version 1.0. (See accompanying
0007  * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
0008  * Author: Jeff Garland, Bart Garst
0009  * $Date$
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   //! A function to replace the std::transform( , , ,tolower) construct
0033   /*! This function simply takes a string, and changes all the characters
0034    * in that string to lowercase (according to the default system locale).
0035    * In the event that a compiler does not support locales, the old
0036    * C style tolower() is used.
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         // tolower and others were brought in to std for borland >= v564
0052         // in compiler_config.hpp
0053         std::tolower(inp[i], loc);
0054 #endif
0055     }
0056     return inp;
0057   }
0058 
0059     //! Helper function for parse_date.
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         //c++98 support
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  //c+11 and beyond
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; // intentionally out of range - name not found
0119     }
0120  
0121 
0122     //! Generic function to parse a delimited date (eg: 2002-02-10)
0123     /*! Accepted formats are: "2003-02-10" or " 2003-Feb-10" or
0124      * "2003-Feburary-10"
0125      * The order in which the Month, Day, & Year appear in the argument
0126      * string can be accomodated by passing in the appropriate ymd_order_spec
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 { // (order_spec == ymd_order_us)
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       // may need more delimiters, these work for the regression tests
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         } //switch
0178       }
0179       return date_type(year, month, day);
0180     }
0181 
0182     //! Generic function to parse undelimited date (eg: 20020201)
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       //typename date_type::ymd_type ymd((year_type::min)(),1,1);
0189       unsigned short y = 0, m = 0, d = 0;
0190 
0191       /* The two bool arguments state that parsing will not wrap
0192        * (only the first 8 characters will be parsed) and partial
0193        * strings will not be parsed.
0194        * Ex:
0195        * "2005121" will parse 2005 & 12, but not the "1" */
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     //! Helper function for 'date gregorian::from_stream()'
0216     /*! Creates a string from the iterators that reference the
0217      * begining & end of a char[] or string. All elements are
0218      * used in output string */
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     //! Helper function for 'date gregorian::from_stream()'
0234     /*! Returns the first string found in the stream referenced by the
0235      * begining & end iterators */
0236     template<class date_type, class iterator_type>
0237     inline
0238     date_type
0239     from_stream_type(iterator_type& beg,
0240                      iterator_type const& /* end */,
0241                      std::string const&)
0242     {
0243       return parse_date<date_type>(*beg);
0244     }
0245 
0246     /* I believe the wchar stuff would be best elsewhere, perhaps in
0247      * parse_date<>()? In the mean time this gets us started... */
0248     //! Helper function for 'date gregorian::from_stream()'
0249     /*! Creates a string from the iterators that reference the
0250      * begining & end of a wstring. All elements are
0251      * used in output string */
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'); // 'X' will cause exception to be thrown
0264       }
0265 #else
0266       while(beg != end) {
0267         char c = 'X'; // 'X' will cause exception to be thrown
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     //! Helper function for 'date gregorian::from_stream()'
0278     /*! Creates a string from the first wstring found in the stream
0279      * referenced by the begining & end iterators */
0280     template<class date_type, class iterator_type>
0281     inline
0282     date_type
0283     from_stream_type(iterator_type& beg,
0284                      iterator_type const& /* end */,
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'); // 'X' will cause exception to be thrown
0294       }
0295 #else
0296       while(wsb != wse) {
0297         char c = 'X'; // 'X' will cause exception to be thrown
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 // BOOST_NO_STD_WSTRING
0307 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
0308     // This function cannot be compiled with MSVC 6.0 due to internal compiler shorcomings
0309 #else
0310     //! function called by wrapper functions: date_period_from_(w)string()
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       // get 2 string iterators and generate a date from them
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); // next token
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 } } //namespace date_time
0337 
0338 
0339 
0340 
0341 #endif
0342