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/cstdint.hpp"
21 //! computes exponential math like 2^8 => 256, only works with positive integers
22 //Not general purpose, but needed b/c std::pow is not available
23 //everywehere. Hasn't been tested with negatives and zeros
24 template<class int_type>
26 int_type power(int_type base, int_type exponent)
29 for(int i = 0; i < exponent; ++i){
35 //! Creates a time_duration object from a delimited string
36 /*! Expected format for string is "[-]h[h][:mm][:ss][.fff]".
37 * If the number of fractional digits provided is greater than the
38 * precision of the time duration type then the extra digits are
41 * A negative duration will be created if the first character in
42 * string is a '-', all other '-' will be treated as delimiters.
43 * Accepted delimiters are "-:,.".
45 template<class time_duration, class char_type>
48 str_from_delimited_time_duration(const std::basic_string<char_type>& s)
50 unsigned short min=0, sec =0;
52 bool is_neg = (s.at(0) == '-');
56 typedef typename std::basic_string<char_type>::traits_type traits_type;
57 typedef boost::char_separator<char_type, traits_type> char_separator_type;
58 typedef boost::tokenizer<char_separator_type,
59 typename std::basic_string<char_type>::const_iterator,
60 std::basic_string<char_type> > tokenizer;
61 typedef typename boost::tokenizer<char_separator_type,
62 typename std::basic_string<char_type>::const_iterator,
63 typename std::basic_string<char_type> >::iterator tokenizer_iterator;
65 char_type sep_chars[5] = {'-',':',',','.'};
66 char_separator_type sep(sep_chars);
68 for(tokenizer_iterator beg=tok.begin(); beg!=tok.end();++beg){
71 hour = boost::lexical_cast<int>(*beg);
75 min = boost::lexical_cast<unsigned short>(*beg);
79 sec = boost::lexical_cast<unsigned short>(*beg);
83 int digits = static_cast<int>(beg->length());
84 //Works around a bug in MSVC 6 library that does not support
85 //operator>> thus meaning lexical_cast will fail to compile.
86 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
87 // msvc wouldn't compile 'time_duration::num_fractional_digits()'
88 // (required template argument list) as a workaround a temp
89 // time_duration object was used
90 time_duration td(hour,min,sec,fs);
91 int precision = td.num_fractional_digits();
92 // _atoi64 is an MS specific function
93 if(digits >= precision) {
95 fs = _atoi64(beg->substr(0, precision).c_str());
98 fs = _atoi64(beg->c_str());
101 int precision = time_duration::num_fractional_digits();
102 if(digits >= precision) {
103 // drop excess digits
104 fs = boost::lexical_cast<boost::int64_t>(beg->substr(0, precision));
107 fs = boost::lexical_cast<boost::int64_t>(*beg);
110 if(digits < precision){
111 // trailing zeros get dropped from the string,
112 // "1:01:01.1" would yield .000001 instead of .100000
113 // the power() compensates for the missing decimal places
114 fs *= power(10, precision - digits);
124 return -time_duration(hour, min, sec, fs);
127 return time_duration(hour, min, sec, fs);
131 //! Creates a time_duration object from a delimited string
132 /*! Expected format for string is "[-]h[h][:mm][:ss][.fff]".
133 * If the number of fractional digits provided is greater than the
134 * precision of the time duration type then the extra digits are
137 * A negative duration will be created if the first character in
138 * string is a '-', all other '-' will be treated as delimiters.
139 * Accepted delimiters are "-:,.".
141 template<class time_duration>
144 parse_delimited_time_duration(const std::string& s)
146 return str_from_delimited_time_duration<time_duration,char>(s);
149 //! Utility function to split appart string
152 split(const std::string& s,
157 std::string::size_type sep_pos = s.find(sep);
158 first = s.substr(0,sep_pos);
159 if (sep_pos!=std::string::npos)
160 second = s.substr(sep_pos+1);
165 template<class time_type>
168 parse_delimited_time(const std::string& s, char sep)
170 typedef typename time_type::time_duration_type time_duration;
171 typedef typename time_type::date_type date_type;
173 //split date/time on a unique delimiter char such as ' ' or 'T'
174 std::string date_string, tod_string;
175 split(s, sep, date_string, tod_string);
176 //call parse_date with first string
177 date_type d = parse_date<date_type>(date_string);
178 //call parse_time_duration with remaining string
179 time_duration td = parse_delimited_time_duration<time_duration>(tod_string);
181 return time_type(d, td);
185 //! Parse time duration part of an iso time of form: [-]hhmmss[.fff...] (eg: 120259.123 is 12 hours, 2 min, 59 seconds, 123000 microseconds)
186 template<class time_duration>
189 parse_undelimited_time_duration(const std::string& s)
193 // msvc wouldn't compile 'time_duration::num_fractional_digits()'
194 // (required template argument list) as a workaround, a temp
195 // time_duration object was used
196 time_duration tmp(0,0,0,1);
197 precision = tmp.num_fractional_digits();
199 // 'precision+1' is so we grab all digits, plus the decimal
200 int offsets[] = {2,2,2, precision+1};
201 int pos = 0, sign = 0;
205 // increment one position if the string was "signed"
206 if(s.at(sign) == '-')
210 // stlport choked when passing s.substr() to tokenizer
211 // using a new string fixed the error
212 std::string remain = s.substr(sign);
213 /* We do not want the offset_separator to wrap the offsets, we
214 * will never want to process more than:
215 * 2 char, 2 char, 2 char, frac_sec length.
216 * We *do* want the offset_separator to give us a partial for the
217 * last characters if there were not enough provided in the input string. */
218 bool wrap_off = false;
219 bool ret_part = true;
220 boost::offset_separator osf(offsets, offsets+4, wrap_off, ret_part);
221 typedef boost::tokenizer<boost::offset_separator,
222 std::basic_string<char>::const_iterator,
223 std::basic_string<char> > tokenizer;
224 typedef boost::tokenizer<boost::offset_separator,
225 std::basic_string<char>::const_iterator,
226 std::basic_string<char> >::iterator tokenizer_iterator;
227 tokenizer tok(remain, osf);
228 for(tokenizer_iterator ti=tok.begin(); ti!=tok.end();++ti){
232 hours = boost::lexical_cast<int>(*ti);
237 min = boost::lexical_cast<short>(*ti);
242 sec = boost::lexical_cast<short>(*ti);
247 std::string char_digits(ti->substr(1)); // digits w/no decimal
248 int digits = static_cast<int>(char_digits.length());
250 //Works around a bug in MSVC 6 library that does not support
251 //operator>> thus meaning lexical_cast will fail to compile.
252 #if (defined(BOOST_MSVC) && (_MSC_VER <= 1200)) // 1200 == VC++ 6.0
253 // _atoi64 is an MS specific function
254 if(digits >= precision) {
255 // drop excess digits
256 fs = _atoi64(char_digits.substr(0, precision).c_str());
258 else if(digits == 0) {
259 fs = 0; // just in case _atoi64 doesn't like an empty string
262 fs = _atoi64(char_digits.c_str());
265 if(digits >= precision) {
266 // drop excess digits
267 fs = boost::lexical_cast<boost::int64_t>(char_digits.substr(0, precision));
269 else if(digits == 0) {
270 fs = 0; // lexical_cast doesn't like empty strings
273 fs = boost::lexical_cast<boost::int64_t>(char_digits);
276 if(digits < precision){
277 // trailing zeros get dropped from the string,
278 // "1:01:01.1" would yield .000001 instead of .100000
279 // the power() compensates for the missing decimal places
280 fs *= power(10, precision - digits);
290 return -time_duration(hours, min, sec, fs);
293 return time_duration(hours, min, sec, fs);
297 //! Parse time string of form YYYYMMDDThhmmss where T is delimeter between date and time
298 template<class time_type>
301 parse_iso_time(const std::string& s, char sep)
303 typedef typename time_type::time_duration_type time_duration;
304 typedef typename time_type::date_type date_type;
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);
314 return time_type(d, td);
319 } }//namespace date_time