123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- #ifndef _DATE_TIME_DATE_PARSING_HPP___
- #define _DATE_TIME_DATE_PARSING_HPP___
- #include <string>
- #include <iterator>
- #include <algorithm>
- #include <boost/tokenizer.hpp>
- #include <boost/lexical_cast.hpp>
- #include <boost/date_time/compiler_config.hpp>
- #include <boost/date_time/parse_format_base.hpp>
- #if defined(BOOST_DATE_TIME_NO_LOCALE)
- #include <cctype>
- #else
- #include <locale>
- #endif
- namespace boost {
- namespace date_time {
-
-
- inline
- std::string
- convert_to_lower(std::string inp)
- {
- #if !defined(BOOST_DATE_TIME_NO_LOCALE)
- const std::locale loc(std::locale::classic());
- #endif
- std::string::size_type i = 0, n = inp.length();
- for (; i < n; ++i) {
- inp[i] =
- #if defined(BOOST_DATE_TIME_NO_LOCALE)
- static_cast<char>(std::tolower(inp[i]));
- #else
-
-
- std::tolower(inp[i], loc);
- #endif
- }
- return inp;
- }
-
-
- template<class month_type>
- inline unsigned short
- month_str_to_ushort(std::string const& s) {
- if((s.at(0) >= '0') && (s.at(0) <= '9')) {
- return boost::lexical_cast<unsigned short>(s);
- }
- else {
- std::string str = convert_to_lower(s);
- typename month_type::month_map_ptr_type ptr = month_type::get_month_map_ptr();
- typename month_type::month_map_type::iterator iter = ptr->find(str);
- if(iter != ptr->end()) {
- return iter->second;
- }
- }
- return 13;
- }
-
-
-
- template<class charT>
- short find_match(const charT* const* short_names,
- const charT* const* long_names,
- short size,
- const std::basic_string<charT>& s) {
- for(short i = 0; i < size; ++i){
- if(short_names[i] == s || long_names[i] == s){
- return i;
- }
- }
- return size;
- }
-
-
- template<class date_type>
- date_type
- parse_date(const std::string& s, int order_spec = ymd_order_iso) {
- std::string spec_str;
- if(order_spec == ymd_order_iso) {
- spec_str = "ymd";
- }
- else if(order_spec == ymd_order_dmy) {
- spec_str = "dmy";
- }
- else {
- spec_str = "mdy";
- }
- typedef typename date_type::month_type month_type;
- unsigned pos = 0;
- unsigned short year(0), month(0), day(0);
- typedef typename std::basic_string<char>::traits_type traits_type;
- typedef boost::char_separator<char, traits_type> char_separator_type;
- typedef boost::tokenizer<char_separator_type,
- std::basic_string<char>::const_iterator,
- std::basic_string<char> > tokenizer;
- typedef boost::tokenizer<char_separator_type,
- std::basic_string<char>::const_iterator,
- std::basic_string<char> >::iterator tokenizer_iterator;
-
- const char sep_char[] = {',','-','.',' ','/','\0'};
- char_separator_type sep(sep_char);
- tokenizer tok(s,sep);
- for(tokenizer_iterator beg=tok.begin();
- beg!=tok.end() && pos < spec_str.size();
- ++beg, ++pos) {
- switch(spec_str.at(pos)) {
- case 'y':
- {
- year = boost::lexical_cast<unsigned short>(*beg);
- break;
- }
- case 'm':
- {
- month = month_str_to_ushort<month_type>(*beg);
- break;
- }
- case 'd':
- {
- day = boost::lexical_cast<unsigned short>(*beg);
- break;
- }
- default: break;
- }
- }
- return date_type(year, month, day);
- }
-
- template<class date_type>
- date_type
- parse_undelimited_date(const std::string& s) {
- int offsets[] = {4,2,2};
- int pos = 0;
-
- unsigned short y = 0, m = 0, d = 0;
-
- boost::offset_separator osf(offsets, offsets+3, false, false);
- typedef typename boost::tokenizer<boost::offset_separator,
- std::basic_string<char>::const_iterator,
- std::basic_string<char> > tokenizer_type;
- tokenizer_type tok(s, osf);
- for(typename tokenizer_type::iterator ti=tok.begin(); ti!=tok.end();++ti) {
- unsigned short i = boost::lexical_cast<unsigned short>(*ti);
- switch(pos) {
- case 0: y = i; break;
- case 1: m = i; break;
- case 2: d = i; break;
- default: break;
- }
- pos++;
- }
- return date_type(y,m,d);
- }
-
-
- template<class date_type, class iterator_type>
- inline
- date_type
- from_stream_type(iterator_type& beg,
- iterator_type const& end,
- char)
- {
- std::ostringstream ss;
- while(beg != end) {
- ss << *beg++;
- }
- return parse_date<date_type>(ss.str());
- }
-
-
- template<class date_type, class iterator_type>
- inline
- date_type
- from_stream_type(iterator_type& beg,
- iterator_type const& /* end */,
- std::string const&)
- {
- return parse_date<date_type>(*beg);
- }
-
-
-
- template<class date_type, class iterator_type>
- inline
- date_type from_stream_type(iterator_type& beg,
- iterator_type const& end,
- wchar_t)
- {
- std::ostringstream ss;
- #if !defined(BOOST_DATE_TIME_NO_LOCALE)
- std::locale loc;
- std::ctype<wchar_t> const& fac = std::use_facet<std::ctype<wchar_t> >(loc);
- while(beg != end) {
- ss << fac.narrow(*beg++, 'X');
- }
- #else
- while(beg != end) {
- char c = 'X';
- const wchar_t wc = *beg++;
- if (wc >= 0 && wc <= 127)
- c = static_cast< char >(wc);
- ss << c;
- }
- #endif
- return parse_date<date_type>(ss.str());
- }
- #ifndef BOOST_NO_STD_WSTRING
-
-
- template<class date_type, class iterator_type>
- inline
- date_type
- from_stream_type(iterator_type& beg,
- iterator_type const& /* end */,
- std::wstring const&) {
- std::wstring ws = *beg;
- std::ostringstream ss;
- std::wstring::iterator wsb = ws.begin(), wse = ws.end();
- #if !defined(BOOST_DATE_TIME_NO_LOCALE)
- std::locale loc;
- std::ctype<wchar_t> const& fac = std::use_facet<std::ctype<wchar_t> >(loc);
- while(wsb != wse) {
- ss << fac.narrow(*wsb++, 'X');
- }
- #else
- while(wsb != wse) {
- char c = 'X';
- const wchar_t wc = *wsb++;
- if (wc >= 0 && wc <= 127)
- c = static_cast< char >(wc);
- ss << c;
- }
- #endif
- return parse_date<date_type>(ss.str());
- }
- #endif
- #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
-
- #else
-
- template<class date_type, class charT>
- period<date_type, typename date_type::duration_type>
- from_simple_string_type(const std::basic_string<charT>& s){
- typedef typename std::basic_string<charT>::traits_type traits_type;
- typedef typename boost::char_separator<charT, traits_type> char_separator;
- typedef typename boost::tokenizer<char_separator,
- typename std::basic_string<charT>::const_iterator,
- std::basic_string<charT> > tokenizer;
- const charT sep_list[4] = {'[','/',']','\0'};
- char_separator sep(sep_list);
- tokenizer tokens(s, sep);
- typename tokenizer::iterator tok_it = tokens.begin();
- std::basic_string<charT> date_string = *tok_it;
-
- typename std::basic_string<charT>::iterator date_string_start = date_string.begin(),
- date_string_end = date_string.end();
- typedef typename std::iterator_traits<typename std::basic_string<charT>::iterator>::value_type value_type;
- date_type d1 = from_stream_type<date_type>(date_string_start, date_string_end, value_type());
- date_string = *(++tok_it);
- date_string_start = date_string.begin(), date_string_end = date_string.end();
- date_type d2 = from_stream_type<date_type>(date_string_start, date_string_end, value_type());
- return period<date_type, typename date_type::duration_type>(d1, d2);
- }
- #endif
- } }
- #endif
|