| openPilotLog

atime.h
1 /*
2 *openPilotLog - A FOSS Pilot Logbook Application
3 *Copyright (C) 2020-2021 Felix Turowsky
4 *
5 *This program is free software: you can redistribute it and/or modify
6 *it under the terms of the GNU General Public License as published by
7 *the Free Software Foundation, either version 3 of the License, or
8 *(at your option) any later version.
9 *
10 *This program is distributed in the hope that it will be useful,
11 *but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 *GNU General Public License for more details.
14 *
15 *You should have received a copy of the GNU General Public License
16 *along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18 #ifndef ATIME_H
19 #define ATIME_H
20
21 #include <QtCore>
22 #include <QTime>
23 #include "src/opl.h"
24 #include "src/functions/alog.h"
25
26 namespace ATime {
27
31 inline const QString toString(const QTime &time, Opl::Time::FlightTimeFormat format = Opl::Time::Default)
32 {
33 switch (format) {
34 case Opl::Time::Default:
35 return time.toString(QStringLiteral("hh:mm"));
36 break;
37 case Opl::Time::Decimal:
38 return QString::number(((time.hour() * 60 + time.minute() )/60.0), 'f', 2);
39 break;
40 default:
41 return QString();
42 }
43 }
44
48 inline const QString toString(int minutes_in, Opl::Time::FlightTimeFormat format = Opl::Time::Default)
49 {
50 switch (format) {
51 case Opl::Time::Default:
52 {
53 QString hour = QString::number(minutes_in / 60);
54 if (hour.size() < 2) {
55 hour.prepend(QStringLiteral("0"));
56 }
57 QString minute = QString::number(minutes_in % 60);
58 if (minute.size() < 2) {
59 minute.prepend(QStringLiteral("0"));
60 }
61 return hour + ':' + minute;
62 }
63 case Opl::Time::Decimal:
64 {
65 int hour = minutes_in / 60;
66 double minute = (minutes_in % 60) / 60.0;
67 return QString::number((hour + minute), 'f', 2);
68 }
69 default:
70 return QString();
71 }
72
73 }
74
75 inline double toDecimalHours(const QTime &time){
76 return (time.hour() * 60 + time.minute()) / 60.0;
77 }
78
79 inline QTime qTimefromMinutes(int total_minutes)
80 {
81 int minute = total_minutes % 60;
82 int hour = total_minutes / 60;
83
84 return QTime(hour, minute, 0);
85 }
86
87 inline const QTime fromString(QString time_string, Opl::Time::FlightTimeFormat format = Opl::Time::Default)
88 {
89 switch (format) {
90 case Opl::Time::Default:
91 return QTime::fromString(time_string, QStringLiteral("hh:mm"));
92 break;
93 case Opl::Time::Decimal:
94 {
95 double decimal_time = time_string.toDouble();
96 int hour = decimal_time;
97 int minute = round((decimal_time - hour) * 60);
98 return QTime(hour, minute, 0);
99 break;
100 }
101 default:
102 return QTime();
103 }
104 }
105
106 inline const QTime fromString(const char* time_string, Opl::Time::FlightTimeFormat format = Opl::Time::Default)
107 {
108 switch (format) {
109 case Opl::Time::Default:
110 return QTime::fromString(time_string, QStringLiteral("hh:mm"));
111 break;
112 case Opl::Time::Decimal:
113 {
114 double decimal_time = QString(time_string).toDouble();
115 int hour = decimal_time;
116 int minute = round((decimal_time - hour) * 60);
117 return QTime(hour, minute, 0);
118 break;
119 }
120 default:
121 return QTime();
122 }
123 }
124
125 inline int toMinutes(const QTime &time) {return time.hour() * 60 + time.minute();}
126 inline int toMinutes(const QString &time_string) {return toMinutes(fromString(time_string));}
127
128 inline QTime blocktime(const QTime &tofb, const QTime &tonb)
129 {
130 QTime blocktime_out(0, 0); // initialise return value at midnight
131
132 if (tonb > tofb) { // landing same day
133 int block_seconds = tofb.secsTo(tonb);
134 blocktime_out = blocktime_out.addSecs(block_seconds);
135 } else { // landing next day
136 QTime midnight(0, 0);
137 int seconds = tofb.secsTo(midnight);
138 blocktime_out = blocktime_out.addSecs(seconds);
139 seconds = midnight.secsTo(tonb);
140 blocktime_out = blocktime_out.addSecs(seconds);
141 }
142 return blocktime_out;
143 }
144
145 inline QTime blocktime(const QString& tofb, const QString& tonb)
146 {
147 QTime t_tofb = ATime::fromString(tofb);
148 QTime t_tonb = ATime::fromString(tonb);
149 return blocktime(t_tofb, t_tonb);
150 }
151
156 inline int blockMinutes(const QString& tofb, const QString& tonb)
157 {
158 const QTime t_tofb = ATime::fromString(tofb);
159 const QTime t_tonb = ATime::fromString(tonb);
160 if (t_tofb.isValid() && t_tonb.isValid()) {
161 const auto tblk = ATime::blocktime(t_tofb, t_tonb);
162 return ATime::toMinutes(tblk);
163 } else
164 return 0;
165 }
166
171 inline int blockMinutes(const QTime& tofb, const QTime& tonb)
172 {
173 if (tofb.isValid() && tonb.isValid()) {
174 const auto tblk = ATime::blocktime(tofb, tonb);
175 return ATime::toMinutes(tblk);
176 } else
177 return 0;
178 }
179
187 inline const QString formatTimeInput(QString user_input)
188 {
189 QTime temp_time; //empty time object is invalid by default
190
191 bool contains_seperator = user_input.contains(':');
192 if (user_input.length() == 4 && !contains_seperator) {
193 temp_time = QTime::fromString(user_input, QStringLiteral("hhmm"));
194 } else if (user_input.length() == 3 && !contains_seperator) {
195 if (user_input.toInt() < 240) { //Qtime is invalid if time is between 000 and 240 for this case
196 QString tempstring = user_input.prepend(QStringLiteral("0"));
197 temp_time = QTime::fromString(tempstring, QStringLiteral("hhmm"));
198 } else {
199 temp_time = QTime::fromString(user_input, QStringLiteral("Hmm"));
200 }
201 } else if (user_input.length() == 4 && contains_seperator) {
202 temp_time = QTime::fromString(user_input, QStringLiteral("h:mm"));
203 } else if (user_input.length() == 5 && contains_seperator) {
204 temp_time = QTime::fromString(user_input, QStringLiteral("hh:mm"));
205 }
206
207 auto output = temp_time.toString(QStringLiteral("hh:mm"));
208
209 if (output.isEmpty()) {
210 DEB << "Time input is invalid.";
211 }
212 return output;
213 }
214
215 } // namespace ATime
216
217 #endif // ATIME_H
ACalc::formatTimeInput
QString formatTimeInput(QString user_input)
ACalc::formatTimeInput verifies user input and formats to hh:mm if the output is not a valid time,...
Definition: acalc.cpp:35