X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fdatautils.cpp;h=1de28d5c12827d34bbb779d13b1d44bb259294f4;hb=fe9d17324e88a65d4f28afccf21857c5a5e60649;hp=82fb53f3d3fe5d23987d446b2295a309ae10196d;hpb=29f9a13b38a0547b70236d24300668385e1dbf6e;p=minitube diff --git a/src/datautils.cpp b/src/datautils.cpp index 82fb53f..1de28d5 100644 --- a/src/datautils.cpp +++ b/src/datautils.cpp @@ -15,8 +15,7 @@ QString DataUtils::stringToFilename(const QString &s) { f.replace('*', ' '); f = f.simplified(); - if (!f.isEmpty() && f.at(0) == '.') - f = f.mid(1).trimmed(); + if (!f.isEmpty() && f.at(0) == '.') f = f.midRef(1).trimmed().toString(); return f; } @@ -33,47 +32,24 @@ QString DataUtils::systemRegioneCode() { } uint DataUtils::parseIsoPeriod(const QString &isoPeriod) { - // QTime time = QTime::fromString("1mm12car00", "PT8M50S"); - // ^P((\d+Y)?(\d+M)?(\d+W)?(\d+D)?)?(T(\d+H)?(\d+M)?(\d+S)?)?$ - /* - QRegExp isoPeriodRE("^PT(\d+H)?(\d+M)?(\d+S)?)?$"); - if (!isoPeriodRE.indexIn(isoPeriod)) { - qWarning() << "Cannot parse ISO period" << isoPeriod; - continue; - } - - int totalCaptures = isoPeriodRE.capturedTexts(); - for (int i = totalCaptures; i > 0; --i) { - - } - */ - uint days = 0, hours = 0, minutes = 0, seconds = 0; - QByteArray ba = isoPeriod.toLocal8Bit(); - const char *ptr = ba.data(); - - while (*ptr) { - if(*ptr == 'P' || *ptr == 'T') { - ptr++; - continue; + const int len = isoPeriod.length(); + int digitStart = -1; + for (int i = 0; i < len; ++i) { + const QChar c = isoPeriod.at(i); + if (c.isDigit()) { + if (digitStart == -1) digitStart = i; + } else if (digitStart != -1) { + if (c == 'H') { + hours = QStringRef(&isoPeriod, digitStart, i - digitStart).toUInt(); + } else if (c == 'M') { + minutes = QStringRef(&isoPeriod, digitStart, i - digitStart).toUInt(); + } else if (c == 'S') { + seconds = QStringRef(&isoPeriod, digitStart, i - digitStart).toUInt(); + } + digitStart = -1; } - - int value, charsRead; - char type; - if (sscanf(ptr, "%d%c%n", &value, &type, &charsRead) != 2) - continue; - - if (type == 'D') - days = value; - else if (type == 'H') - hours = value; - else if (type == 'M') - minutes = value; - else if (type == 'S') - seconds = value; - - ptr += charsRead; } uint period = ((days * 24 + hours) * 60 + minutes) * 60 + seconds; @@ -81,23 +57,28 @@ uint DataUtils::parseIsoPeriod(const QString &isoPeriod) { } QString DataUtils::formatDateTime(const QDateTime &dt) { - const qint64 seconds = dt.secsTo(QDateTime::currentDateTime()); + const qint64 seconds = dt.secsTo(QDateTime::currentDateTimeUtc()); QString s; int f = 60; if (seconds < f) { - s = qApp->translate("DataUtils", "Just now"); + s = QCoreApplication::translate("DataUtils", "Just now"); } else if (seconds < (f *= 60)) { - s = qApp->translate("DataUtils", "%n minute(s) ago", "", seconds / 60); + s = QCoreApplication::translate("DataUtils", "%n minute(s) ago", Q_NULLPTR, seconds / 60); } else if (seconds < (f *= 24)) { - s = qApp->translate("DataUtils", "%n hour(s) ago", "", seconds / (60*60)); + int n = seconds / (60 * 60); + s = QCoreApplication::translate("DataUtils", "%n hour(s) ago", Q_NULLPTR, n); } else if (seconds < (f *= 7)) { - s = qApp->translate("DataUtils", "%n day(s) ago", "", seconds / (60*60*24)); - } else if (seconds < (f = 60*60*24*30)) { - s = qApp->translate("DataUtils", "%n weeks(s) ago", "", seconds / (60*60*24*7)); - } else if (seconds < (f = 60*60*24*365)) { - s = qApp->translate("DataUtils", "%n month(s) ago", "", seconds / (60*60*24*30)); + int n = seconds / (60 * 60 * 24); + s = QCoreApplication::translate("DataUtils", "%n day(s) ago", Q_NULLPTR, n); + } else if (seconds < (f = 60 * 60 * 24 * 30)) { + int n = seconds / (60 * 60 * 24 * 7); + s = QCoreApplication::translate("DataUtils", "%n week(s) ago", Q_NULLPTR, n); + } else if (seconds < (f = 60 * 60 * 24 * 365)) { + int n = seconds / (60 * 60 * 24 * 30); + s = QCoreApplication::translate("DataUtils", "%n month(s) ago", Q_NULLPTR, n); } else { - s = dt.date().toString(Qt::DefaultLocaleShortDate); + int n = seconds / (60 * 60 * 24 * 30 * 12); + s = QCoreApplication::translate("DataUtils", "%n year(s) ago", Q_NULLPTR, n); } return s; } @@ -110,7 +91,30 @@ QString DataUtils::formatDuration(uint secs) { uint minutes = d % 60; d /= 60; uint hours = d % 24; - if (hours == 0) - return res.sprintf("%d:%02d", minutes, seconds); + if (hours == 0) return res.sprintf("%d:%02d", minutes, seconds); return res.sprintf("%d:%02d:%02d", hours, minutes, seconds); } + +QString DataUtils::formatCount(int c) { + QString s; + int f = 1; + if (c < 1) { + return s; + } else if (c < (f *= 1000)) { + s = QString::number(c); + } else if (c < (f *= 1000)) { + int n = c / 1000; + s = QString::number(n) + + QCoreApplication::translate("DataUtils", "K", "K as in Kilo, i.e. thousands"); + } else if (c < (f *= 1000)) { + int n = c / (1000 * 1000); + s = QString::number(n) + + QCoreApplication::translate("DataUtils", "M", "M stands for Millions"); + } else { + int n = c / (1000 * 1000 * 1000); + s = QString::number(n) + + QCoreApplication::translate("DataUtils", "B", "B stands for Billions"); + } + + return QCoreApplication::translate("DataUtils", "%1 views").arg(s); +}