| 295 | } |
| 296 | |
| 297 | QString Util::formatTimeString(quint64 totalNanoseconds, bool shortForm) |
| 298 | { |
| 299 | if (totalNanoseconds < 1000) { |
| 300 | return QString::number(totalNanoseconds) + QLatin1String("ns"); |
| 301 | } |
| 302 | |
| 303 | auto format = [](quint64 fragment, int precision) -> QString { |
| 304 | return QString::number(fragment).rightJustified(precision, QLatin1Char('0')); |
| 305 | }; |
| 306 | |
| 307 | const auto microseconds = (totalNanoseconds / 1000) % 1000; |
| 308 | if (totalNanoseconds < 1000000) { |
| 309 | const auto nanoseconds = totalNanoseconds % 1000; |
| 310 | if (shortForm) { |
| 311 | return QString::number(microseconds) + QStringLiteral("µs"); |
| 312 | } |
| 313 | return format(microseconds, 3) + QLatin1Char('.') + format(nanoseconds, 3) + QStringLiteral("µs"); |
| 314 | } |
| 315 | |
| 316 | const auto milliseconds = (totalNanoseconds / 1000000) % 1000; |
| 317 | if (totalNanoseconds < 1000000000) { |
| 318 | if (shortForm) { |
| 319 | return QString::number(milliseconds) + QLatin1String("ms"); |
| 320 | } |
| 321 | return format(milliseconds, 3) + QLatin1Char('.') + format(microseconds, 3) + QLatin1String("ms"); |
| 322 | } |
| 323 | |
| 324 | const auto totalSeconds = totalNanoseconds / 1000000000; |
| 325 | const auto days = totalSeconds / 60 / 60 / 24; |
| 326 | const auto hours = (totalSeconds / 60 / 60) % 24; |
| 327 | const auto minutes = (totalSeconds / 60) % 60; |
| 328 | const auto seconds = totalSeconds % 60; |
| 329 | |
| 330 | auto optional = [](quint64 fragment, const char* unit) -> QString { |
| 331 | if (fragment > 0) |
| 332 | return QString::number(fragment) + QLatin1String(unit) + QLatin1Char(' '); |
| 333 | return {}; |
| 334 | }; |
| 335 | if (shortForm) { |
| 336 | return optional(days, "d") + optional(hours, "h") + optional(minutes, "min") + QString::number(seconds) |
| 337 | + QLatin1Char('s'); |
| 338 | } |
| 339 | return optional(days, "d") + optional(hours, "h") + optional(minutes, "min") + format(seconds, 2) + QLatin1Char('.') |
| 340 | + format(milliseconds, 3) + QLatin1Char('s'); |
| 341 | } |
| 342 | |
| 343 | QString Util::formatFrequency(quint64 occurrences, quint64 nanoseconds) |
| 344 | { |
nothing calls this directly
no outgoing calls
no test coverage detected