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