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 int toMinutes(const QTime &time) {return time.hour() * 60 + time.minute();}
80 
81 inline QTime fromMinutes(int total_minutes)
82 {
83  int minute = total_minutes % 60;
84  int hour = total_minutes / 60;
85 
86  return QTime(hour, minute, 0);
87 }
88 
89 inline const QTime fromString(QString time_string, Opl::Time::FlightTimeFormat format = Opl::Time::Default)
90 {
91  switch (format) {
92  case Opl::Time::Default:
93  return QTime::fromString(time_string, QStringLiteral("hh:mm"));
94  break;
95  case Opl::Time::Decimal:
96  {
97  double decimal_time = time_string.toDouble();
98  int hour = decimal_time;
99  int minute = round((decimal_time - hour) * 60);
100  return QTime(hour, minute, 0);
101  break;
102  }
103  default:
104  return QTime();
105  }
106 }
107 
108 inline const QTime fromString(const char* time_string, Opl::Time::FlightTimeFormat format = Opl::Time::Default)
109 {
110  switch (format) {
111  case Opl::Time::Default:
112  return QTime::fromString(time_string, QStringLiteral("hh:mm"));
113  break;
114  case Opl::Time::Decimal:
115  {
116  double decimal_time = QString(time_string).toDouble();
117  int hour = decimal_time;
118  int minute = round((decimal_time - hour) * 60);
119  return QTime(hour, minute, 0);
120  break;
121  }
122  default:
123  return QTime();
124  }
125 }
126 
127 inline QTime blocktime(const QTime &tofb, const QTime &tonb)
128 {
129  QTime blocktime_out(0, 0); // initialise return value at midnight
130 
131  if (tonb > tofb) { // landing same day
132  int block_seconds = tofb.secsTo(tonb);
133  blocktime_out = blocktime_out.addSecs(block_seconds);
134  } else { // landing next day
135  QTime midnight(0, 0);
136  int seconds = tofb.secsTo(midnight);
137  blocktime_out = blocktime_out.addSecs(seconds);
138  seconds = midnight.secsTo(tonb);
139  blocktime_out = blocktime_out.addSecs(seconds);
140  }
141  return blocktime_out;
142 }
143 
151 inline const QString formatTimeInput(QString user_input)
152 {
153  QTime temp_time; //empty time object is invalid by default
154 
155  bool contains_seperator = user_input.contains(':');
156  if (user_input.length() == 4 && !contains_seperator) {
157  temp_time = QTime::fromString(user_input, QStringLiteral("hhmm"));
158  } else if (user_input.length() == 3 && !contains_seperator) {
159  if (user_input.toInt() < 240) { //Qtime is invalid if time is between 000 and 240 for this case
160  QString tempstring = user_input.prepend(QStringLiteral("0"));
161  temp_time = QTime::fromString(tempstring, QStringLiteral("hhmm"));
162  } else {
163  temp_time = QTime::fromString(user_input, QStringLiteral("Hmm"));
164  }
165  } else if (user_input.length() == 4 && contains_seperator) {
166  temp_time = QTime::fromString(user_input, QStringLiteral("h:mm"));
167  } else if (user_input.length() == 5 && contains_seperator) {
168  temp_time = QTime::fromString(user_input, QStringLiteral("hh:mm"));
169  }
170 
171  auto output = temp_time.toString(QStringLiteral("hh:mm"));
172 
173  if (output.isEmpty()) {
174  DEB << "Time input is invalid.";
175  }
176  return output;
177 }
178 
179 } // namespace ATime
180 
181 #endif // ATIME_H
ACalc::blocktime
QT_DEPRECATED QTime blocktime(QTime tofb, QTime tonb)
ACalc::blocktime Calculates Block Time for a given departure and arrival time.
Definition: acalc.h:43
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:34