date_parsing.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 <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::month_type month_type;
  108. unsigned pos = 0;
  109. unsigned short year(0), month(0), day(0);
  110. typedef typename std::basic_string<char>::traits_type traits_type;
  111. typedef boost::char_separator<char, traits_type> char_separator_type;
  112. typedef boost::tokenizer<char_separator_type,
  113. std::basic_string<char>::const_iterator,
  114. std::basic_string<char> > tokenizer;
  115. typedef boost::tokenizer<char_separator_type,
  116. std::basic_string<char>::const_iterator,
  117. std::basic_string<char> >::iterator tokenizer_iterator;
  118. // may need more delimiters, these work for the regression tests
  119. const char sep_char[] = {',','-','.',' ','/','\0'};
  120. char_separator_type sep(sep_char);
  121. tokenizer tok(s,sep);
  122. for(tokenizer_iterator beg=tok.begin();
  123. beg!=tok.end() && pos < spec_str.size();
  124. ++beg, ++pos) {
  125. switch(spec_str.at(pos)) {
  126. case 'y':
  127. {
  128. year = boost::lexical_cast<unsigned short>(*beg);
  129. break;
  130. }
  131. case 'm':
  132. {
  133. month = month_str_to_ushort<month_type>(*beg);
  134. break;
  135. }
  136. case 'd':
  137. {
  138. day = boost::lexical_cast<unsigned short>(*beg);
  139. break;
  140. }
  141. default: break;
  142. } //switch
  143. }
  144. return date_type(year, month, day);
  145. }
  146. //! Generic function to parse undelimited date (eg: 20020201)
  147. template<class date_type>
  148. date_type
  149. parse_undelimited_date(const std::string& s) {
  150. int offsets[] = {4,2,2};
  151. int pos = 0;
  152. //typename date_type::ymd_type ymd((year_type::min)(),1,1);
  153. unsigned short y = 0, m = 0, d = 0;
  154. /* The two bool arguments state that parsing will not wrap
  155. * (only the first 8 characters will be parsed) and partial
  156. * strings will not be parsed.
  157. * Ex:
  158. * "2005121" will parse 2005 & 12, but not the "1" */
  159. boost::offset_separator osf(offsets, offsets+3, false, false);
  160. typedef typename boost::tokenizer<boost::offset_separator,
  161. std::basic_string<char>::const_iterator,
  162. std::basic_string<char> > tokenizer_type;
  163. tokenizer_type tok(s, osf);
  164. for(typename tokenizer_type::iterator ti=tok.begin(); ti!=tok.end();++ti) {
  165. unsigned short i = boost::lexical_cast<unsigned short>(*ti);
  166. switch(pos) {
  167. case 0: y = i; break;
  168. case 1: m = i; break;
  169. case 2: d = i; break;
  170. default: break;
  171. }
  172. pos++;
  173. }
  174. return date_type(y,m,d);
  175. }
  176. //! Helper function for 'date gregorian::from_stream()'
  177. /*! Creates a string from the iterators that reference the
  178. * begining & end of a char[] or string. All elements are
  179. * used in output string */
  180. template<class date_type, class iterator_type>
  181. inline
  182. date_type
  183. from_stream_type(iterator_type& beg,
  184. iterator_type const& end,
  185. char)
  186. {
  187. std::ostringstream ss;
  188. while(beg != end) {
  189. ss << *beg++;
  190. }
  191. return parse_date<date_type>(ss.str());
  192. }
  193. //! Helper function for 'date gregorian::from_stream()'
  194. /*! Returns the first string found in the stream referenced by the
  195. * begining & end iterators */
  196. template<class date_type, class iterator_type>
  197. inline
  198. date_type
  199. from_stream_type(iterator_type& beg,
  200. iterator_type const& /* end */,
  201. std::string const&)
  202. {
  203. return parse_date<date_type>(*beg);
  204. }
  205. /* I believe the wchar stuff would be best elsewhere, perhaps in
  206. * parse_date<>()? In the mean time this gets us started... */
  207. //! Helper function for 'date gregorian::from_stream()'
  208. /*! Creates a string from the iterators that reference the
  209. * begining & end of a wstring. All elements are
  210. * used in output string */
  211. template<class date_type, class iterator_type>
  212. inline
  213. date_type from_stream_type(iterator_type& beg,
  214. iterator_type const& end,
  215. wchar_t)
  216. {
  217. std::ostringstream ss;
  218. #if !defined(BOOST_DATE_TIME_NO_LOCALE)
  219. std::locale loc;
  220. std::ctype<wchar_t> const& fac = std::use_facet<std::ctype<wchar_t> >(loc);
  221. while(beg != end) {
  222. ss << fac.narrow(*beg++, 'X'); // 'X' will cause exception to be thrown
  223. }
  224. #else
  225. while(beg != end) {
  226. char c = 'X'; // 'X' will cause exception to be thrown
  227. const wchar_t wc = *beg++;
  228. if (wc >= 0 && wc <= 127)
  229. c = static_cast< char >(wc);
  230. ss << c;
  231. }
  232. #endif
  233. return parse_date<date_type>(ss.str());
  234. }
  235. #ifndef BOOST_NO_STD_WSTRING
  236. //! Helper function for 'date gregorian::from_stream()'
  237. /*! Creates a string from the first wstring found in the stream
  238. * referenced by the begining & end iterators */
  239. template<class date_type, class iterator_type>
  240. inline
  241. date_type
  242. from_stream_type(iterator_type& beg,
  243. iterator_type const& /* end */,
  244. std::wstring const&) {
  245. std::wstring ws = *beg;
  246. std::ostringstream ss;
  247. std::wstring::iterator wsb = ws.begin(), wse = ws.end();
  248. #if !defined(BOOST_DATE_TIME_NO_LOCALE)
  249. std::locale loc;
  250. std::ctype<wchar_t> const& fac = std::use_facet<std::ctype<wchar_t> >(loc);
  251. while(wsb != wse) {
  252. ss << fac.narrow(*wsb++, 'X'); // 'X' will cause exception to be thrown
  253. }
  254. #else
  255. while(wsb != wse) {
  256. char c = 'X'; // 'X' will cause exception to be thrown
  257. const wchar_t wc = *wsb++;
  258. if (wc >= 0 && wc <= 127)
  259. c = static_cast< char >(wc);
  260. ss << c;
  261. }
  262. #endif
  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. } } //namespace date_time
  295. #endif