]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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/cstdint.hpp" | |
16 | #include <iostream> | |
17 | ||
18 | namespace boost { | |
19 | namespace date_time { | |
20 | ||
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> | |
25 | inline | |
26 | int_type power(int_type base, int_type exponent) | |
27 | { | |
28 | int_type result = 1; | |
29 | for(int i = 0; i < exponent; ++i){ | |
30 | result *= base; | |
31 | } | |
32 | return result; | |
33 | } | |
34 | ||
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 | |
39 | * truncated. | |
40 | * | |
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 "-:,.". | |
44 | */ | |
45 | template<class time_duration, class char_type> | |
46 | inline | |
47 | time_duration | |
48 | str_from_delimited_time_duration(const std::basic_string<char_type>& s) | |
49 | { | |
50 | unsigned short min=0, sec =0; | |
51 | int hour =0; | |
52 | bool is_neg = (s.at(0) == '-'); | |
53 | boost::int64_t fs=0; | |
54 | int pos = 0; | |
55 | ||
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; | |
64 | ||
65 | char_type sep_chars[5] = {'-',':',',','.'}; | |
66 | char_separator_type sep(sep_chars); | |
67 | tokenizer tok(s,sep); | |
68 | for(tokenizer_iterator beg=tok.begin(); beg!=tok.end();++beg){ | |
69 | switch(pos) { | |
70 | case 0: { | |
71 | hour = boost::lexical_cast<int>(*beg); | |
72 | break; | |
73 | } | |
74 | case 1: { | |
75 | min = boost::lexical_cast<unsigned short>(*beg); | |
76 | break; | |
77 | } | |
78 | case 2: { | |
79 | sec = boost::lexical_cast<unsigned short>(*beg); | |
80 | break; | |
81 | }; | |
82 | case 3: { | |
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) { | |
94 | // drop excess digits | |
95 | fs = _atoi64(beg->substr(0, precision).c_str()); | |
96 | } | |
97 | else { | |
98 | fs = _atoi64(beg->c_str()); | |
99 | } | |
100 | #else | |
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)); | |
105 | } | |
106 | else { | |
107 | fs = boost::lexical_cast<boost::int64_t>(*beg); | |
108 | } | |
109 | #endif | |
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); | |
115 | } | |
116 | ||
117 | break; | |
118 | } | |
119 | default: break; | |
120 | }//switch | |
121 | pos++; | |
122 | } | |
123 | if(is_neg) { | |
124 | return -time_duration(hour, min, sec, fs); | |
125 | } | |
126 | else { | |
127 | return time_duration(hour, min, sec, fs); | |
128 | } | |
129 | } | |
130 | ||
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 | |
135 | * truncated. | |
136 | * | |
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 "-:,.". | |
140 | */ | |
141 | template<class time_duration> | |
142 | inline | |
143 | time_duration | |
144 | parse_delimited_time_duration(const std::string& s) | |
145 | { | |
146 | return str_from_delimited_time_duration<time_duration,char>(s); | |
147 | } | |
148 | ||
149 | //! Utility function to split appart string | |
150 | inline | |
151 | bool | |
152 | split(const std::string& s, | |
153 | char sep, | |
154 | std::string& first, | |
155 | std::string& second) | |
156 | { | |
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); | |
161 | return true; | |
162 | } | |
163 | ||
164 | ||
165 | template<class time_type> | |
166 | inline | |
167 | time_type | |
168 | parse_delimited_time(const std::string& s, char sep) | |
169 | { | |
170 | typedef typename time_type::time_duration_type time_duration; | |
171 | typedef typename time_type::date_type date_type; | |
172 | ||
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); | |
180 | //construct a time | |
181 | return time_type(d, td); | |
182 | ||
183 | } | |
184 | ||
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> | |
187 | inline | |
188 | time_duration | |
189 | parse_undelimited_time_duration(const std::string& s) | |
190 | { | |
191 | int precision = 0; | |
192 | { | |
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(); | |
198 | } | |
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; | |
202 | int hours = 0; | |
203 | short min=0, sec=0; | |
204 | boost::int64_t fs=0; | |
205 | // increment one position if the string was "signed" | |
206 | if(s.at(sign) == '-') | |
207 | { | |
208 | ++sign; | |
209 | } | |
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){ | |
229 | switch(pos) { | |
230 | case 0: | |
231 | { | |
232 | hours = boost::lexical_cast<int>(*ti); | |
233 | break; | |
234 | } | |
235 | case 1: | |
236 | { | |
237 | min = boost::lexical_cast<short>(*ti); | |
238 | break; | |
239 | } | |
240 | case 2: | |
241 | { | |
242 | sec = boost::lexical_cast<short>(*ti); | |
243 | break; | |
244 | } | |
245 | case 3: | |
246 | { | |
247 | std::string char_digits(ti->substr(1)); // digits w/no decimal | |
248 | int digits = static_cast<int>(char_digits.length()); | |
249 | ||
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()); | |
257 | } | |
258 | else if(digits == 0) { | |
259 | fs = 0; // just in case _atoi64 doesn't like an empty string | |
260 | } | |
261 | else { | |
262 | fs = _atoi64(char_digits.c_str()); | |
263 | } | |
264 | #else | |
265 | if(digits >= precision) { | |
266 | // drop excess digits | |
267 | fs = boost::lexical_cast<boost::int64_t>(char_digits.substr(0, precision)); | |
268 | } | |
269 | else if(digits == 0) { | |
270 | fs = 0; // lexical_cast doesn't like empty strings | |
271 | } | |
272 | else { | |
273 | fs = boost::lexical_cast<boost::int64_t>(char_digits); | |
274 | } | |
275 | #endif | |
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); | |
281 | } | |
282 | ||
283 | break; | |
284 | } | |
285 | default: break; | |
286 | }; | |
287 | pos++; | |
288 | } | |
289 | if(sign) { | |
290 | return -time_duration(hours, min, sec, fs); | |
291 | } | |
292 | else { | |
293 | return time_duration(hours, min, sec, fs); | |
294 | } | |
295 | } | |
296 | ||
297 | //! Parse time string of form YYYYMMDDThhmmss where T is delimeter between date and time | |
298 | template<class time_type> | |
299 | inline | |
300 | time_type | |
301 | parse_iso_time(const std::string& s, char sep) | |
302 | { | |
303 | typedef typename time_type::time_duration_type time_duration; | |
304 | typedef typename time_type::date_type date_type; | |
305 | ||
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); | |
313 | //construct a time | |
314 | return time_type(d, td); | |
315 | } | |
316 | ||
317 | ||
318 | ||
319 | } }//namespace date_time | |
320 | ||
321 | ||
322 | ||
323 | ||
324 | #endif |