2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <QTextStream>
37 static const QString SIPrefixes[17] =
38 {"y", "z", "a", "f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G",
39 "T", "P", "E", "Z", "Y"};
40 const int FirstSIPrefix = 8;
41 const int FirstSIPrefixPower = -(FirstSIPrefix * 3);
43 QString format_si_value(double v, QString unit, int prefix,
44 unsigned int precision, bool sign)
47 int exp = -FirstSIPrefixPower;
50 while ((fabs(v) * pow(10.0, exp)) > 999.0 &&
51 prefix < (int)(countof(SIPrefixes) - 1)) {
58 assert(prefix < (int)countof(SIPrefixes));
60 const double multiplier = pow(10.0,
61 (int)- prefix * 3 - FirstSIPrefixPower);
67 ts << fixed << qSetRealNumberPrecision(precision) <<
68 (v * multiplier) << " " << SIPrefixes[prefix] << unit;
73 static QString pad_number(unsigned int number, int length)
75 return QString("%1").arg(number, length, 10, QChar('0'));
78 static QString format_time_in_full(double t, signed precision, bool force_sign)
80 const unsigned int whole_seconds = abs((int) t);
81 const unsigned int days = whole_seconds / (60 * 60 * 24);
82 const unsigned int hours = (whole_seconds / (60 * 60)) % 24;
83 const unsigned int minutes = (whole_seconds / 60) % 60;
84 const unsigned int seconds = whole_seconds % 60;
89 if (force_sign && (t >= 0))
94 bool use_padding = false;
102 ts << pad_number(hours, use_padding ? 2 : 0) << ":";
106 if (minutes || hours || days) {
107 ts << pad_number(minutes, use_padding ? 2 : 0);
110 // We're not showing any seconds with a negative precision
115 // precision < 0: Use DD:HH:MM format
116 // precision = 0: Use DD:HH:MM:SS format
117 // precision > 0: Use DD:HH:MM:SS.mmm nnn ppp fff format
118 if (precision >= 0) {
119 ts << pad_number(seconds, use_padding ? 2 : 0);
121 const double fraction = fabs(t - whole_seconds);
123 if (precision > 0 && precision < 1000) {
124 QString fs = QString("%1").arg(fraction, -(2 + precision), 'f',
125 precision, QChar('0'));
129 // Copy all digits, inserting spaces as unit separators
130 for (int i = 1; i <= precision; i++) {
131 // Start at index 2 to skip the "0." at the beginning
134 if ((i > 0) && (i % 3 == 0))
143 static QString format_time_with_si(double t, QString unit, int prefix,
144 unsigned int precision, bool sign)
146 // The precision is always given without taking the prefix into account
147 // so we need to deduct the number of decimals the prefix might imply
148 const int prefix_order =
149 -(prefix * 3 + pv::util::FirstSIPrefixPower);
151 const unsigned int relative_prec =
152 (prefix >= pv::util::FirstSIPrefix) ? precision :
153 std::max((int)(precision - prefix_order), 0);
155 return format_si_value(t, unit, prefix, relative_prec, sign);
158 QString format_time(double t, int prefix, TimeUnit unit,
159 unsigned int precision, double step_size, bool sign)
161 // If we have to use samples then we have no alternative formats
163 return format_time_with_si(t, "sa", prefix, precision, sign);
165 // View zoomed way out -> low precision (0), high step size (>=60s)
167 if ((precision == 0) && (step_size >= 60))
168 return format_time_in_full(t, -1, sign);
170 // View in "normal" range -> medium precision, medium step size
171 // -> HH:MM:SS.mmm... or xxxx (si unit) if less than 60 seconds
172 // View zoomed way in -> high precision (>3), low step size (<1s)
173 // -> HH:MM:SS.mmm... or xxxx (si unit) if less than 60 seconds
175 return format_time_with_si(t, "s", prefix, precision, sign);
177 return format_time_in_full(t, precision, sign);
180 QString format_second(double second)
182 return format_si_value(second, "s", -1, 0, false);