-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.cpp
137 lines (111 loc) · 4.48 KB
/
misc.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* This file is part of EasyTimeTracker.
*
* EasyTimeTracker 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.
*
* EasyTimeTracker 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 EasyTimeTracker. If not, see <http://www.gnu.org/licenses/>.
*
* EasyTimeTracker: First creation by 2BlackCoffees: http://www.twoblackcoffees.com/
*
**/
#include "misc.h"
const QDate MiscDateTime::mInitDate = QDate(1, 1, 1);
const QTime MiscDateTime::mInitTime = QTime(0, 0, 0);
QString Misc::getUserName() {
#ifndef _WIN32
return getenv("USER"); ///for MAC or Linux
#else
return std::getenv("USERNAME");
#endif
}
// =============================================================================
QTime MiscDateTime::stringToTime(const QString& text) {
QStringList listText = text.split(":"/*QRegExp("[:-\\.]")*/);
int hour = 0;
int minute = 0;
int second = 0;
if(listText.size() > 0) hour = listText.at(0).toInt();
if(listText.size() > 1) minute = listText.at(1).toInt();
if(listText.size() > 2) second = listText.at(2).toInt();
QTime tmpTime = QTime(hour, minute, second);
if(!tmpTime.isValid()) {
throw (TTExceptionWrongBaseTime(QString(
QObject::tr("Time defined by hour %1, minute %2, second %3 is invalid.").
arg(hour, minute, second))));
}
return tmpTime;
}
// =============================================================================
int MiscDateTime::stringHoursToSeconds(const QString& text) {
int allSecs = 0;
QString tmpText = text;
bool negativ = tmpText.length() > 0 && tmpText.at(0) == '-';
if(negativ) {
tmpText = tmpText.mid(1);
}
QStringList listText = tmpText.split(":"/*QRegExp("[:-\\.]")*/);
if(listText.size() > 2) {
allSecs = listText.at(0).toInt() * 3600 +
listText.at(1).toInt() * 60 +
listText.at(2).toInt();
} else if(listText.size() > 1) {
allSecs = listText.at(0).toInt() * 3600 +
listText.at(1).toInt() * 60;
} else if(listText.size() > 0) {
allSecs = listText.at(0).toInt() * 3600;
}
if(negativ) allSecs = -allSecs;
return allSecs;
}
// =============================================================================
QDate MiscDateTime::stringToDate(const QString& text) {
QStringList listText = text.split("-"/*QRegExp("[:-\\.]")*/);
int day = 1;
int month = 1;
int year = 1;
if(listText.size() > 0) day = listText.at(0).toInt();
if(listText.size() > 1) month = listText.at(1).toInt();
if(listText.size() > 2) year = listText.at(2).toInt();
DEBUG(QString("stringToDate: Day: %1, Month: %2, Year: %3").arg(day).arg(month).arg(year));
QDate tmpDate = QDate(year, month, day);
if(!tmpDate.isValid()) {
throw (TTExceptionWrongBaseDate(QString(
QObject::tr("Date defined by day %1, month %2, year %3 is invalid.").
arg(day, month, year))));
}
return tmpDate;
}
// =============================================================================
QDateTime MiscDateTime::stringToDateTime(const QString& text) {
QStringList listText = text.split(" ");
if(listText.size() > 1) {
DEBUG(QString("String1: %1, String2: %2").arg(listText.at(1)).arg(listText.at(0)));
return QDateTime(stringToDate(listText.at(0)), stringToTime(listText.at(1)));
}
if(listText.size() > 0) {
return QDateTime(mInitDate, stringToTime(listText.at(0)));
}
return QDateTime(mInitDate, mInitTime);
}
// =============================================================================
QString MiscDateTime::secondsToHours(int seconds) {
bool negativ = seconds < 0;
if(negativ) {
seconds = -seconds;
}
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
QString out = QString("%1:%2").arg(hours).arg(minutes, 2, 10, (const QChar)'0');
if(negativ) {
out = "-" + out;
}
return out;
}