]> sigrok.org Git - pulseview.git/blobdiff - pv/util.cpp
format_time(): Use the timestamp type in the calculation
[pulseview.git] / pv / util.cpp
index 2e54adeaed3dffae88cb2b7354ce9eb57395302b..cf2d7f90bfcf9761eb4cef4a6b5eb38a7e2c43e3 100644 (file)
@@ -24,6 +24,9 @@
 
 #include <assert.h>
 
+#include <algorithm>
+#include <sstream>
+
 #include <QTextStream>
 #include <QDebug>
 
@@ -32,48 +35,209 @@ using namespace Qt;
 namespace pv {
 namespace util {
 
-static const QString SIPrefixes[17] =
-       {"y", "z", "a", "f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G",
-       "T", "P", "E", "Z", "Y"};
-const int FirstSIPrefixPower = -24;
+static const QString SIPrefixes[17] = {
+       "y", "z",
+       "a", "f", "p",
+       "n", QChar(0x03BC), "m",
+       "",
+       "k", "M", "G",
+       "T", "P", "E",
+       "Z", "Y"};
+const int EmptySIPrefix = 8;
+const int FirstSIPrefixPower = -(EmptySIPrefix * 3);
+
+// Insert the timestamp value into the stream in fixed-point notation
+// (and honor the precision)
+static QTextStream& operator<<(QTextStream& stream, const Timestamp& t)
+{
+       // The multiprecision types already have a function and a stream insertion
+       // operator to convert them to a string, however these functions abuse a
+       // precision value of zero to print all available decimal places instead of
+       // none, and the boost authors refuse to fix this because they don't want
+       // to break buggy code that relies on this bug.
+       // (https://svn.boost.org/trac/boost/ticket/10103)
+       // Therefore we have to work around the case where precision is zero.
+
+       int precision = stream.realNumberPrecision();
+
+       std::ostringstream ss;
+       ss << std::fixed;
+
+       if (stream.numberFlags() & QTextStream::ForceSign) {
+               ss << std::showpos;
+       }
 
-QString format_si_value(double v, QString unit, int prefix,
+       if (0 == precision) {
+               ss
+                       << std::setprecision(1)
+                       << round(t);
+       } else {
+               ss
+                       << std::setprecision(precision)
+                       << t;
+       }
+
+       std::string str(ss.str());
+       if (0 == precision) {
+               // remove the separator and the unwanted decimal place
+               str.resize(str.size() - 2);
+       }
+
+       return stream << QString::fromStdString(str);
+}
+
+QString format_si_value(const Timestamp& v, QString unit, int prefix,
        unsigned int precision, bool sign)
 {
        if (prefix < 0) {
-               int exp = -FirstSIPrefixPower;
-
-               prefix = 0;
-               while ((fabs(v) * pow(10.0, exp)) > 999.0 &&
-                       prefix < (int)(countof(SIPrefixes) - 1)) {
-                       prefix++;
-                       exp -= 3;
+               // No prefix given, calculate it
+
+               if (v.is_zero()) {
+                       prefix = EmptySIPrefix;
+               } else {
+                       int exp = -FirstSIPrefixPower;
+
+                       prefix = 0;
+                       while ((fabs(v) * pow(10.0, exp)) > 999.0 &&
+                               prefix < (int)(countof(SIPrefixes) - 1)) {
+                               prefix++;
+                               exp -= 3;
+                       }
                }
        }
 
        assert(prefix >= 0);
-       assert(prefix < countof(SIPrefixes));
+       assert(prefix < (int)countof(SIPrefixes));
 
-       const double multiplier = pow(10.0,
+       const Timestamp multiplier = pow(Timestamp(10),
                (int)- prefix * 3 - FirstSIPrefixPower);
 
        QString s;
        QTextStream ts(&s);
-       if (sign)
+       if (sign && !v.is_zero())
                ts << forcesign;
-       ts << fixed << qSetRealNumberPrecision(precision) <<
-               (v  * multiplier) << " " << SIPrefixes[prefix] << unit;
+       ts
+               << qSetRealNumberPrecision(precision)
+               << (v * multiplier)
+               << ' '
+               << SIPrefixes[prefix]
+               << unit;
 
        return s;
 }
 
-QString format_time(double t, int prefix,
-       unsigned int precision, bool sign)
+static QString pad_number(unsigned int number, int length)
+{
+       return QString("%1").arg(number, length, 10, QChar('0'));
+}
+
+static QString format_time_in_full(const Timestamp& t, signed precision)
+{
+       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<uint>();
+       const unsigned int minutes = fmod(whole_seconds / 60, 60).convert_to<uint>();
+       const unsigned int seconds = fmod(whole_seconds, 60).convert_to<uint>();
+
+       QString s;
+       QTextStream ts(&s);
+
+       if (t >= 0)
+               ts << "+";
+       else
+               ts << "-";
+
+       bool use_padding = false;
+
+       if (days) {
+               ts << days.str().c_str() << ":";
+               use_padding = true;
+       }
+
+       if (hours || days) {
+               ts << pad_number(hours, use_padding ? 2 : 0) << ":";
+               use_padding = true;
+       }
+
+       if (minutes || hours || days) {
+               ts << pad_number(minutes, use_padding ? 2 : 0);
+               use_padding = true;
+
+               // We're not showing any seconds with a negative precision
+               if (precision >= 0)
+                        ts << ":";
+       }
+
+       // precision < 0: Use DD:HH:MM format
+       // precision = 0: Use DD:HH:MM:SS format
+       // precision > 0: Use DD:HH:MM:SS.mmm nnn ppp fff format
+       if (precision >= 0) {
+               ts << pad_number(seconds, use_padding ? 2 : 0);
+
+               const Timestamp fraction = fabs(t) - whole_seconds;
+
+               if (precision > 0 && precision < 1000) {
+                       std::ostringstream ss;
+                       ss
+                               << std::fixed
+                               << std::setprecision(2 + precision)
+                               << std::setfill('0')
+                               << fraction;
+                       std::string fs = ss.str();
+
+                       ts << ".";
+
+                       // Copy all digits, inserting spaces as unit separators
+                       for (int i = 1; i <= precision; i++) {
+                               // Start at index 2 to skip the "0." at the beginning
+                               ts << fs[1 + i];
+
+                               if ((i > 0) && (i % 3 == 0) && (i != precision))
+                                       ts << " ";
+                       }
+               }
+       }
+
+       return s;
+}
+
+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
+       const int prefix_order =
+               -(prefix * 3 + pv::util::FirstSIPrefixPower);
+
+       const unsigned int relative_prec =
+               (prefix >= EmptySIPrefix) ? precision :
+               std::max((int)(precision - prefix_order), 0);
+
+       return format_si_value(t, unit, prefix, relative_prec);
+}
+
+QString format_time(const Timestamp& t, int prefix, TimeUnit unit,
+       unsigned int precision)
 {
-       return format_si_value(t, "s", prefix, precision, sign);
+       // Make 0 appear as 0, not random +0 or -0
+       if (t.is_zero())
+               return "0";
+
+       // If we have to use samples then we have no alternative formats
+       if (unit == Samples)
+               return format_time_with_si(t, "sa", prefix, precision);
+
+       // View in "normal" range -> medium precision, medium step size
+       // -> HH:MM:SS.mmm... or xxxx (si unit) if less than 60 seconds
+       // View zoomed way in -> high precision (>3), low step size (<1s)
+       // -> HH:MM:SS.mmm... or xxxx (si unit) if less than 60 seconds
+       if (abs(t) < 60)
+               return format_time_with_si(t, "s", prefix, precision);
+       else
+               return format_time_in_full(t, precision);
 }
 
-QString format_second(double second)
+QString format_second(const Timestamp& second)
 {
        return format_si_value(second, "s", -1, 0, false);
 }