comparison base/RealTime.cpp @ 612:75f154085a4d

* Add time display format preference
author Chris Cannam
date Fri, 25 Sep 2009 12:02:22 +0000
parents 81963c51b488
children 06f13a3b9e9e
comparison
equal deleted inserted replaced
611:dd97f7b3d120 612:75f154085a4d
19 */ 19 */
20 20
21 #include <iostream> 21 #include <iostream>
22 22
23 #include <cstdlib> 23 #include <cstdlib>
24
25 #if (__GNUC__ < 3)
26 #include <strstream>
27 #define stringstream strstream
28 #else
29 #include <sstream> 24 #include <sstream>
30 #endif
31 25
32 using std::cerr; 26 using std::cerr;
33 using std::endl; 27 using std::endl;
34 28
35 #include "RealTime.h" 29 #include "RealTime.h"
36 #include "sys/time.h" 30 #include "sys/time.h"
31
32 #include "Preferences.h"
37 33
38 // A RealTime consists of two ints that must be at least 32 bits each. 34 // A RealTime consists of two ints that must be at least 32 bits each.
39 // A signed 32-bit int can store values exceeding +/- 2 billion. This 35 // A signed 32-bit int can store values exceeding +/- 2 billion. This
40 // means we can safely use our lower int for nanoseconds, as there are 36 // means we can safely use our lower int for nanoseconds, as there are
41 // 1 billion nanoseconds in a second and we need to handle double that 37 // 1 billion nanoseconds in a second and we need to handle double that
195 RealTime::toString(bool align) const 191 RealTime::toString(bool align) const
196 { 192 {
197 std::stringstream out; 193 std::stringstream out;
198 out << *this; 194 out << *this;
199 195
200 #if (__GNUC__ < 3)
201 out << std::ends;
202 #endif
203
204 std::string s = out.str(); 196 std::string s = out.str();
205 197
206 if (!align && *this >= RealTime::zeroTime) { 198 if (!align && *this >= RealTime::zeroTime) {
207 // remove leading " " 199 // remove leading " "
208 s = s.substr(1, s.length() - 1); 200 s = s.substr(1, s.length() - 1);
258 250
259 std::string 251 std::string
260 RealTime::toText(bool fixedDp) const 252 RealTime::toText(bool fixedDp) const
261 { 253 {
262 if (*this < RealTime::zeroTime) return "-" + (-*this).toText(fixedDp); 254 if (*this < RealTime::zeroTime) return "-" + (-*this).toText(fixedDp);
255
256 Preferences *p = Preferences::getInstance();
257 if (p) {
258 int fps = 0;
259 switch (p->getTimeToTextMode()) {
260 case Preferences::TimeToTextMs: break;
261 case Preferences::TimeToTextUs: fps = 1000000; break;
262 case Preferences::TimeToText24Frame: fps = 24; break;
263 case Preferences::TimeToText25Frame: fps = 25; break;
264 case Preferences::TimeToText30Frame: fps = 30; break;
265 case Preferences::TimeToText50Frame: fps = 50; break;
266 case Preferences::TimeToText60Frame: fps = 60; break;
267 }
268 if (fps != 0) return toFrameText(fps);
269 }
263 270
264 std::stringstream out; 271 std::stringstream out;
265 272
266 if (sec >= 3600) { 273 if (sec >= 3600) {
267 out << (sec / 3600) << ":"; 274 out << (sec / 3600) << ":";
296 } 303 }
297 } else if (fixedDp) { 304 } else if (fixedDp) {
298 out << ".000"; 305 out << ".000";
299 } 306 }
300 307
301 #if (__GNUC__ < 3)
302 out << std::ends;
303 #endif
304
305 std::string s = out.str(); 308 std::string s = out.str();
309
310 return s;
311 }
312
313 std::string
314 RealTime::toFrameText(int fps) const
315 {
316 if (*this < RealTime::zeroTime) return "-" + (-*this).toFrameText(fps);
317
318 std::stringstream out;
319
320 if (sec >= 3600) {
321 out << (sec / 3600) << ":";
322 }
323
324 if (sec >= 60) {
325 out << (sec % 3600) / 60 << ":";
326 }
327
328 if (sec >= 10) {
329 out << ((sec % 60) / 10);
330 }
331
332 out << (sec % 10);
333
334 int f = nsec / (ONE_BILLION / fps);
335
336 int div = 1;
337 int n = fps - 1;
338 while ((n = n / 10)) {
339 div *= 10;
340 }
341
342 out << ":";
343
344 std::cerr << "div = " << div << ", f = "<< f << std::endl;
345
346 while (div) {
347 int d = (f / div) % 10;
348 out << d;
349 div /= 10;
350 }
351
352 std::string s = out.str();
353
354 std::cerr << "converted " << toString() << " to " << s << std::endl;
306 355
307 return s; 356 return s;
308 } 357 }
309 358
310 std::string 359 std::string
329 out << (sec % 10); 378 out << (sec % 10);
330 379
331 if (sec < 60) { 380 if (sec < 60) {
332 out << "s"; 381 out << "s";
333 } 382 }
334
335
336 #if (__GNUC__ < 3)
337 out << std::ends;
338 #endif
339 383
340 std::string s = out.str(); 384 std::string s = out.str();
341 385
342 return s; 386 return s;
343 } 387 }