Chris@16: #ifndef _DATE_TIME_DATE_PARSING_HPP___ Chris@16: #define _DATE_TIME_DATE_PARSING_HPP___ Chris@16: Chris@16: /* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc. Chris@16: * Use, modification and distribution is subject to the Chris@16: * Boost Software License, Version 1.0. (See accompanying Chris@16: * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) Chris@16: * Author: Jeff Garland, Bart Garst Chris@101: * $Date$ Chris@16: */ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_DATE_TIME_NO_LOCALE) Chris@16: #include // ::tolower(int) Chris@16: #else Chris@16: #include // std::tolower(char, locale) Chris@16: #endif Chris@16: Chris@16: namespace boost { Chris@16: namespace date_time { Chris@16: Chris@16: //! A function to replace the std::transform( , , ,tolower) construct Chris@16: /*! This function simply takes a string, and changes all the characters Chris@16: * in that string to lowercase (according to the default system locale). Chris@16: * In the event that a compiler does not support locales, the old Chris@16: * C style tolower() is used. Chris@16: */ Chris@16: inline Chris@16: std::string Chris@16: convert_to_lower(std::string inp) Chris@16: { Chris@16: #if !defined(BOOST_DATE_TIME_NO_LOCALE) Chris@16: const std::locale loc(std::locale::classic()); Chris@16: #endif Chris@16: std::string::size_type i = 0, n = inp.length(); Chris@16: for (; i < n; ++i) { Chris@16: inp[i] = Chris@16: #if defined(BOOST_DATE_TIME_NO_LOCALE) Chris@16: static_cast(std::tolower(inp[i])); Chris@16: #else Chris@16: // tolower and others were brought in to std for borland >= v564 Chris@16: // in compiler_config.hpp Chris@16: std::tolower(inp[i], loc); Chris@16: #endif Chris@16: } Chris@16: return inp; Chris@16: } Chris@16: Chris@16: //! Helper function for parse_date. Chris@16: /* Used by-value parameter because we change the string and may Chris@16: * want to preserve the original argument */ Chris@16: template Chris@16: inline unsigned short Chris@16: month_str_to_ushort(std::string const& s) { Chris@16: if((s.at(0) >= '0') && (s.at(0) <= '9')) { Chris@16: return boost::lexical_cast(s); Chris@16: } Chris@16: else { Chris@16: std::string str = convert_to_lower(s); Chris@16: typename month_type::month_map_ptr_type ptr = month_type::get_month_map_ptr(); Chris@16: typename month_type::month_map_type::iterator iter = ptr->find(str); Chris@16: if(iter != ptr->end()) { // required for STLport Chris@16: return iter->second; Chris@16: } Chris@16: } Chris@16: return 13; // intentionally out of range - name not found Chris@16: } Chris@16: Chris@16: //! Find index of a string in either of 2 arrays Chris@16: /*! find_match searches both arrays for a match to 's'. Both arrays Chris@16: * must contain 'size' elements. The index of the match is returned. Chris@16: * If no match is found, 'size' is returned. Chris@16: * Ex. "Jan" returns 0, "Dec" returns 11, "Tue" returns 2. Chris@16: * 'size' can be sent in with: (greg_month::max)() (which 12), Chris@16: * (greg_weekday::max)() + 1 (which is 7) or date_time::NumSpecialValues */ Chris@16: template Chris@16: short find_match(const charT* const* short_names, Chris@16: const charT* const* long_names, Chris@16: short size, Chris@16: const std::basic_string& s) { Chris@16: for(short i = 0; i < size; ++i){ Chris@16: if(short_names[i] == s || long_names[i] == s){ Chris@16: return i; Chris@16: } Chris@16: } Chris@16: return size; // not-found, return a value out of range Chris@16: } Chris@16: Chris@16: //! Generic function to parse a delimited date (eg: 2002-02-10) Chris@16: /*! Accepted formats are: "2003-02-10" or " 2003-Feb-10" or Chris@16: * "2003-Feburary-10" Chris@16: * The order in which the Month, Day, & Year appear in the argument Chris@16: * string can be accomodated by passing in the appropriate ymd_order_spec Chris@16: */ Chris@16: template Chris@16: date_type Chris@16: parse_date(const std::string& s, int order_spec = ymd_order_iso) { Chris@16: std::string spec_str; Chris@16: if(order_spec == ymd_order_iso) { Chris@16: spec_str = "ymd"; Chris@16: } Chris@16: else if(order_spec == ymd_order_dmy) { Chris@16: spec_str = "dmy"; Chris@16: } Chris@16: else { // (order_spec == ymd_order_us) Chris@16: spec_str = "mdy"; Chris@16: } Chris@16: Chris@16: typedef typename date_type::month_type month_type; Chris@16: unsigned pos = 0; Chris@16: unsigned short year(0), month(0), day(0); Chris@16: typedef typename std::basic_string::traits_type traits_type; Chris@16: typedef boost::char_separator char_separator_type; Chris@16: typedef boost::tokenizer::const_iterator, Chris@16: std::basic_string > tokenizer; Chris@16: typedef boost::tokenizer::const_iterator, Chris@16: std::basic_string >::iterator tokenizer_iterator; Chris@16: // may need more delimiters, these work for the regression tests Chris@16: const char sep_char[] = {',','-','.',' ','/','\0'}; Chris@16: char_separator_type sep(sep_char); Chris@16: tokenizer tok(s,sep); Chris@16: for(tokenizer_iterator beg=tok.begin(); Chris@16: beg!=tok.end() && pos < spec_str.size(); Chris@16: ++beg, ++pos) { Chris@16: switch(spec_str.at(pos)) { Chris@16: case 'y': Chris@16: { Chris@16: year = boost::lexical_cast(*beg); Chris@16: break; Chris@16: } Chris@16: case 'm': Chris@16: { Chris@16: month = month_str_to_ushort(*beg); Chris@16: break; Chris@16: } Chris@16: case 'd': Chris@16: { Chris@16: day = boost::lexical_cast(*beg); Chris@16: break; Chris@16: } Chris@16: default: break; Chris@16: } //switch Chris@16: } Chris@16: return date_type(year, month, day); Chris@16: } Chris@16: Chris@16: //! Generic function to parse undelimited date (eg: 20020201) Chris@16: template Chris@16: date_type Chris@16: parse_undelimited_date(const std::string& s) { Chris@16: int offsets[] = {4,2,2}; Chris@16: int pos = 0; Chris@16: //typename date_type::ymd_type ymd((year_type::min)(),1,1); Chris@16: unsigned short y = 0, m = 0, d = 0; Chris@16: Chris@16: /* The two bool arguments state that parsing will not wrap Chris@16: * (only the first 8 characters will be parsed) and partial Chris@16: * strings will not be parsed. Chris@16: * Ex: Chris@16: * "2005121" will parse 2005 & 12, but not the "1" */ Chris@16: boost::offset_separator osf(offsets, offsets+3, false, false); Chris@16: Chris@16: typedef typename boost::tokenizer::const_iterator, Chris@16: std::basic_string > tokenizer_type; Chris@16: tokenizer_type tok(s, osf); Chris@16: for(typename tokenizer_type::iterator ti=tok.begin(); ti!=tok.end();++ti) { Chris@16: unsigned short i = boost::lexical_cast(*ti); Chris@16: switch(pos) { Chris@16: case 0: y = i; break; Chris@16: case 1: m = i; break; Chris@16: case 2: d = i; break; Chris@16: default: break; Chris@16: } Chris@16: pos++; Chris@16: } Chris@16: return date_type(y,m,d); Chris@16: } Chris@16: Chris@16: //! Helper function for 'date gregorian::from_stream()' Chris@16: /*! Creates a string from the iterators that reference the Chris@16: * begining & end of a char[] or string. All elements are Chris@16: * used in output string */ Chris@16: template Chris@16: inline Chris@16: date_type Chris@16: from_stream_type(iterator_type& beg, Chris@16: iterator_type const& end, Chris@16: char) Chris@16: { Chris@16: std::ostringstream ss; Chris@16: while(beg != end) { Chris@16: ss << *beg++; Chris@16: } Chris@16: return parse_date(ss.str()); Chris@16: } Chris@16: Chris@16: //! Helper function for 'date gregorian::from_stream()' Chris@16: /*! Returns the first string found in the stream referenced by the Chris@16: * begining & end iterators */ Chris@16: template Chris@16: inline Chris@16: date_type Chris@16: from_stream_type(iterator_type& beg, Chris@16: iterator_type const& /* end */, Chris@16: std::string const&) Chris@16: { Chris@16: return parse_date(*beg); Chris@16: } Chris@16: Chris@16: /* I believe the wchar stuff would be best elsewhere, perhaps in Chris@16: * parse_date<>()? In the mean time this gets us started... */ Chris@16: //! Helper function for 'date gregorian::from_stream()' Chris@16: /*! Creates a string from the iterators that reference the Chris@16: * begining & end of a wstring. All elements are Chris@16: * used in output string */ Chris@16: template Chris@16: inline Chris@16: date_type from_stream_type(iterator_type& beg, Chris@16: iterator_type const& end, Chris@16: wchar_t) Chris@16: { Chris@16: std::ostringstream ss; Chris@16: #if !defined(BOOST_DATE_TIME_NO_LOCALE) Chris@16: std::locale loc; Chris@16: std::ctype const& fac = std::use_facet >(loc); Chris@16: while(beg != end) { Chris@16: ss << fac.narrow(*beg++, 'X'); // 'X' will cause exception to be thrown Chris@16: } Chris@16: #else Chris@16: while(beg != end) { Chris@16: char c = 'X'; // 'X' will cause exception to be thrown Chris@16: const wchar_t wc = *beg++; Chris@16: if (wc >= 0 && wc <= 127) Chris@16: c = static_cast< char >(wc); Chris@16: ss << c; Chris@16: } Chris@16: #endif Chris@16: return parse_date(ss.str()); Chris@16: } Chris@16: #ifndef BOOST_NO_STD_WSTRING Chris@16: //! Helper function for 'date gregorian::from_stream()' Chris@16: /*! Creates a string from the first wstring found in the stream Chris@16: * referenced by the begining & end iterators */ Chris@16: template Chris@16: inline Chris@16: date_type Chris@16: from_stream_type(iterator_type& beg, Chris@16: iterator_type const& /* end */, Chris@16: std::wstring const&) { Chris@16: std::wstring ws = *beg; Chris@16: std::ostringstream ss; Chris@16: std::wstring::iterator wsb = ws.begin(), wse = ws.end(); Chris@16: #if !defined(BOOST_DATE_TIME_NO_LOCALE) Chris@16: std::locale loc; Chris@16: std::ctype const& fac = std::use_facet >(loc); Chris@16: while(wsb != wse) { Chris@16: ss << fac.narrow(*wsb++, 'X'); // 'X' will cause exception to be thrown Chris@16: } Chris@16: #else Chris@16: while(wsb != wse) { Chris@16: char c = 'X'; // 'X' will cause exception to be thrown Chris@16: const wchar_t wc = *wsb++; Chris@16: if (wc >= 0 && wc <= 127) Chris@16: c = static_cast< char >(wc); Chris@16: ss << c; Chris@16: } Chris@16: #endif Chris@16: return parse_date(ss.str()); Chris@16: } Chris@16: #endif // BOOST_NO_STD_WSTRING Chris@16: #if (defined(BOOST_MSVC) && (_MSC_VER < 1300)) Chris@16: // This function cannot be compiled with MSVC 6.0 due to internal compiler shorcomings Chris@16: #else Chris@16: //! function called by wrapper functions: date_period_from_(w)string() Chris@16: template Chris@16: period Chris@16: from_simple_string_type(const std::basic_string& s){ Chris@16: typedef typename std::basic_string::traits_type traits_type; Chris@16: typedef typename boost::char_separator char_separator; Chris@16: typedef typename boost::tokenizer::const_iterator, Chris@16: std::basic_string > tokenizer; Chris@16: const charT sep_list[4] = {'[','/',']','\0'}; Chris@16: char_separator sep(sep_list); Chris@16: tokenizer tokens(s, sep); Chris@16: typename tokenizer::iterator tok_it = tokens.begin(); Chris@16: std::basic_string date_string = *tok_it; Chris@16: // get 2 string iterators and generate a date from them Chris@16: typename std::basic_string::iterator date_string_start = date_string.begin(), Chris@16: date_string_end = date_string.end(); Chris@16: typedef typename std::iterator_traits::iterator>::value_type value_type; Chris@16: date_type d1 = from_stream_type(date_string_start, date_string_end, value_type()); Chris@16: date_string = *(++tok_it); // next token Chris@16: date_string_start = date_string.begin(), date_string_end = date_string.end(); Chris@16: date_type d2 = from_stream_type(date_string_start, date_string_end, value_type()); Chris@16: return period(d1, d2); Chris@16: } Chris@16: #endif Chris@16: Chris@16: } } //namespace date_time Chris@16: Chris@16: Chris@16: Chris@16: Chris@16: #endif Chris@16: