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