date_parsing.hpp 12 KB

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