comparison base/RealTime.cpp @ 1070:b8a788c9a6f1

Fixes to, and tests for, RealTime-to-text methods
author Chris Cannam
date Tue, 12 May 2015 16:19:45 +0100
parents a1cd5abcb38b
children c811991a5efa
comparison
equal deleted inserted replaced
1069:32ab6c48efaa 1070:b8a788c9a6f1
256 RealTime::toText(bool fixedDp) const 256 RealTime::toText(bool fixedDp) const
257 { 257 {
258 if (*this < RealTime::zeroTime) return "-" + (-*this).toText(fixedDp); 258 if (*this < RealTime::zeroTime) return "-" + (-*this).toText(fixedDp);
259 259
260 Preferences *p = Preferences::getInstance(); 260 Preferences *p = Preferences::getInstance();
261 bool hms = true;
262
261 if (p) { 263 if (p) {
264 hms = p->getShowHMS();
262 int fps = 0; 265 int fps = 0;
263 switch (p->getTimeToTextMode()) { 266 switch (p->getTimeToTextMode()) {
264 case Preferences::TimeToTextMs: break; 267 case Preferences::TimeToTextMs: break;
265 case Preferences::TimeToTextUs: fps = 1000000; break; 268 case Preferences::TimeToTextUs: fps = 1000000; break;
266 case Preferences::TimeToText24Frame: fps = 24; break; 269 case Preferences::TimeToText24Frame: fps = 24; break;
267 case Preferences::TimeToText25Frame: fps = 25; break; 270 case Preferences::TimeToText25Frame: fps = 25; break;
268 case Preferences::TimeToText30Frame: fps = 30; break; 271 case Preferences::TimeToText30Frame: fps = 30; break;
269 case Preferences::TimeToText50Frame: fps = 50; break; 272 case Preferences::TimeToText50Frame: fps = 50; break;
270 case Preferences::TimeToText60Frame: fps = 60; break; 273 case Preferences::TimeToText60Frame: fps = 60; break;
271 } 274 }
272 if (fps != 0) return toFrameText(fps); 275 if (fps != 0) return toFrameText(fps, hms);
273 } 276 }
274 277
275 std::stringstream out; 278 return toMSText(fixedDp, hms);
276 279 }
277 if (p->getShowHMS()) { 280
278 281 static void
282 writeSecPart(std::stringstream &out, bool hms, int sec)
283 {
284 if (hms) {
279 if (sec >= 3600) { 285 if (sec >= 3600) {
280 out << (sec / 3600) << ":"; 286 out << (sec / 3600) << ":";
281 } 287 }
282 288
283 if (sec >= 60) { 289 if (sec >= 60) {
284 out << (sec % 3600) / 60 << ":"; 290 int minutes = (sec % 3600) / 60;
291 if (sec >= 3600 && minutes < 10) out << "0";
292 out << minutes << ":";
285 } 293 }
286 294
287 if (sec >= 10) { 295 if (sec >= 10) {
288 out << ((sec % 60) / 10); 296 out << ((sec % 60) / 10);
289 } 297 }
291 out << (sec % 10); 299 out << (sec % 10);
292 300
293 } else { 301 } else {
294 out << sec; 302 out << sec;
295 } 303 }
304 }
305
306 std::string
307 RealTime::toMSText(bool fixedDp, bool hms) const
308 {
309 if (*this < RealTime::zeroTime) return "-" + (-*this).toMSText(fixedDp, hms);
310
311 std::stringstream out;
312
313 writeSecPart(out, hms, sec);
296 314
297 int ms = msec(); 315 int ms = msec();
298 316
299 if (ms != 0) { 317 if (ms != 0) {
300 out << "."; 318 out << ".";
319 337
320 return s; 338 return s;
321 } 339 }
322 340
323 std::string 341 std::string
324 RealTime::toFrameText(int fps) const 342 RealTime::toFrameText(int fps, bool hms) const
325 { 343 {
326 if (*this < RealTime::zeroTime) return "-" + (-*this).toFrameText(fps); 344 if (*this < RealTime::zeroTime) return "-" + (-*this).toFrameText(fps, hms);
327
328 Preferences *p = Preferences::getInstance();
329 345
330 std::stringstream out; 346 std::stringstream out;
331 347
332 if (p->getShowHMS()) { 348 writeSecPart(out, hms, sec);
333 349
334 if (sec >= 3600) { 350 // avoid rounding error if fps does not divide into ONE_BILLION
335 out << (sec / 3600) << ":"; 351 int64_t fbig = nsec;
336 } 352 fbig *= fps;
337 353 int f = int(fbig / ONE_BILLION);
338 if (sec >= 60) {
339 out << (sec % 3600) / 60 << ":";
340 }
341
342 if (sec >= 10) {
343 out << ((sec % 60) / 10);
344 }
345
346 out << (sec % 10);
347
348 } else {
349 out << sec;
350 }
351
352 int f = nsec / (ONE_BILLION / fps);
353 354
354 int div = 1; 355 int div = 1;
355 int n = fps - 1; 356 int n = fps - 1;
356 while ((n = n / 10)) { 357 while ((n = n / 10)) {
357 div *= 10; 358 div *= 10;
379 { 380 {
380 if (*this < RealTime::zeroTime) return "-" + (-*this).toSecText(); 381 if (*this < RealTime::zeroTime) return "-" + (-*this).toSecText();
381 382
382 std::stringstream out; 383 std::stringstream out;
383 384
384 if (sec >= 3600) { 385 writeSecPart(out, true, sec);
385 out << (sec / 3600) << ":";
386 }
387
388 if (sec >= 60) {
389 out << (sec % 3600) / 60 << ":";
390 }
391
392 if (sec >= 10) {
393 out << ((sec % 60) / 10);
394 }
395
396 out << (sec % 10);
397 386
398 if (sec < 60) { 387 if (sec < 60) {
399 out << "s"; 388 out << "s";
400 } 389 }
401 390