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