time_parsing.hpp 11 KB

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