+ assert(prefix != SIPrefix::yotta);
+ return static_cast<SIPrefix>(static_cast<int>(prefix) + 1);
+}
+
+// Insert the timestamp value into the stream in fixed-point notation
+// (and honor the precision)
+static QTextStream& operator<<(QTextStream& stream, const Timestamp& t)
+{
+ // The multiprecision types already have a function and a stream insertion
+ // operator to convert them to a string, however these functions abuse a
+ // precision value of zero to print all available decimal places instead of
+ // none, and the boost authors refuse to fix this because they don't want
+ // to break buggy code that relies on this bug.
+ // (https://svn.boost.org/trac/boost/ticket/10103)
+ // Therefore we have to work around the case where precision is zero.
+
+ int precision = stream.realNumberPrecision();
+
+ std::ostringstream ss;
+ ss << std::fixed;
+
+ if (stream.numberFlags() & QTextStream::ForceSign) {
+ ss << std::showpos;
+ }
+
+ if (0 == precision) {
+ ss
+ << std::setprecision(1)
+ << round(t);
+ } else {
+ ss
+ << std::setprecision(precision)
+ << t;
+ }
+
+ std::string str(ss.str());
+ if (0 == precision) {
+ // remove the separator and the unwanted decimal place
+ str.resize(str.size() - 2);
+ }
+
+ return stream << QString::fromStdString(str);
+}
+
+QString format_time_si(
+ const Timestamp& v,
+ SIPrefix prefix,
+ unsigned int precision,
+ QString unit,
+ bool sign)
+{
+ if (prefix == SIPrefix::unspecified) {
+ // No prefix given, calculate it
+
+ if (v.is_zero()) {
+ prefix = SIPrefix::none;
+ } else {
+ int exp = exponent(SIPrefix::yotta);
+ prefix = SIPrefix::yocto;
+ while ((fabs(v) * pow(Timestamp(10), exp)) > 999 &&
+ prefix < SIPrefix::yotta) {
+ prefix = successor(prefix);
+ exp -= 3;
+ }