30 #include <QTextStream>
41 case SIPrefix::yocto:
return stream <<
'y';
42 case SIPrefix::zepto:
return stream <<
'z';
43 case SIPrefix::atto:
return stream <<
'a';
44 case SIPrefix::femto:
return stream <<
'f';
45 case SIPrefix::pico:
return stream <<
'p';
46 case SIPrefix::nano:
return stream <<
'n';
47 case SIPrefix::micro:
return stream << QChar(0x03BC);
48 case SIPrefix::milli:
return stream <<
'm';
49 case SIPrefix::kilo:
return stream <<
'k';
50 case SIPrefix::mega:
return stream <<
'M';
51 case SIPrefix::giga:
return stream <<
'G';
52 case SIPrefix::tera:
return stream <<
'T';
53 case SIPrefix::peta:
return stream <<
'P';
54 case SIPrefix::exa:
return stream <<
'E';
55 case SIPrefix::zetta:
return stream <<
'Z';
56 case SIPrefix::yotta:
return stream <<
'Y';
58 default:
return stream;
64 return 3 * (
static_cast<int>(prefix) - static_cast<int>(SIPrefix::none));
69 assert(prefix != SIPrefix::yotta);
70 return static_cast<SIPrefix>(
static_cast<int>(prefix) + 1);
85 int precision = stream.realNumberPrecision();
87 std::ostringstream ss;
90 if (stream.numberFlags() & QTextStream::ForceSign)
94 ss << std::setprecision(1) << round(t);
96 ss << std::setprecision(precision) << t;
98 std::string str(ss.str());
101 str.resize(str.size() - 2);
104 return stream << QString::fromStdString(str);
110 unsigned int precision,
114 if (prefix == SIPrefix::unspecified) {
118 prefix = SIPrefix::none;
120 int exp =
exponent(SIPrefix::yotta);
121 prefix = SIPrefix::yocto;
122 while ((fabs(v) * pow(
Timestamp(10), exp)) > 999 &&
123 prefix < SIPrefix::yotta) {
130 assert(prefix >= SIPrefix::yocto);
131 assert(prefix <= SIPrefix::yotta);
137 if (sign && !v.is_zero())
140 << qSetRealNumberPrecision(precision)
158 const int prefix_order = -
exponent(prefix);
160 const unsigned int relative_prec =
161 (prefix >= SIPrefix::none) ? precision :
162 std::max((
int)(precision - prefix_order), 0);
171 return QString(
"%1").arg(number, length, 10, QChar(
'0'));
176 const Timestamp whole_seconds = floor(abs(t));
177 const Timestamp days = floor(whole_seconds / (60 * 60 * 24));
178 const unsigned int hours = fmod(whole_seconds / (60 * 60), 24).convert_to<uint>();
179 const unsigned int minutes = fmod(whole_seconds / 60, 60).convert_to<uint>();
180 const unsigned int seconds = fmod(whole_seconds, 60).convert_to<uint>();
190 bool use_padding =
false;
194 ts << days.str().c_str() <<
":";
200 ts <<
pad_number(hours, use_padding ? 2 : 0) <<
":";
205 ts <<
pad_number(minutes, use_padding ? 2 : 0);
215 const Timestamp fraction = fabs(t) - whole_seconds;
217 std::ostringstream ss;
220 << std::setprecision(precision)
223 std::string fs = ss.str();
226 for (
int i = 1; i <= precision; i++) {
230 if ((i > 0) && (i % 3 == 0) && (i != precision))
static QTextStream & operator<<(QTextStream &stream, const Timestamp &t)
QString format_time_si(const Timestamp &v, SIPrefix prefix, unsigned int 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.
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.