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