@@ -366,7 +366,56 @@ namespace QtCommon
366366 return result;
367367 }
368368
369- QString QtUtils::ClockToTimeUnit (double clk, int unit_type)
369+ static constexpr double kClockMultiplier = 1000000000.0 ;
370+
371+ // @brief Get precision based on scale.
372+ //
373+ // As the ruler is zoomed in, more precision will be needed between the graduations.
374+ //
375+ // @param [in] scale The time scale. The smaller the scale, the larger the precision needed.
376+ //
377+ // @return The precision, as in the number of decimal places needed.
378+ static int GetPrecision (double scale)
379+ {
380+ constexpr int kMinPrecision = 2 ;
381+ constexpr int kMaxPrecision = 6 ;
382+
383+ // Some error checking on the input.
384+ if (scale < 0.001 )
385+ {
386+ return kMaxPrecision ;
387+ }
388+
389+ double precision = std::log10 (kClockMultiplier / scale);
390+ if (precision < 2.0 )
391+ {
392+ return kMinPrecision ;
393+ }
394+ else if (precision > 6.0 )
395+ {
396+ return kMaxPrecision ;
397+ }
398+ return static_cast <int >(precision);
399+ }
400+
401+ // @brief Get the fractional part of the time passed in.
402+ //
403+ // The fractional part is any part coming after the decimal point when displaying larger time
404+ // values, for example 6,491ms may be displayed as 6.491s, so 491 would be returned.
405+ //
406+ // @param [in] time The time, in clock cycles
407+ // @param [in] scale The time scale. The smaller the scale, the larger the precision needed.
408+ //
409+ // @return The fractional part of the time.
410+ static uint64_t GetFraction (double time, int scale)
411+ {
412+ double power = pow (10 , scale);
413+ double time_scale = kClockMultiplier / power;
414+ uint64_t fraction = static_cast <uint64_t >(fmod ((time / time_scale), power));
415+ return fraction;
416+ }
417+
418+ QString QtUtils::ClockToTimeUnit (double clk, int unit_type, double scale)
370419 {
371420 double time = clk;
372421
@@ -397,31 +446,34 @@ namespace QtCommon
397446 case kTimeUnitTypeSecond :
398447 {
399448 out.setRealNumberPrecision (0 );
400- uint64_t secs = (uint64_t )(time / 1000000000.0 );
401- uint64_t ms = (uint64_t )fmod ((time / 10000000.0 ), 100 );
402- out << QString::number (secs).rightJustified (2 , ' 0' ) << " ." << QString::number (ms).rightJustified (2 , ' 0' ) << " s" ;
449+ uint64_t secs = static_cast <uint64_t >(time / kClockMultiplier );
450+ int precision = GetPrecision (scale);
451+ uint64_t fraction = GetFraction (time, precision);
452+ out << QString::number (secs).rightJustified (2 , ' 0' ) << " ." << QString::number (fraction).rightJustified (precision, ' 0' ) << " s" ;
403453 }
404454 break ;
405455
406456 case kTimeUnitTypeMinute :
407457 {
408458 out.setRealNumberPrecision (0 );
409- uint64_t mins = (uint64_t )(time / 60000000000.0 );
410- uint64_t secs = (uint64_t )fmod ((time / 1000000000.0 ), 60 );
411- uint64_t ms = (uint64_t )fmod ((time / 10000000.0 ), 100 );
412- out << mins << " m " << QString::number (secs).rightJustified (2 , ' 0' ) << " ." << QString::number (ms).rightJustified (2 , ' 0' ) << " s" ;
459+ uint64_t mins = static_cast <uint64_t >(time / 60000000000.0 );
460+ uint64_t secs = static_cast <uint64_t >(fmod ((time / kClockMultiplier ), 60 ));
461+ int precision = GetPrecision (scale);
462+ uint64_t fraction = GetFraction (time, precision);
463+ out << mins << " m " << QString::number (secs).rightJustified (2 , ' 0' ) << " ." << QString::number (fraction).rightJustified (precision, ' 0' ) << " s" ;
413464 }
414465 break ;
415466
416467 case kTimeUnitTypeHour :
417468 {
418469 out.setRealNumberPrecision (0 );
419- uint64_t hours = static_cast <uint64_t >(time / (60 * 60 * 1000000000.0 ));
420- uint64_t mins = static_cast <uint64_t >(fmod ((time / (60 * 1000000000.0 )), 60 ));
421- uint64_t secs = static_cast <uint64_t >(fmod ((time / 1000000000.0 ), 60 ));
422- uint64_t fraction = static_cast <uint64_t >(fmod ((time / 10000.0 ), 100000 ));
470+ uint64_t hours = static_cast <uint64_t >(time / (60 * 60 * kClockMultiplier ));
471+ uint64_t mins = static_cast <uint64_t >(fmod ((time / (60 * kClockMultiplier )), 60 ));
472+ uint64_t secs = static_cast <uint64_t >(fmod ((time / kClockMultiplier ), 60 ));
473+ int precision = GetPrecision (scale);
474+ uint64_t fraction = GetFraction (time, precision);
423475 out << hours << " :" << QString::number (mins).rightJustified (2 , ' 0' ) << " :" << QString::number (secs).rightJustified (2 , ' 0' ) << " ."
424- << QString::number (fraction).leftJustified ( 5 , ' 0' );
476+ << QString::number (fraction).rightJustified (precision , ' 0' );
425477 }
426478 break ;
427479
0 commit comments