]> sigrok.org Git - pulseview.git/blob - pv/util.cpp
Use a type with a greater resolution to represent time values
[pulseview.git] / pv / util.cpp
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
21 #include "util.hpp"
22
23 #include <extdef.h>
24
25 #include <assert.h>
26
27 #include <algorithm>
28
29 #include <QTextStream>
30 #include <QDebug>
31
32 using namespace Qt;
33
34 namespace pv {
35 namespace util {
36
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);
42 const double MinTimeDelta = 1e-15; // Anything below 1 fs can be considered zero
43
44 static QString format_si_value(double v, QString unit, int prefix,
45         unsigned int precision, bool sign)
46 {
47         if (prefix < 0) {
48                 int exp = -FirstSIPrefixPower;
49
50                 prefix = 0;
51                 while ((fabs(v) * pow(10.0, exp)) > 999.0 &&
52                         prefix < (int)(countof(SIPrefixes) - 1)) {
53                         prefix++;
54                         exp -= 3;
55                 }
56         }
57
58         assert(prefix >= 0);
59         assert(prefix < (int)countof(SIPrefixes));
60
61         const double multiplier = pow(10.0,
62                 (int)- prefix * 3 - FirstSIPrefixPower);
63
64         QString s;
65         QTextStream ts(&s);
66         if (sign)
67                 ts << forcesign;
68         ts << fixed << qSetRealNumberPrecision(precision) <<
69                 (v  * multiplier) << " " << SIPrefixes[prefix] << unit;
70
71         return s;
72 }
73
74 QString format_si_value(const Timestamp& v, QString unit, int prefix,
75         unsigned int precision, bool sign)
76 {
77         return format_si_value(v.convert_to<double>(), unit, prefix, precision, sign);
78 }
79
80 static QString pad_number(unsigned int number, int length)
81 {
82         return QString("%1").arg(number, length, 10, QChar('0'));
83 }
84
85 static QString format_time_in_full(double t, signed precision)
86 {
87         const unsigned int whole_seconds = abs((int) t);
88         const unsigned int days = whole_seconds / (60 * 60 * 24);
89         const unsigned int hours = (whole_seconds / (60 * 60)) % 24;
90         const unsigned int minutes = (whole_seconds / 60) % 60;
91         const unsigned int seconds = whole_seconds % 60;
92
93         QString s;
94         QTextStream ts(&s);
95
96         if (t >= 0)
97                 ts << "+";
98         else
99                 ts << "-";
100
101         bool use_padding = false;
102
103         if (days) {
104                 ts << days << ":";
105                 use_padding = true;
106         }
107
108         if (hours || days) {
109                 ts << pad_number(hours, use_padding ? 2 : 0) << ":";
110                 use_padding = true;
111         }
112
113         if (minutes || hours || days) {
114                 ts << pad_number(minutes, use_padding ? 2 : 0);
115                 use_padding = true;
116
117                 // We're not showing any seconds with a negative precision
118                 if (precision >= 0)
119                          ts << ":";
120         }
121
122         // precision < 0: Use DD:HH:MM format
123         // precision = 0: Use DD:HH:MM:SS format
124         // precision > 0: Use DD:HH:MM:SS.mmm nnn ppp fff format
125         if (precision >= 0) {
126                 ts << pad_number(seconds, use_padding ? 2 : 0);
127
128                 const double fraction = fabs(t) - whole_seconds;
129
130                 if (precision > 0 && precision < 1000) {
131                         QString fs = QString("%1").arg(fraction, -(2 + precision), 'f',
132                                 precision, QChar('0'));
133
134                         ts << ".";
135
136                         // Copy all digits, inserting spaces as unit separators
137                         for (int i = 1; i <= precision; i++) {
138                                 // Start at index 2 to skip the "0." at the beginning
139                                 ts << fs[1 + i];
140
141                                 if ((i > 0) && (i % 3 == 0))
142                                         ts << " ";
143                         }
144                 }
145         }
146
147         return s;
148 }
149
150 static QString format_time_with_si(double t, QString unit, int prefix,
151         unsigned int precision)
152 {
153         // The precision is always given without taking the prefix into account
154         // so we need to deduct the number of decimals the prefix might imply
155         const int prefix_order =
156                 -(prefix * 3 + pv::util::FirstSIPrefixPower);
157
158         const unsigned int relative_prec =
159                 (prefix >= pv::util::FirstSIPrefix) ? precision :
160                 std::max((int)(precision - prefix_order), 0);
161
162         return format_si_value(t, unit, prefix, relative_prec);
163 }
164
165 static QString format_time(double t, int prefix, TimeUnit unit, unsigned int precision)
166 {
167         // Make 0 appear as 0, not random +0 or -0
168         if (fabs(t) < MinTimeDelta)
169                 return "0";
170
171         // If we have to use samples then we have no alternative formats
172         if (unit == Samples)
173                 return format_time_with_si(t, "sa", prefix, precision);
174
175         // View in "normal" range -> medium precision, medium step size
176         // -> HH:MM:SS.mmm... or xxxx (si unit) if less than 60 seconds
177         // View zoomed way in -> high precision (>3), low step size (<1s)
178         // -> HH:MM:SS.mmm... or xxxx (si unit) if less than 60 seconds
179         if (abs(t) < 60)
180                 return format_time_with_si(t, "s", prefix, precision);
181         else
182                 return format_time_in_full(t, precision);
183 }
184
185 QString format_time(const Timestamp& t, int prefix, TimeUnit unit, unsigned int precision)
186 {
187         return format_time(t.convert_to<double>(), prefix, unit, precision);
188 }
189
190 QString format_second(const Timestamp& second)
191 {
192         return format_si_value(second.convert_to<double>(), "s", -1, 0, false);
193 }
194
195 } // namespace util
196 } // namespace pv