Mercurial > hg > svcore
comparison base/RealTime.cpp @ 383:94fc0591ea43 1.2-stable
* merge from trunk (1.2 ended up being tracked from trunk, but we may want
this branch for fixes later)
author | Chris Cannam |
---|---|
date | Wed, 27 Feb 2008 10:32:45 +0000 |
parents | 21b9b25bff48 |
children |
comparison
equal
deleted
inserted
replaced
349:f39d33b0b265 | 383:94fc0591ea43 |
---|---|
118 s = s.substr(1, s.length() - 1); | 118 s = s.substr(1, s.length() - 1); |
119 } | 119 } |
120 | 120 |
121 // remove trailing R | 121 // remove trailing R |
122 return s.substr(0, s.length() - 1); | 122 return s.substr(0, s.length() - 1); |
123 } | |
124 | |
125 RealTime | |
126 RealTime::fromString(std::string s) | |
127 { | |
128 bool negative = false; | |
129 bool faulty = false; | |
130 bool section = 0; | |
131 std::string ssec, snsec; | |
132 | |
133 for (size_t i = 0; i < s.length(); ++i) { | |
134 | |
135 char c = s[i]; | |
136 if (isspace(c)) continue; | |
137 | |
138 if (section == 0) { | |
139 | |
140 if (c == '-') negative = true; | |
141 else if (isdigit(c)) { section = 1; ssec += c; } | |
142 else if (c == '.') section = 2; | |
143 else break; | |
144 | |
145 } else if (section == 1) { | |
146 | |
147 if (c == '.') section = 2; | |
148 else if (isdigit(c)) ssec += c; | |
149 else break; | |
150 | |
151 } else if (section == 2) { | |
152 | |
153 if (isdigit(c)) snsec += c; | |
154 else break; | |
155 } | |
156 } | |
157 | |
158 while (snsec.length() < 8) snsec += '0'; | |
159 | |
160 int sec = atoi(ssec.c_str()); | |
161 int nsec = atoi(snsec.c_str()); | |
162 if (negative) sec = -sec; | |
163 | |
164 std::cerr << "RealTime::fromString: string " << s << " -> " | |
165 << sec << " sec, " << nsec << " nsec" << std::endl; | |
166 | |
167 return RealTime(sec, nsec); | |
123 } | 168 } |
124 | 169 |
125 std::string | 170 std::string |
126 RealTime::toText(bool fixedDp) const | 171 RealTime::toText(bool fixedDp) const |
127 { | 172 { |
225 double nsecdiv = (double(nsec) + ONE_BILLION * double(secrem)) / d; | 270 double nsecdiv = (double(nsec) + ONE_BILLION * double(secrem)) / d; |
226 | 271 |
227 return RealTime(secdiv, int(nsecdiv + 0.5)); | 272 return RealTime(secdiv, int(nsecdiv + 0.5)); |
228 } | 273 } |
229 | 274 |
275 RealTime | |
276 RealTime::operator*(double m) const | |
277 { | |
278 double t = (double(nsec) / ONE_BILLION) * m; | |
279 t += sec * m; | |
280 return fromSeconds(t); | |
281 } | |
282 | |
283 RealTime | |
284 RealTime::operator/(double d) const | |
285 { | |
286 double t = (double(nsec) / ONE_BILLION) / d; | |
287 t += sec / d; | |
288 return fromSeconds(t); | |
289 } | |
290 | |
230 double | 291 double |
231 RealTime::operator/(const RealTime &r) const | 292 RealTime::operator/(const RealTime &r) const |
232 { | 293 { |
233 double lTotal = double(sec) * ONE_BILLION + double(nsec); | 294 double lTotal = double(sec) * ONE_BILLION + double(nsec); |
234 double rTotal = double(r.sec) * ONE_BILLION + double(r.nsec); | 295 double rTotal = double(r.sec) * ONE_BILLION + double(r.nsec); |