-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.cpp
92 lines (75 loc) · 2.78 KB
/
utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Radiator
* Copyright (C) 2018 Stefan Böhmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "utils.h"
QString Utils::time_elapsed_string(const QDateTime &from, const QDateTime &to)
{
static qint64 min_in_secs = 60;
static qint64 hour_in_secs = min_in_secs * 60;
static qint64 day_in_secs = hour_in_secs * 24;
if(!from.isValid() || !to.isValid()) {
return QString();
}
// bug: use days since or so and not years diff (consider 31.12.17 to 01.01.18 -> years = 1)
int years = to.date().year() - from.date().year();
if(years == 0) {
if(from.secsTo(to) < min_in_secs) {
return QObject::tr("just now");
}
if(from.secsTo(to) < hour_in_secs) {
return QObject::tr("%n minute(s) ago", "", static_cast<int>(from.secsTo(to) / min_in_secs));
}
if(from.secsTo(to) < day_in_secs) {
return QObject::tr("%n hour(s) ago", "", static_cast<int>(from.secsTo(to) / hour_in_secs));
}
if(from.daysTo(to) < 14) {
return QObject::tr("%n day(s) ago", "", static_cast<int>(from.daysTo(to)));
}
if(from.daysTo(to) < from.date().daysInMonth()) {
return QObject::tr("%n week(s) ago", "", static_cast<int>(from.daysTo(to) / 7));
}
return QObject::tr("%n month(s) ago", "", to.date().month() - from.date().month());
}
if(years > 3) {
return QObject::tr("antediluvian");
}
if(years > 1) {
return QObject::tr("prehistoric");
}
return QObject::tr("a long time ago");
}
QString Utils::human_time_diff(qint64 seconds)
{
qint64 days = seconds / 60 / 60 / 24;
seconds -= days * 60 * 60 * 24;
qint64 hours = seconds / 60 / 60;
seconds -= hours * 60 * 60;
qint64 minutes = seconds / 60;
seconds -= minutes * 60;
QString str;
if(days >= 1) {
str += QString("%1d ").arg(days);
}
else if(!str.isEmpty() || hours >= 1) {
str += QString(" %1h").arg(hours);
}
else if(!str.isEmpty() || minutes >= 1) {
str += QString(" %1m").arg(minutes);
}
str += QString(" %1s").arg(seconds, str.isEmpty() ? 1 : 2, 10, QChar('0'));
return str.trimmed();
}