29 #include <QTextStream> 33 using std::ostringstream;
35 using std::setprecision;
47 case SIPrefix::yocto:
return stream <<
'y';
48 case SIPrefix::zepto:
return stream <<
'z';
49 case SIPrefix::atto:
return stream <<
'a';
50 case SIPrefix::femto:
return stream <<
'f';
51 case SIPrefix::pico:
return stream <<
'p';
52 case SIPrefix::nano:
return stream <<
'n';
53 case SIPrefix::micro:
return stream << QChar(0x03BC);
54 case SIPrefix::milli:
return stream <<
'm';
55 case SIPrefix::kilo:
return stream <<
'k';
56 case SIPrefix::mega:
return stream <<
'M';
57 case SIPrefix::giga:
return stream <<
'G';
58 case SIPrefix::tera:
return stream <<
'T';
59 case SIPrefix::peta:
return stream <<
'P';
60 case SIPrefix::exa:
return stream <<
'E';
61 case SIPrefix::zetta:
return stream <<
'Z';
62 case SIPrefix::yotta:
return stream <<
'Y';
64 default:
return stream;
70 return 3 * (
static_cast<int>(prefix) - static_cast<int>(SIPrefix::none));
75 assert(prefix != SIPrefix::yotta);
76 return static_cast<SIPrefix>(
static_cast<int>(prefix) + 1);
91 int precision = stream.realNumberPrecision();
96 if (stream.numberFlags() & QTextStream::ForceSign)
100 ss << setprecision(1) << round(t);
102 ss << setprecision(precision) <<
t;
104 string str(ss.str());
105 if (0 == precision) {
107 str.resize(str.size() - 2);
110 return stream << QString::fromStdString(str);
118 prefix = SIPrefix::none;
120 int exp =
exponent(SIPrefix::yotta);
121 prefix = SIPrefix::yocto;
122 while ((fabs(v) *
pow(10, exp)) > 999 &&
123 prefix < SIPrefix::yotta) {
133 unsigned int precision, QString unit,
bool sign)
135 if (prefix == SIPrefix::unspecified)
138 assert(prefix >= SIPrefix::yocto);
139 assert(prefix <= SIPrefix::yotta);
145 if (sign && !v.is_zero())
146 ts.setNumberFlags(ts.numberFlags() | QTextStream::ForceSign);
147 ts << qSetRealNumberPrecision(precision) << (v * multiplier);
148 ts <<
' ' << prefix << unit;
154 QString unit,
bool sign)
156 if (prefix == SIPrefix::unspecified) {
159 const int prefix_order = -
exponent(prefix);
160 precision = (prefix >= SIPrefix::none) ?
max((
int)(precision + prefix_order), 0) :
161 max((
int)(precision - prefix_order), 0);
164 assert(prefix >= SIPrefix::yocto);
165 assert(prefix <= SIPrefix::yotta);
167 const double multiplier =
pow(10.0, -
exponent(prefix));
171 if (sign && (v != 0))
172 ts.setNumberFlags(ts.numberFlags() | QTextStream::ForceSign);
173 ts.setRealNumberNotation(QTextStream::FixedNotation);
174 ts.setRealNumberPrecision(precision);
175 ts << (v * multiplier) <<
' ' << prefix << unit;
181 unsigned precision, QString unit,
bool sign)
185 const int prefix_order = -
exponent(prefix);
187 const unsigned int relative_prec =
188 (prefix >= SIPrefix::none) ? precision :
189 max((
int)(precision - prefix_order), 0);
197 return QString(
"%1").arg(number, length, 10, QChar(
'0'));
202 const Timestamp whole_seconds = floor(abs(t));
203 const Timestamp days = floor(whole_seconds / (60 * 60 * 24));
204 const unsigned int hours = fmod(whole_seconds / (60 * 60), 24).convert_to<uint>();
205 const unsigned int minutes = fmod(whole_seconds / 60, 60).convert_to<uint>();
206 const unsigned int seconds = fmod(whole_seconds, 60).convert_to<uint>();
216 bool use_padding =
false;
220 ts << days.str().c_str() <<
":";
226 ts <<
pad_number(hours, use_padding ? 2 : 0) <<
":";
231 ts <<
pad_number(minutes, use_padding ? 2 : 0);
241 const Timestamp fraction = fabs(t) - whole_seconds;
244 ss << fixed << setprecision(precision) << setfill(
'0') << fraction;
245 string fs = ss.str();
248 for (
int i = 1; i <= precision; i++) {
252 if ((i > 0) && (i % 3 == 0) && (i != precision))
270 vector<string> result;
273 while ((pos = text.find(separator)) != std::string::npos) {
274 result.push_back(text.substr(0, pos));
275 text = text.substr(pos + separator.length());
277 result.push_back(text);
290 std::streamsize
text_width(
const QFontMetrics &metric,
const QString &
string)
292 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) 293 return metric.horizontalAdvance(
string);
295 return metric.width(
string);
static QTextStream & operator<<(QTextStream &stream, const Timestamp &t)
x y t t *t x y t t t x y t t t x *y t *t t x *y t *t t x y t t t x y t t t t(t+t)") define_sfop3(16
QString format_time_si(const Timestamp &v, SIPrefix prefix, unsigned int precision, QString unit, bool sign)
std::streamsize text_width(const QFontMetrics &metric, const QString &string)
QString format_value_si(double v, SIPrefix prefix, unsigned precision, QString unit, bool sign)
QString format_time_si_adjusted(const Timestamp &t, SIPrefix prefix, unsigned precision, QString unit, bool sign)
int exponent(SIPrefix prefix)
Returns the exponent that corresponds to a given prefix.
T max(const T v0, const T v1)
vector< string > split_string(string text, string separator)
SIPrefix determine_value_prefix(double v)
QString format_time_minutes(const Timestamp &t, signed precision, bool sign)
static QString pad_number(unsigned int number, int length)
static SIPrefix successor(SIPrefix prefix)
boost::multiprecision::number< boost::multiprecision::cpp_dec_float< 24 >, boost::multiprecision::et_off > Timestamp
Timestamp type providing yoctosecond resolution.
T pow(const T v0, const T v1)