From: Jens Steinhauser Date: Sun, 30 Aug 2015 21:11:31 +0000 (+0200) Subject: format_time(): Use the timestamp type in the calculation X-Git-Tag: pulseview-0.3.0~124 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=af95045e903a0d2ac5aac63095eb124038be40c9;p=pulseview.git format_time(): Use the timestamp type in the calculation --- diff --git a/pv/util.cpp b/pv/util.cpp index 78b32af7..cf2d7f90 100644 --- a/pv/util.cpp +++ b/pv/util.cpp @@ -45,7 +45,6 @@ static const QString SIPrefixes[17] = { "Z", "Y"}; const int EmptySIPrefix = 8; const int FirstSIPrefixPower = -(EmptySIPrefix * 3); -const double MinTimeDelta = 1e-15; // Anything below 1 fs can be considered zero // Insert the timestamp value into the stream in fixed-point notation // (and honor the precision) @@ -132,13 +131,13 @@ static QString pad_number(unsigned int number, int length) return QString("%1").arg(number, length, 10, QChar('0')); } -static QString format_time_in_full(double t, signed precision) +static QString format_time_in_full(const Timestamp& t, signed precision) { - const unsigned int whole_seconds = abs((int) t); - const unsigned int days = whole_seconds / (60 * 60 * 24); - const unsigned int hours = (whole_seconds / (60 * 60)) % 24; - const unsigned int minutes = (whole_seconds / 60) % 60; - const unsigned int seconds = whole_seconds % 60; + const Timestamp whole_seconds = floor(abs(t)); + const Timestamp days = floor(whole_seconds / (60 * 60 * 24)); + const unsigned int hours = fmod(whole_seconds / (60 * 60), 24).convert_to(); + const unsigned int minutes = fmod(whole_seconds / 60, 60).convert_to(); + const unsigned int seconds = fmod(whole_seconds, 60).convert_to(); QString s; QTextStream ts(&s); @@ -151,7 +150,7 @@ static QString format_time_in_full(double t, signed precision) bool use_padding = false; if (days) { - ts << days << ":"; + ts << days.str().c_str() << ":"; use_padding = true; } @@ -175,11 +174,16 @@ static QString format_time_in_full(double t, signed precision) if (precision >= 0) { ts << pad_number(seconds, use_padding ? 2 : 0); - const double fraction = fabs(t) - whole_seconds; + const Timestamp fraction = fabs(t) - whole_seconds; if (precision > 0 && precision < 1000) { - QString fs = QString("%1").arg(fraction, -(2 + precision), 'f', - precision, QChar('0')); + std::ostringstream ss; + ss + << std::fixed + << std::setprecision(2 + precision) + << std::setfill('0') + << fraction; + std::string fs = ss.str(); ts << "."; @@ -188,7 +192,7 @@ static QString format_time_in_full(double t, signed precision) // Start at index 2 to skip the "0." at the beginning ts << fs[1 + i]; - if ((i > 0) && (i % 3 == 0)) + if ((i > 0) && (i % 3 == 0) && (i != precision)) ts << " "; } } @@ -197,8 +201,8 @@ static QString format_time_in_full(double t, signed precision) return s; } -static QString format_time_with_si(double t, QString unit, int prefix, - unsigned int precision) +static QString format_time_with_si(const Timestamp& t, QString unit, + int prefix, unsigned int precision) { // The precision is always given without taking the prefix into account // so we need to deduct the number of decimals the prefix might imply @@ -212,10 +216,11 @@ static QString format_time_with_si(double t, QString unit, int prefix, return format_si_value(t, unit, prefix, relative_prec); } -static QString format_time(double t, int prefix, TimeUnit unit, unsigned int precision) +QString format_time(const Timestamp& t, int prefix, TimeUnit unit, + unsigned int precision) { // Make 0 appear as 0, not random +0 or -0 - if (fabs(t) < MinTimeDelta) + if (t.is_zero()) return "0"; // If we have to use samples then we have no alternative formats @@ -232,11 +237,6 @@ static QString format_time(double t, int prefix, TimeUnit unit, unsigned int pre return format_time_in_full(t, precision); } -QString format_time(const Timestamp& t, int prefix, TimeUnit unit, unsigned int precision) -{ - return format_time(t.convert_to(), prefix, unit, precision); -} - QString format_second(const Timestamp& second) { return format_si_value(second, "s", -1, 0, false); diff --git a/test/util.cpp b/test/util.cpp index 1e33790b..ce9b64d2 100644 --- a/test/util.cpp +++ b/test/util.cpp @@ -141,4 +141,52 @@ BOOST_AUTO_TEST_CASE(format_si_value_test) BOOST_CHECK_EQUAL(format_si_value(ts("1"), "V", 8, 0, false), "1 V"); } +BOOST_AUTO_TEST_CASE(format_time_test) +{ + BOOST_CHECK_EQUAL(format_time(ts("-0.00005"), 6, Time, 5), QString("-50 ") + mu + "s"); + BOOST_CHECK_EQUAL(format_time(ts( "0.00005"), 6, Time, 5), QString("+50 ") + mu + "s"); + BOOST_CHECK_EQUAL(format_time(ts( "1")), "+1 s"); + BOOST_CHECK_EQUAL(format_time(ts("-1")), "-1 s"); + BOOST_CHECK_EQUAL(format_time(ts( "100")), "+1:40"); + BOOST_CHECK_EQUAL(format_time(ts("-100")), "-1:40"); + BOOST_CHECK_EQUAL(format_time(ts( "4000")), "+1:06:40"); + BOOST_CHECK_EQUAL(format_time(ts("-4000")), "-1:06:40"); + BOOST_CHECK_EQUAL(format_time(ts("12000"), 9, Time, 0), "+3:20:00"); + BOOST_CHECK_EQUAL(format_time(ts("15000"), 9, Time, 0), "+4:10:00"); + BOOST_CHECK_EQUAL(format_time(ts("20000"), 9, Time, 0), "+5:33:20"); + BOOST_CHECK_EQUAL(format_time(ts("25000"), 9, Time, 0), "+6:56:40"); + + BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 0), "+123:04:05:06"); + BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 1), "+123:04:05:06.0"); + BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 2), "+123:04:05:06.00"); + BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 3), "+123:04:05:06.007"); + BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 4), "+123:04:05:06.007 0"); + BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 5), "+123:04:05:06.007 00"); + BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 6), "+123:04:05:06.007 008"); + BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 7), "+123:04:05:06.007 008 0"); + BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 8), "+123:04:05:06.007 008 00"); + BOOST_CHECK_EQUAL(format_time(ts("10641906.007008009"), 0, Time, 9), "+123:04:05:06.007 008 009"); + + BOOST_CHECK_EQUAL(format_time(ts("-1.5"), 7), "-1500 ms"); + BOOST_CHECK_EQUAL(format_time(ts("-1.0"), 7), "-1000 ms"); + BOOST_CHECK_EQUAL(format_time(ts("-0.2")), "-200 ms"); + BOOST_CHECK_EQUAL(format_time(ts("-0.1")), "-100 ms"); + BOOST_CHECK_EQUAL(format_time(ts("0.0")), "0"); + BOOST_CHECK_EQUAL(format_time(ts("0.1")), "+100 ms"); + BOOST_CHECK_EQUAL(format_time(ts("0.2")), "+200 ms"); + BOOST_CHECK_EQUAL(format_time(ts("0.3")), "+300 ms"); + BOOST_CHECK_EQUAL(format_time(ts("0.4")), "+400 ms"); + BOOST_CHECK_EQUAL(format_time(ts("0.5")), "+500 ms"); + BOOST_CHECK_EQUAL(format_time(ts("0.6")), "+600 ms"); + BOOST_CHECK_EQUAL(format_time(ts("0.7")), "+700 ms"); + BOOST_CHECK_EQUAL(format_time(ts("0.8")), "+800 ms"); + BOOST_CHECK_EQUAL(format_time(ts("0.9")), "+900 ms"); + BOOST_CHECK_EQUAL(format_time(ts("1.0"), 7), "+1000 ms"); + BOOST_CHECK_EQUAL(format_time(ts("1.1"), 7), "+1100 ms"); + BOOST_CHECK_EQUAL(format_time(ts("1.2"), 7), "+1200 ms"); + BOOST_CHECK_EQUAL(format_time(ts("1.3"), 7), "+1300 ms"); + BOOST_CHECK_EQUAL(format_time(ts("1.4"), 7), "+1400 ms"); + BOOST_CHECK_EQUAL(format_time(ts("1.5"), 7), "+1500 ms"); +} + BOOST_AUTO_TEST_SUITE_END()