]> sigrok.org Git - pulseview.git/blame - pv/util.cpp
format_si_value(): Use the timestamp type in the calculation
[pulseview.git] / pv / util.cpp
CommitLineData
f0c9f81c
JS
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
2acdb232 21#include "util.hpp"
f0c9f81c
JS
22
23#include <extdef.h>
24
25#include <assert.h>
26
ac981988 27#include <algorithm>
f52be90d 28#include <sstream>
ac981988 29
f0c9f81c
JS
30#include <QTextStream>
31#include <QDebug>
32
33using namespace Qt;
34
35namespace pv {
36namespace util {
37
f52be90d
JS
38static const QString SIPrefixes[17] = {
39 "y", "z",
40 "a", "f", "p",
41 "n", QChar(0x03BC), "m",
42 "",
43 "k", "M", "G",
44 "T", "P", "E",
45 "Z", "Y"};
46const int EmptySIPrefix = 8;
47const int FirstSIPrefixPower = -(EmptySIPrefix * 3);
f4ebff5e 48const double MinTimeDelta = 1e-15; // Anything below 1 fs can be considered zero
f0c9f81c 49
f52be90d
JS
50// Insert the timestamp value into the stream in fixed-point notation
51// (and honor the precision)
52static QTextStream& operator<<(QTextStream& stream, const Timestamp& t)
53{
54 // The multiprecision types already have a function and a stream insertion
55 // operator to convert them to a string, however these functions abuse a
56 // precision value of zero to print all available decimal places instead of
57 // none, and the boost authors refuse to fix this because they don't want
58 // to break buggy code that relies on this bug.
59 // (https://svn.boost.org/trac/boost/ticket/10103)
60 // Therefore we have to work around the case where precision is zero.
61
62 int precision = stream.realNumberPrecision();
63
64 std::ostringstream ss;
65 ss << std::fixed;
66
67 if (stream.numberFlags() & QTextStream::ForceSign) {
68 ss << std::showpos;
69 }
70
71 if (0 == precision) {
72 ss
73 << std::setprecision(1)
74 << round(t);
75 } else {
76 ss
77 << std::setprecision(precision)
78 << t;
79 }
80
81 std::string str(ss.str());
82 if (0 == precision) {
83 // remove the separator and the unwanted decimal place
84 str.resize(str.size() - 2);
85 }
86
87 return stream << QString::fromStdString(str);
88}
89
90QString format_si_value(const Timestamp& v, QString unit, int prefix,
f0c9f81c
JS
91 unsigned int precision, bool sign)
92{
4bc10a91 93 if (prefix < 0) {
f52be90d
JS
94 // No prefix given, calculate it
95
96 if (v.is_zero()) {
97 prefix = EmptySIPrefix;
98 } else {
99 int exp = -FirstSIPrefixPower;
100
101 prefix = 0;
102 while ((fabs(v) * pow(10.0, exp)) > 999.0 &&
103 prefix < (int)(countof(SIPrefixes) - 1)) {
104 prefix++;
105 exp -= 3;
106 }
4bc10a91
JH
107 }
108 }
109
110 assert(prefix >= 0);
0ba3971c 111 assert(prefix < (int)countof(SIPrefixes));
f0c9f81c 112
f52be90d 113 const Timestamp multiplier = pow(Timestamp(10),
f0c9f81c
JS
114 (int)- prefix * 3 - FirstSIPrefixPower);
115
116 QString s;
117 QTextStream ts(&s);
f52be90d 118 if (sign && !v.is_zero())
f0c9f81c 119 ts << forcesign;
f52be90d
JS
120 ts
121 << qSetRealNumberPrecision(precision)
122 << (v * multiplier)
123 << ' '
124 << SIPrefixes[prefix]
125 << unit;
f0c9f81c
JS
126
127 return s;
128}
129
ac981988
SA
130static QString pad_number(unsigned int number, int length)
131{
132 return QString("%1").arg(number, length, 10, QChar('0'));
133}
134
dae21ebc 135static QString format_time_in_full(double t, signed precision)
ac981988
SA
136{
137 const unsigned int whole_seconds = abs((int) t);
138 const unsigned int days = whole_seconds / (60 * 60 * 24);
139 const unsigned int hours = (whole_seconds / (60 * 60)) % 24;
140 const unsigned int minutes = (whole_seconds / 60) % 60;
141 const unsigned int seconds = whole_seconds % 60;
142
143 QString s;
144 QTextStream ts(&s);
145
dae21ebc 146 if (t >= 0)
ac981988 147 ts << "+";
dae21ebc 148 else
ac981988
SA
149 ts << "-";
150
151 bool use_padding = false;
152
153 if (days) {
154 ts << days << ":";
155 use_padding = true;
156 }
157
158 if (hours || days) {
159 ts << pad_number(hours, use_padding ? 2 : 0) << ":";
160 use_padding = true;
161 }
162
163 if (minutes || hours || days) {
164 ts << pad_number(minutes, use_padding ? 2 : 0);
165 use_padding = true;
166
167 // We're not showing any seconds with a negative precision
168 if (precision >= 0)
169 ts << ":";
170 }
171
172 // precision < 0: Use DD:HH:MM format
173 // precision = 0: Use DD:HH:MM:SS format
174 // precision > 0: Use DD:HH:MM:SS.mmm nnn ppp fff format
175 if (precision >= 0) {
176 ts << pad_number(seconds, use_padding ? 2 : 0);
177
ca810a8c 178 const double fraction = fabs(t) - whole_seconds;
ac981988
SA
179
180 if (precision > 0 && precision < 1000) {
181 QString fs = QString("%1").arg(fraction, -(2 + precision), 'f',
182 precision, QChar('0'));
183
184 ts << ".";
185
186 // Copy all digits, inserting spaces as unit separators
187 for (int i = 1; i <= precision; i++) {
188 // Start at index 2 to skip the "0." at the beginning
189 ts << fs[1 + i];
190
191 if ((i > 0) && (i % 3 == 0))
192 ts << " ";
193 }
194 }
195 }
196
197 return s;
198}
199
6638455f 200static QString format_time_with_si(double t, QString unit, int prefix,
dae21ebc 201 unsigned int precision)
6638455f
SA
202{
203 // The precision is always given without taking the prefix into account
204 // so we need to deduct the number of decimals the prefix might imply
205 const int prefix_order =
206 -(prefix * 3 + pv::util::FirstSIPrefixPower);
207
208 const unsigned int relative_prec =
f52be90d 209 (prefix >= EmptySIPrefix) ? precision :
6638455f
SA
210 std::max((int)(precision - prefix_order), 0);
211
dae21ebc 212 return format_si_value(t, unit, prefix, relative_prec);
6638455f
SA
213}
214
60d9b99a 215static QString format_time(double t, int prefix, TimeUnit unit, unsigned int precision)
62974f45 216{
f4ebff5e
SA
217 // Make 0 appear as 0, not random +0 or -0
218 if (fabs(t) < MinTimeDelta)
219 return "0";
220
ac981988 221 // If we have to use samples then we have no alternative formats
6638455f 222 if (unit == Samples)
dae21ebc 223 return format_time_with_si(t, "sa", prefix, precision);
ac981988
SA
224
225 // View in "normal" range -> medium precision, medium step size
226 // -> HH:MM:SS.mmm... or xxxx (si unit) if less than 60 seconds
227 // View zoomed way in -> high precision (>3), low step size (<1s)
228 // -> HH:MM:SS.mmm... or xxxx (si unit) if less than 60 seconds
6638455f 229 if (abs(t) < 60)
dae21ebc 230 return format_time_with_si(t, "s", prefix, precision);
6638455f 231 else
dae21ebc 232 return format_time_in_full(t, precision);
4bc10a91 233}
62974f45 234
60d9b99a
JS
235QString format_time(const Timestamp& t, int prefix, TimeUnit unit, unsigned int precision)
236{
237 return format_time(t.convert_to<double>(), prefix, unit, precision);
238}
239
240QString format_second(const Timestamp& second)
4bc10a91 241{
f52be90d 242 return format_si_value(second, "s", -1, 0, false);
62974f45
JS
243}
244
f0c9f81c
JS
245} // namespace util
246} // namespace pv