1 #ifndef _DATE_TIME_TIME_PARSING_HPP___
2 #define _DATE_TIME_TIME_PARSING_HPP___
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
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"
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 //everywehere. Hasn't been tested with negatives and zeros
25 template<class int_type>
27 int_type power(int_type base, int_type exponent)
30 for(int i = 0; i < exponent; ++i){
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
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 "-:,.".
46 template<class time_duration, class char_type>
49 str_from_delimited_time_duration(const std::basic_string<char_type>& s)
51 unsigned short min=0, sec =0;
53 bool is_neg = (s.at(0) == '-');
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;
66 char_type sep_chars[5] = {'-',':',',','.'};
67 char_separator_type sep(sep_chars);
69 for(tokenizer_iterator beg=tok.begin(); beg!=tok.end();++beg){
72 hour = boost::lexical_cast<int>(*beg);
76 min = boost::lexical_cast<unsigned short>(*beg);
80 sec = boost::lexical_cast<unsigned short>(*beg);
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) {
96 fs = _atoi64(beg->substr(0, precision).c_str());
99 fs = _atoi64(beg->c_str());
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));
108 fs = boost::lexical_cast<boost::int64_t>(*beg);
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);
125 return -time_duration(hour, min, sec, fs);
128 return time_duration(hour, min, sec, fs);
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
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 "-:,.".
142 template<class time_duration>
145 parse_delimited_time_duration(const std::string& s)
147 return str_from_delimited_time_duration<time_duration,char>(s);
150 //! Utility function to split appart string
153 split(const std::string& s,
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);
166 template<class time_type>
169 parse_delimited_time(const std::string& s, char sep)
171 typedef typename time_type::time_duration_type time_duration;
172 typedef typename time_type::date_type date_type;
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);
182 return time_type(d, td);
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>
190 parse_undelimited_time_duration(const std::string& s)
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();
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;
206 // increment one position if the string was "signed"
207 if(s.at(sign) == '-')
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){
233 hours = boost::lexical_cast<int>(*ti);
238 min = boost::lexical_cast<short>(*ti);
243 sec = boost::lexical_cast<short>(*ti);
248 std::string char_digits(ti->substr(1)); // digits w/no decimal
249 int digits = static_cast<int>(char_digits.length());
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());
259 else if(digits == 0) {
260 fs = 0; // just in case _atoi64 doesn't like an empty string
263 fs = _atoi64(char_digits.c_str());
266 if(digits >= precision) {
267 // drop excess digits
268 fs = boost::lexical_cast<boost::int64_t>(char_digits.substr(0, precision));
270 else if(digits == 0) {
271 fs = 0; // lexical_cast doesn't like empty strings
274 fs = boost::lexical_cast<boost::int64_t>(char_digits);
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);
291 return -time_duration(hours, min, sec, fs);
294 return time_duration(hours, min, sec, fs);
298 //! Parse time string of form YYYYMMDDThhmmss where T is delimeter between date and time
299 template<class time_type>
302 parse_iso_time(const std::string& s, char sep)
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;
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
321 if (p.match(itr, end, mr)) {
322 return time_type(static_cast<special_values>(mr.current_match));
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);
334 return time_type(d, td);
339 } }//namespace date_time