1 #include "datautils.h"
2
stringToFilename(const QString & s)3 QString DataUtils::stringToFilename(const QString &s) {
4 QString f = s;
5 f.replace('(', '[');
6 f.replace(')', ']');
7 f.replace('/', ' ');
8 f.replace('\\', ' ');
9 f.replace('<', ' ');
10 f.replace('>', ' ');
11 f.replace(':', ' ');
12 f.replace('"', ' ');
13 f.replace('|', ' ');
14 f.replace('?', ' ');
15 f.replace('*', ' ');
16 f = f.simplified();
17
18 if (!f.isEmpty() && f.at(0) == '.') f = f.midRef(1).trimmed().toString();
19
20 return f;
21 }
22
regioneCode(const QLocale & locale)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);
28 }
29
systemRegioneCode()30 QString DataUtils::systemRegioneCode() {
31 return regioneCode(QLocale::system());
32 }
33
parseIsoPeriod(const QString & isoPeriod)34 uint DataUtils::parseIsoPeriod(const QString &isoPeriod) {
35 uint days = 0, hours = 0, minutes = 0, seconds = 0;
36
37 const int len = isoPeriod.length();
38 int digitStart = -1;
39 for (int i = 0; i < len; ++i) {
40 const QChar c = isoPeriod.at(i);
41 if (c.isDigit()) {
42 if (digitStart == -1) digitStart = i;
43 } else if (digitStart != -1) {
44 if (c == 'H') {
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();
50 }
51 digitStart = -1;
52 }
53 }
54
55 uint period = ((days * 24 + hours) * 60 + minutes) * 60 + seconds;
56 return period;
57 }
58
formatDateTime(const QDateTime & dt)59 QString DataUtils::formatDateTime(const QDateTime &dt) {
60 const qint64 seconds = dt.secsTo(QDateTime::currentDateTimeUtc());
61 QString s;
62 int f = 60;
63 if (seconds < f) {
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);
79 } else {
80 int n = seconds / (60 * 60 * 24 * 30 * 12);
81 s = QCoreApplication::translate("DataUtils", "%n year(s) ago", Q_NULLPTR, n);
82 }
83 return s;
84 }
85
formatDuration(uint secs)86 QString DataUtils::formatDuration(uint secs) {
87 uint d = secs;
88 QString res;
89 uint seconds = d % 60;
90 d /= 60;
91 uint minutes = d % 60;
92 d /= 60;
93 uint hours = d % 24;
94 if (hours == 0) return res.sprintf("%d:%02d", minutes, seconds);
95 return res.sprintf("%d:%02d:%02d", hours, minutes, seconds);
96 }
97
formatCount(int c)98 QString DataUtils::formatCount(int c) {
99 QString s;
100 int f = 1;
101 if (c < 1) {
102 return s;
103 } else if (c < (f *= 1000)) {
104 s = QString::number(c);
105 } else if (c < (f *= 1000)) {
106 int n = c / 1000;
107 s = QString::number(n) +
108 QCoreApplication::translate("DataUtils", "K", "K as in Kilo, i.e. thousands");
109 } else if (c < (f *= 1000)) {
110 int n = c / (1000 * 1000);
111 s = QString::number(n) +
112 QCoreApplication::translate("DataUtils", "M", "M stands for Millions");
113 } else {
114 int n = c / (1000 * 1000 * 1000);
115 s = QString::number(n) +
116 QCoreApplication::translate("DataUtils", "B", "B stands for Billions");
117 }
118
119 return QCoreApplication::translate("DataUtils", "%1 views").arg(s);
120 }
121