]> sigrok.org Git - pulseview.git/blame - pv/util.cpp
Util: Introduce DD:HH:MM:SS.mmm nnn ppp fff format
[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
SA
27#include <algorithm>
28
f0c9f81c
JS
29#include <QTextStream>
30#include <QDebug>
31
32using namespace Qt;
33
34namespace pv {
35namespace util {
36
6f1f59c8
JH
37static const QString SIPrefixes[17] =
38 {"y", "z", "a", "f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G",
39 "T", "P", "E", "Z", "Y"};
ac981988
SA
40const int FirstSIPrefix = 8;
41const int FirstSIPrefixPower = -(FirstSIPrefix * 3);
f0c9f81c 42
4bc10a91 43QString format_si_value(double v, QString unit, int prefix,
f0c9f81c
JS
44 unsigned int precision, bool sign)
45{
4bc10a91
JH
46 if (prefix < 0) {
47 int exp = -FirstSIPrefixPower;
48
49 prefix = 0;
50 while ((fabs(v) * pow(10.0, exp)) > 999.0 &&
51 prefix < (int)(countof(SIPrefixes) - 1)) {
52 prefix++;
53 exp -= 3;
54 }
55 }
56
57 assert(prefix >= 0);
0ba3971c 58 assert(prefix < (int)countof(SIPrefixes));
f0c9f81c
JS
59
60 const double multiplier = pow(10.0,
61 (int)- prefix * 3 - FirstSIPrefixPower);
62
63 QString s;
64 QTextStream ts(&s);
4bc10a91 65 if (sign)
f0c9f81c 66 ts << forcesign;
e113edc6
JH
67 ts << fixed << qSetRealNumberPrecision(precision) <<
68 (v * multiplier) << " " << SIPrefixes[prefix] << unit;
f0c9f81c
JS
69
70 return s;
71}
72
ac981988
SA
73static QString pad_number(unsigned int number, int length)
74{
75 return QString("%1").arg(number, length, 10, QChar('0'));
76}
77
78static QString format_time_in_full(double t, signed precision, bool force_sign)
79{
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;
85
86 QString s;
87 QTextStream ts(&s);
88
89 if (force_sign && (t >= 0))
90 ts << "+";
91 if (t < 0)
92 ts << "-";
93
94 bool use_padding = false;
95
96 if (days) {
97 ts << days << ":";
98 use_padding = true;
99 }
100
101 if (hours || days) {
102 ts << pad_number(hours, use_padding ? 2 : 0) << ":";
103 use_padding = true;
104 }
105
106 if (minutes || hours || days) {
107 ts << pad_number(minutes, use_padding ? 2 : 0);
108 use_padding = true;
109
110 // We're not showing any seconds with a negative precision
111 if (precision >= 0)
112 ts << ":";
113 }
114
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);
120
121 const double fraction = fabs(t - whole_seconds);
122
123 if (precision > 0 && precision < 1000) {
124 QString fs = QString("%1").arg(fraction, -(2 + precision), 'f',
125 precision, QChar('0'));
126
127 ts << ".";
128
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
132 ts << fs[1 + i];
133
134 if ((i > 0) && (i % 3 == 0))
135 ts << " ";
136 }
137 }
138 }
139
140 return s;
141}
142
96b6316a 143QString format_time(double t, int prefix, TimeUnit unit,
ac981988 144 unsigned int precision, double step_size, bool sign)
62974f45 145{
ac981988
SA
146 // If we have to use samples then we have no alternative formats
147 if (unit == Samples) {
148 // The precision is always given without taking the prefix into account
149 // so we need to deduct the number of decimals the prefix might imply
150 const int prefix_order =
151 -(prefix * 3 + pv::util::FirstSIPrefixPower);
152
153 const unsigned int relative_prec =
154 (prefix >= pv::util::FirstSIPrefix) ? precision :
155 std::max((int)(precision - prefix_order), 0);
156
157 return format_si_value(t, "sa", prefix, relative_prec, sign);
158 }
159
160 // View zoomed way out -> low precision (0), high step size (>60s)
161 // -> DD:HH:MM
162 if ((precision == 0) && (step_size >= 60)) {
163 return format_time_in_full(t, -1, sign);
164 }
165
166 // View in "normal" range -> medium precision, medium step size
167 // -> HH:MM:SS.mmm... or xxxx (si unit) if less than 60 seconds
168 // View zoomed way in -> high precision (>3), low step size (<1s)
169 // -> HH:MM:SS.mmm... or xxxx (si unit) if less than 60 seconds
170 if (abs(t) < 60) {
171 // The precision is always given without taking the prefix into account
172 // so we need to deduct the number of decimals the prefix might imply
173 const int prefix_order =
174 -(prefix * 3 + pv::util::FirstSIPrefixPower);
175
176 const unsigned int relative_prec =
177 (prefix >= pv::util::FirstSIPrefix) ? precision :
178 std::max((int)(precision - prefix_order), 0);
179
180 return format_si_value(t, "s", prefix, relative_prec, sign);
181 } else
182 return format_time_in_full(t, precision, sign);
4bc10a91 183}
62974f45 184
4bc10a91
JH
185QString format_second(double second)
186{
187 return format_si_value(second, "s", -1, 0, false);
62974f45
JS
188}
189
f0c9f81c
JS
190} // namespace util
191} // namespace pv