Mercurial > hg > svcore
comparison base/RealTimeSV.cpp @ 1527:710e6250a401 zoom
Merge from default branch
author | Chris Cannam |
---|---|
date | Mon, 17 Sep 2018 13:51:14 +0100 |
parents | 48e9f538e6e9 |
children | 71207822a7e0 |
comparison
equal
deleted
inserted
replaced
1324:d4a28d1479a8 | 1527:710e6250a401 |
---|---|
17 Rosegarden MIDI and audio sequencer and notation editor. | 17 Rosegarden MIDI and audio sequencer and notation editor. |
18 This file copyright 2000-2006 Chris Cannam. | 18 This file copyright 2000-2006 Chris Cannam. |
19 */ | 19 */ |
20 | 20 |
21 #include <iostream> | 21 #include <iostream> |
22 #include <limits.h> | |
22 | 23 |
23 #include <cstdlib> | 24 #include <cstdlib> |
24 #include <sstream> | 25 #include <sstream> |
25 | 26 |
26 #include "RealTime.h" | 27 #include "RealTime.h" |
41 #define ONE_BILLION 1000000000 | 42 #define ONE_BILLION 1000000000 |
42 | 43 |
43 RealTime::RealTime(int s, int n) : | 44 RealTime::RealTime(int s, int n) : |
44 sec(s), nsec(n) | 45 sec(s), nsec(n) |
45 { | 46 { |
46 if (sec == 0) { | 47 while (nsec <= -ONE_BILLION && sec > INT_MIN) { nsec += ONE_BILLION; --sec; } |
47 while (nsec <= -ONE_BILLION) { nsec += ONE_BILLION; --sec; } | 48 while (nsec >= ONE_BILLION && sec < INT_MAX) { nsec -= ONE_BILLION; ++sec; } |
48 while (nsec >= ONE_BILLION) { nsec -= ONE_BILLION; ++sec; } | 49 while (nsec > 0 && sec < 0) { nsec -= ONE_BILLION; ++sec; } |
49 } else if (sec < 0) { | 50 while (nsec < 0 && sec > 0) { nsec += ONE_BILLION; --sec; } |
50 while (nsec <= -ONE_BILLION) { nsec += ONE_BILLION; --sec; } | |
51 while (nsec > 0 && sec < 0) { nsec -= ONE_BILLION; ++sec; } | |
52 } else { | |
53 while (nsec >= ONE_BILLION) { nsec -= ONE_BILLION; ++sec; } | |
54 while (nsec < 0 && sec > 0) { nsec += ONE_BILLION; --sec; } | |
55 } | |
56 } | 51 } |
57 | 52 |
58 RealTime | 53 RealTime |
59 RealTime::fromSeconds(double sec) | 54 RealTime::fromSeconds(double sec) |
60 { | 55 { |
172 } | 167 } |
173 | 168 |
174 std::ostream &operator<<(std::ostream &out, const RealTime &rt) | 169 std::ostream &operator<<(std::ostream &out, const RealTime &rt) |
175 { | 170 { |
176 if (rt < RealTime::zeroTime) { | 171 if (rt < RealTime::zeroTime) { |
177 out << "-"; | 172 out << "-"; |
178 } else { | 173 } else { |
179 out << " "; | 174 out << " "; |
180 } | 175 } |
181 | 176 |
182 int s = (rt.sec < 0 ? -rt.sec : rt.sec); | 177 int s = (rt.sec < 0 ? -rt.sec : rt.sec); |
183 int n = (rt.nsec < 0 ? -rt.nsec : rt.nsec); | 178 int n = (rt.nsec < 0 ? -rt.nsec : rt.nsec); |
184 | 179 |
185 out << s << "."; | 180 out << s << "."; |
186 | 181 |
187 int nn(n); | 182 int nn(n); |
188 if (nn == 0) out << "00000000"; | 183 if (nn == 0) out << "00000000"; |
189 else while (nn < (ONE_BILLION / 10)) { | 184 else while (nn < (ONE_BILLION / 10)) { |
190 out << "0"; | 185 out << "0"; |
191 nn *= 10; | 186 nn *= 10; |
192 } | 187 } |
193 | 188 |
194 out << n << "R"; | 189 out << n << "R"; |
195 return out; | 190 return out; |
196 } | 191 } |
317 writeSecPart(out, hms, sec); | 312 writeSecPart(out, hms, sec); |
318 | 313 |
319 int ms = msec(); | 314 int ms = msec(); |
320 | 315 |
321 if (ms != 0) { | 316 if (ms != 0) { |
322 out << "."; | 317 out << "."; |
323 out << (ms / 100); | 318 out << (ms / 100); |
324 ms = ms % 100; | 319 ms = ms % 100; |
325 if (ms != 0) { | 320 if (ms != 0) { |
326 out << (ms / 10); | 321 out << (ms / 10); |
327 ms = ms % 10; | 322 ms = ms % 10; |
328 } else if (fixedDp) { | 323 } else if (fixedDp) { |
329 out << "0"; | 324 out << "0"; |
330 } | 325 } |
331 if (ms != 0) { | 326 if (ms != 0) { |
332 out << ms; | 327 out << ms; |
333 } else if (fixedDp) { | 328 } else if (fixedDp) { |
334 out << "0"; | 329 out << "0"; |
335 } | 330 } |
336 } else if (fixedDp) { | 331 } else if (fixedDp) { |
337 out << ".000"; | 332 out << ".000"; |
338 } | 333 } |
339 | 334 |
340 std::string s = out.str(); | 335 std::string s = out.str(); |
341 | 336 |
342 return s; | 337 return s; |
343 } | 338 } |
344 | 339 |
369 while (div) { | 364 while (div) { |
370 int d = (f / div) % 10; | 365 int d = (f / div) % 10; |
371 out << d; | 366 out << d; |
372 div /= 10; | 367 div /= 10; |
373 } | 368 } |
374 | 369 |
375 std::string s = out.str(); | 370 std::string s = out.str(); |
376 | 371 |
377 // cerr << "converted " << toString() << " to " << s << endl; | 372 // cerr << "converted " << toString() << " to " << s << endl; |
378 | 373 |
379 return s; | 374 return s; |