aboutsummaryrefslogtreecommitdiff
path: root/launcher/MMCTime.cpp
diff options
context:
space:
mode:
authorTheKodeToad <TheKodeToad@proton.me>2023-06-14 20:05:02 +0100
committerGitHub <noreply@github.com>2023-06-14 20:05:02 +0100
commit3526d00a2330aad7d01f345816505c4d1cd5b096 (patch)
tree31b34c829f21ef6f49bc3dda6bd1fc6b55ac7029 /launcher/MMCTime.cpp
parent79ce7eb1fc9351e689e7106e3dc3a641d9614c9a (diff)
parenta4502f44c291fad2174e84bf883cdb754aa08e28 (diff)
downloadPrismLauncher-3526d00a2330aad7d01f345816505c4d1cd5b096.tar.gz
PrismLauncher-3526d00a2330aad7d01f345816505c4d1cd5b096.tar.bz2
PrismLauncher-3526d00a2330aad7d01f345816505c4d1cd5b096.zip
Merge branch 'develop' into feat/dont-hide-settings
Signed-off-by: TheKodeToad <TheKodeToad@proton.me>
Diffstat (limited to 'launcher/MMCTime.cpp')
-rw-r--r--launcher/MMCTime.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/launcher/MMCTime.cpp b/launcher/MMCTime.cpp
index 70bc4135..1702ec06 100644
--- a/launcher/MMCTime.cpp
+++ b/launcher/MMCTime.cpp
@@ -18,6 +18,8 @@
#include <MMCTime.h>
#include <QObject>
+#include <QDateTime>
+#include <QTextStream>
QString Time::prettifyDuration(int64_t duration) {
int seconds = (int) (duration % 60);
@@ -36,3 +38,65 @@ QString Time::prettifyDuration(int64_t duration) {
}
return QObject::tr("%1d %2h %3min").arg(days).arg(hours).arg(minutes);
}
+
+QString Time::humanReadableDuration(double duration, int precision) {
+
+ using days = std::chrono::duration<int, std::ratio<86400>>;
+
+ QString outStr;
+ QTextStream os(&outStr);
+
+ bool neg = false;
+ if (duration < 0) {
+ neg = true; // flag
+ duration *= -1; // invert
+ }
+
+ auto std_duration = std::chrono::duration<double>(duration);
+ auto d = std::chrono::duration_cast<days>(std_duration);
+ std_duration -= d;
+ auto h = std::chrono::duration_cast<std::chrono::hours>(std_duration);
+ std_duration -= h;
+ auto m = std::chrono::duration_cast<std::chrono::minutes>(std_duration);
+ std_duration -= m;
+ auto s = std::chrono::duration_cast<std::chrono::seconds>(std_duration);
+ std_duration -= s;
+ auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(std_duration);
+
+ auto dc = d.count();
+ auto hc = h.count();
+ auto mc = m.count();
+ auto sc = s.count();
+ auto msc = ms.count();
+
+ if (neg) {
+ os << '-';
+ }
+ if (dc) {
+ os << dc << QObject::tr("days");
+ }
+ if (hc) {
+ if (dc)
+ os << " ";
+ os << qSetFieldWidth(2) << hc << QObject::tr("h"); // hours
+ }
+ if (mc) {
+ if (dc || hc)
+ os << " ";
+ os << qSetFieldWidth(2) << mc << QObject::tr("m"); // minutes
+ }
+ if (dc || hc || mc || sc) {
+ if (dc || hc || mc)
+ os << " ";
+ os << qSetFieldWidth(2) << sc << QObject::tr("s"); // seconds
+ }
+ if ((msc && (precision > 0)) || !(dc || hc || mc || sc)) {
+ if (dc || hc || mc || sc)
+ os << " ";
+ os << qSetFieldWidth(0) << qSetRealNumberPrecision(precision) << msc << QObject::tr("ms"); // miliseconds
+ }
+
+ os.flush();
+
+ return outStr;
+} \ No newline at end of file