2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #include <QTextStream>
36 static QTextStream& operator<<(QTextStream& stream, SIPrefix prefix)
39 case SIPrefix::yocto: return stream << 'y';
40 case SIPrefix::zepto: return stream << 'z';
41 case SIPrefix::atto: return stream << 'a';
42 case SIPrefix::femto: return stream << 'f';
43 case SIPrefix::pico: return stream << 'p';
44 case SIPrefix::nano: return stream << 'n';
45 case SIPrefix::micro: return stream << QChar(0x03BC);
46 case SIPrefix::milli: return stream << 'm';
47 case SIPrefix::kilo: return stream << 'k';
48 case SIPrefix::mega: return stream << 'M';
49 case SIPrefix::giga: return stream << 'G';
50 case SIPrefix::tera: return stream << 'T';
51 case SIPrefix::peta: return stream << 'P';
52 case SIPrefix::exa: return stream << 'E';
53 case SIPrefix::zetta: return stream << 'Z';
54 case SIPrefix::yotta: return stream << 'Y';
56 default: return stream;
60 int exponent(SIPrefix prefix)
62 return 3 * (static_cast<int>(prefix) - static_cast<int>(SIPrefix::none));
65 static SIPrefix successor(SIPrefix prefix)
67 assert(prefix != SIPrefix::yotta);
68 return static_cast<SIPrefix>(static_cast<int>(prefix) + 1);
71 // Insert the timestamp value into the stream in fixed-point notation
72 // (and honor the precision)
73 static QTextStream& operator<<(QTextStream& stream, const Timestamp& t)
75 // The multiprecision types already have a function and a stream insertion
76 // operator to convert them to a string, however these functions abuse a
77 // precision value of zero to print all available decimal places instead of
78 // none, and the boost authors refuse to fix this because they don't want
79 // to break buggy code that relies on this bug.
80 // (https://svn.boost.org/trac/boost/ticket/10103)
81 // Therefore we have to work around the case where precision is zero.
83 int precision = stream.realNumberPrecision();
85 std::ostringstream ss;
88 if (stream.numberFlags() & QTextStream::ForceSign)
92 ss << std::setprecision(1) << round(t);
94 ss << std::setprecision(precision) << t;
96 std::string str(ss.str());
98 // remove the separator and the unwanted decimal place
99 str.resize(str.size() - 2);
102 return stream << QString::fromStdString(str);
105 QString format_time_si(
108 unsigned int precision,
112 if (prefix == SIPrefix::unspecified) {
113 // No prefix given, calculate it
116 prefix = SIPrefix::none;
118 int exp = exponent(SIPrefix::yotta);
119 prefix = SIPrefix::yocto;
120 while ((fabs(v) * pow(Timestamp(10), exp)) > 999 &&
121 prefix < SIPrefix::yotta) {
122 prefix = successor(prefix);
128 assert(prefix >= SIPrefix::yocto);
129 assert(prefix <= SIPrefix::yotta);
131 const Timestamp multiplier = pow(Timestamp(10), -exponent(prefix));
135 if (sign && !v.is_zero())
138 << qSetRealNumberPrecision(precision)
147 QString format_time_si_adjusted(
154 // The precision is always given without taking the prefix into account
155 // so we need to deduct the number of decimals the prefix might imply
156 const int prefix_order = -exponent(prefix);
158 const unsigned int relative_prec =
159 (prefix >= SIPrefix::none) ? precision :
160 std::max((int)(precision - prefix_order), 0);
162 return format_time_si(t, prefix, relative_prec, unit, sign);
166 // Helper for 'format_time_minutes()'.
167 static QString pad_number(unsigned int number, int length)
169 return QString("%1").arg(number, length, 10, QChar('0'));
172 QString format_time_minutes(const Timestamp& t, signed precision, bool sign)
174 const Timestamp whole_seconds = floor(abs(t));
175 const Timestamp days = floor(whole_seconds / (60 * 60 * 24));
176 const unsigned int hours = fmod(whole_seconds / (60 * 60), 24).convert_to<uint>();
177 const unsigned int minutes = fmod(whole_seconds / 60, 60).convert_to<uint>();
178 const unsigned int seconds = fmod(whole_seconds, 60).convert_to<uint>();
188 bool use_padding = false;
192 ts << days.str().c_str() << ":";
198 ts << pad_number(hours, use_padding ? 2 : 0) << ":";
203 ts << pad_number(minutes, use_padding ? 2 : 0);
208 ts << pad_number(seconds, 2);
213 const Timestamp fraction = fabs(t) - whole_seconds;
215 std::ostringstream ss;
218 << std::setprecision(precision)
221 std::string fs = ss.str();
223 // Copy all digits, inserting spaces as unit separators
224 for (int i = 1; i <= precision; i++) {
225 // Start at index 2 to skip the "0." at the beginning
228 if ((i > 0) && (i % 3 == 0) && (i != precision))