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