date_parsing.hpp 14 KB

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