time_parsing.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #ifndef _DATE_TIME_TIME_PARSING_HPP___
  2. #define _DATE_TIME_TIME_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 "boost/tokenizer.hpp"
  11. #include "boost/lexical_cast.hpp"
  12. #include "boost/date_time/date_parsing.hpp"
  13. #include "boost/date_time/special_values_parser.hpp"
  14. #include "boost/cstdint.hpp"
  15. #include <iostream>
  16. namespace boost {
  17. namespace date_time {
  18. //! computes exponential math like 2^8 => 256, only works with positive integers
  19. //Not general purpose, but needed b/c std::pow is not available
  20. //everywhere. Hasn't been tested with negatives and zeros
  21. template<class int_type>
  22. inline
  23. int_type power(int_type base, int_type exponent)
  24. {
  25. int_type result = 1;
  26. for(int i = 0; i < exponent; ++i){
  27. result *= base;
  28. }
  29. return result;
  30. }
  31. //! Creates a time_duration object from a delimited string
  32. /*! Expected format for string is "[-]h[h][:mm][:ss][.fff]".
  33. * If the number of fractional digits provided is greater than the
  34. * precision of the time duration type then the extra digits are
  35. * truncated.
  36. *
  37. * A negative duration will be created if the first character in
  38. * string is a '-', all other '-' will be treated as delimiters.
  39. * Accepted delimiters are "-:,.".
  40. */
  41. template<class time_duration, class char_type>
  42. inline
  43. time_duration
  44. str_from_delimited_time_duration(const std::basic_string<char_type>& s)
  45. {
  46. unsigned short min=0, sec =0;
  47. int hour =0;
  48. bool is_neg = (s.at(0) == '-');
  49. boost::int64_t fs=0;
  50. int pos = 0;
  51. typedef typename std::basic_string<char_type>::traits_type traits_type;
  52. typedef boost::char_separator<char_type, traits_type> char_separator_type;
  53. typedef boost::tokenizer<char_separator_type,
  54. typename std::basic_string<char_type>::const_iterator,
  55. std::basic_string<char_type> > tokenizer;
  56. typedef typename boost::tokenizer<char_separator_type,
  57. typename std::basic_string<char_type>::const_iterator,
  58. typename std::basic_string<char_type> >::iterator tokenizer_iterator;
  59. char_type sep_chars[5] = {'-',':',',','.'};
  60. char_separator_type sep(sep_chars);
  61. tokenizer tok(s,sep);
  62. for(tokenizer_iterator beg=tok.begin(); beg!=tok.end();++beg){
  63. switch(pos) {
  64. case 0: {
  65. hour = boost::lexical_cast<int>(*beg);
  66. break;
  67. }
  68. case 1: {
  69. min = boost::lexical_cast<unsigned short>(*beg);
  70. break;
  71. }
  72. case 2: {
  73. sec = boost::lexical_cast<unsigned short>(*beg);
  74. break;
  75. }
  76. case 3: {
  77. int digits = static_cast<int>(beg->length());
  78. //Works around a bug in MSVC 6 library that does not support
  79. //operator>> thus meaning lexical_cast will fail to compile.
  80. #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
  81. // msvc wouldn't compile 'time_duration::num_fractional_digits()'
  82. // (required template argument list) as a workaround a temp
  83. // time_duration object was used
  84. time_duration td(hour,min,sec,fs);
  85. int precision = td.num_fractional_digits();
  86. // _atoi64 is an MS specific function
  87. if(digits >= precision) {
  88. // drop excess digits
  89. fs = _atoi64(beg->substr(0, precision).c_str());
  90. }
  91. else {
  92. fs = _atoi64(beg->c_str());
  93. }
  94. #else
  95. int precision = time_duration::num_fractional_digits();
  96. if(digits >= precision) {
  97. // drop excess digits
  98. fs = boost::lexical_cast<boost::int64_t>(beg->substr(0, precision));
  99. }
  100. else {
  101. fs = boost::lexical_cast<boost::int64_t>(*beg);
  102. }
  103. #endif
  104. if(digits < precision){
  105. // trailing zeros get dropped from the string,
  106. // "1:01:01.1" would yield .000001 instead of .100000
  107. // the power() compensates for the missing decimal places
  108. fs *= power(10, precision - digits);
  109. }
  110. break;
  111. }
  112. default: break;
  113. }//switch
  114. pos++;
  115. }
  116. if(is_neg) {
  117. return -time_duration(hour, min, sec, fs);
  118. }
  119. else {
  120. return time_duration(hour, min, sec, fs);
  121. }
  122. }
  123. //! Creates a time_duration object from a delimited string
  124. /*! Expected format for string is "[-]h[h][:mm][:ss][.fff]".
  125. * If the number of fractional digits provided is greater than the
  126. * precision of the time duration type then the extra digits are
  127. * truncated.
  128. *
  129. * A negative duration will be created if the first character in
  130. * string is a '-', all other '-' will be treated as delimiters.
  131. * Accepted delimiters are "-:,.".
  132. */
  133. template<class time_duration>
  134. inline
  135. time_duration
  136. parse_delimited_time_duration(const std::string& s)
  137. {
  138. return str_from_delimited_time_duration<time_duration,char>(s);
  139. }
  140. //! Utility function to split appart string
  141. inline
  142. bool
  143. split(const std::string& s,
  144. char sep,
  145. std::string& first,
  146. std::string& second)
  147. {
  148. std::string::size_type sep_pos = s.find(sep);
  149. first = s.substr(0,sep_pos);
  150. if (sep_pos!=std::string::npos)
  151. second = s.substr(sep_pos+1);
  152. return true;
  153. }
  154. template<class time_type>
  155. inline
  156. time_type
  157. parse_delimited_time(const std::string& s, char sep)
  158. {
  159. typedef typename time_type::time_duration_type time_duration;
  160. typedef typename time_type::date_type date_type;
  161. //split date/time on a unique delimiter char such as ' ' or 'T'
  162. std::string date_string, tod_string;
  163. split(s, sep, date_string, tod_string);
  164. //call parse_date with first string
  165. date_type d = parse_date<date_type>(date_string);
  166. //call parse_time_duration with remaining string
  167. time_duration td = parse_delimited_time_duration<time_duration>(tod_string);
  168. //construct a time
  169. return time_type(d, td);
  170. }
  171. //! Parse time duration part of an iso time of form: [-]hhmmss[.fff...] (eg: 120259.123 is 12 hours, 2 min, 59 seconds, 123000 microseconds)
  172. template<class time_duration>
  173. inline
  174. time_duration
  175. parse_undelimited_time_duration(const std::string& s)
  176. {
  177. int precision = 0;
  178. {
  179. // msvc wouldn't compile 'time_duration::num_fractional_digits()'
  180. // (required template argument list) as a workaround, a temp
  181. // time_duration object was used
  182. time_duration tmp(0,0,0,1);
  183. precision = tmp.num_fractional_digits();
  184. }
  185. // 'precision+1' is so we grab all digits, plus the decimal
  186. int offsets[] = {2,2,2, precision+1};
  187. int pos = 0, sign = 0;
  188. int hours = 0;
  189. short min=0, sec=0;
  190. boost::int64_t fs=0;
  191. // increment one position if the string was "signed"
  192. if(s.at(sign) == '-')
  193. {
  194. ++sign;
  195. }
  196. // stlport choked when passing s.substr() to tokenizer
  197. // using a new string fixed the error
  198. std::string remain = s.substr(sign);
  199. /* We do not want the offset_separator to wrap the offsets, we
  200. * will never want to process more than:
  201. * 2 char, 2 char, 2 char, frac_sec length.
  202. * We *do* want the offset_separator to give us a partial for the
  203. * last characters if there were not enough provided in the input string. */
  204. bool wrap_off = false;
  205. bool ret_part = true;
  206. boost::offset_separator osf(offsets, offsets+4, wrap_off, ret_part);
  207. typedef boost::tokenizer<boost::offset_separator,
  208. std::basic_string<char>::const_iterator,
  209. std::basic_string<char> > tokenizer;
  210. typedef boost::tokenizer<boost::offset_separator,
  211. std::basic_string<char>::const_iterator,
  212. std::basic_string<char> >::iterator tokenizer_iterator;
  213. tokenizer tok(remain, osf);
  214. for(tokenizer_iterator ti=tok.begin(); ti!=tok.end();++ti){
  215. switch(pos) {
  216. case 0:
  217. {
  218. hours = boost::lexical_cast<int>(*ti);
  219. break;
  220. }
  221. case 1:
  222. {
  223. min = boost::lexical_cast<short>(*ti);
  224. break;
  225. }
  226. case 2:
  227. {
  228. sec = boost::lexical_cast<short>(*ti);
  229. break;
  230. }
  231. case 3:
  232. {
  233. std::string char_digits(ti->substr(1)); // digits w/no decimal
  234. int digits = static_cast<int>(char_digits.length());
  235. //Works around a bug in MSVC 6 library that does not support
  236. //operator>> thus meaning lexical_cast will fail to compile.
  237. #if (defined(BOOST_MSVC) && (_MSC_VER <= 1200)) // 1200 == VC++ 6.0
  238. // _atoi64 is an MS specific function
  239. if(digits >= precision) {
  240. // drop excess digits
  241. fs = _atoi64(char_digits.substr(0, precision).c_str());
  242. }
  243. else if(digits == 0) {
  244. fs = 0; // just in case _atoi64 doesn't like an empty string
  245. }
  246. else {
  247. fs = _atoi64(char_digits.c_str());
  248. }
  249. #else
  250. if(digits >= precision) {
  251. // drop excess digits
  252. fs = boost::lexical_cast<boost::int64_t>(char_digits.substr(0, precision));
  253. }
  254. else if(digits == 0) {
  255. fs = 0; // lexical_cast doesn't like empty strings
  256. }
  257. else {
  258. fs = boost::lexical_cast<boost::int64_t>(char_digits);
  259. }
  260. #endif
  261. if(digits < precision){
  262. // trailing zeros get dropped from the string,
  263. // "1:01:01.1" would yield .000001 instead of .100000
  264. // the power() compensates for the missing decimal places
  265. fs *= power(10, precision - digits);
  266. }
  267. break;
  268. }
  269. default: break;
  270. }
  271. pos++;
  272. }
  273. if(sign) {
  274. return -time_duration(hours, min, sec, fs);
  275. }
  276. else {
  277. return time_duration(hours, min, sec, fs);
  278. }
  279. }
  280. //! Parse time string of form YYYYMMDDThhmmss where T is delimeter between date and time
  281. template<class time_type>
  282. inline
  283. time_type
  284. parse_iso_time(const std::string& s, char sep)
  285. {
  286. typedef typename time_type::time_duration_type time_duration;
  287. typedef typename time_type::date_type date_type;
  288. typedef special_values_parser<date_type, std::string::value_type> svp_type;
  289. // given to_iso_string can produce a special value string
  290. // then from_iso_string should be able to read a special value string
  291. // the special_values_parser is expensive to set up and not thread-safe
  292. // so it cannot be static, so we need to be careful about when we use it
  293. if (svp_type::likely(s)) {
  294. typedef typename svp_type::stringstream_type ss_type;
  295. typedef typename svp_type::stream_itr_type itr_type;
  296. typedef typename svp_type::match_results mr_type;
  297. svp_type p; // expensive
  298. mr_type mr;
  299. ss_type ss(s);
  300. itr_type itr(ss);
  301. itr_type end;
  302. if (p.match(itr, end, mr)) {
  303. return time_type(static_cast<special_values>(mr.current_match));
  304. }
  305. }
  306. //split date/time on a unique delimiter char such as ' ' or 'T'
  307. std::string date_string, tod_string;
  308. split(s, sep, date_string, tod_string);
  309. //call parse_date with first string
  310. date_type d = parse_undelimited_date<date_type>(date_string);
  311. //call parse_time_duration with remaining string
  312. time_duration td = parse_undelimited_time_duration<time_duration>(tod_string);
  313. //construct a time
  314. return time_type(d, td);
  315. }
  316. } }//namespace date_time
  317. #endif