3 QString DataUtils::stringToFilename(const QString &s) {
18 if (!f.isEmpty() && f.at(0) == '.') f = f.midRef(1).trimmed().toString();
23 QString DataUtils::regioneCode(const QLocale &locale) {
24 QString name = locale.name();
25 int index = name.indexOf('_');
26 if (index == -1) return QString();
27 return name.right(index);
30 QString DataUtils::systemRegioneCode() {
31 return regioneCode(QLocale::system());
34 uint DataUtils::parseIsoPeriod(const QString &isoPeriod) {
35 uint days = 0, hours = 0, minutes = 0, seconds = 0;
37 const int len = isoPeriod.length();
39 for (int i = 0; i < len; ++i) {
40 const QChar c = isoPeriod.at(i);
42 if (digitStart == -1) digitStart = i;
43 } else if (digitStart != -1) {
45 hours = QStringRef(&isoPeriod, digitStart, i - digitStart).toUInt();
46 } else if (c == 'M') {
47 minutes = QStringRef(&isoPeriod, digitStart, i - digitStart).toUInt();
48 } else if (c == 'S') {
49 seconds = QStringRef(&isoPeriod, digitStart, i - digitStart).toUInt();
55 uint period = ((days * 24 + hours) * 60 + minutes) * 60 + seconds;
59 QString DataUtils::formatDateTime(const QDateTime &dt) {
60 const qint64 seconds = dt.secsTo(QDateTime::currentDateTime());
64 s = QCoreApplication::translate("DataUtils", "Just now");
65 } else if (seconds < (f *= 60)) {
66 s = QCoreApplication::translate("DataUtils", "%n minute(s) ago", Q_NULLPTR, seconds / 60);
67 } else if (seconds < (f *= 24)) {
68 int n = seconds / (60 * 60);
69 s = QCoreApplication::translate("DataUtils", "%n hour(s) ago", Q_NULLPTR, n);
70 } else if (seconds < (f *= 7)) {
71 int n = seconds / (60 * 60 * 24);
72 s = QCoreApplication::translate("DataUtils", "%n day(s) ago", Q_NULLPTR, n);
73 } else if (seconds < (f = 60 * 60 * 24 * 30)) {
74 int n = seconds / (60 * 60 * 24 * 7);
75 s = QCoreApplication::translate("DataUtils", "%n week(s) ago", Q_NULLPTR, n);
76 } else if (seconds < (f = 60 * 60 * 24 * 365)) {
77 int n = seconds / (60 * 60 * 24 * 30);
78 s = QCoreApplication::translate("DataUtils", "%n month(s) ago", Q_NULLPTR, n);
80 s = dt.date().toString(Qt::DefaultLocaleShortDate);
85 QString DataUtils::formatDuration(uint secs) {
88 uint seconds = d % 60;
90 uint minutes = d % 60;
93 if (hours == 0) return res.sprintf("%d:%02d", minutes, seconds);
94 return res.sprintf("%d:%02d:%02d", hours, minutes, seconds);
97 QString DataUtils::formatCount(int c) {
102 } else if (c < (f *= 1000)) {
103 s = QString::number(c);
104 } else if (c < (f *= 1000)) {
106 s = QString::number(n) +
107 QCoreApplication::translate("DataUtils", "K", "K as in Kilo, i.e. thousands");
108 } else if (c < (f *= 1000)) {
109 int n = c / (1000 * 1000);
110 s = QString::number(n) +
111 QCoreApplication::translate("DataUtils", "M", "M stands for Millions");
113 int n = c / (1000 * 1000 * 1000);
114 s = QString::number(n) +
115 QCoreApplication::translate("DataUtils", "B", "B stands for Billions");
118 return QCoreApplication::translate("DataUtils", "%1 views").arg(s);