atime.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #include <QtCore>
  21. #include <QTime>
  22. #include "src/opl.h"
  23. #include "src/functions/alog.h"
  24. namespace ATime {
  25. /*!
  26. * \brief Converts a QTime to a String to be used in the UI
  27. */
  28. inline const QString toString(const QTime &time, Opl::Time::FlightTimeFormat format = Opl::Time::Default)
  29. {
  30. switch (format) {
  31. case Opl::Time::Default:
  32. return time.toString(QStringLiteral("hh:mm"));
  33. break;
  34. case Opl::Time::Decimal:
  35. return QString::number(((time.hour() * 60 + time.minute() )/60.0), 'f', 2);
  36. break;
  37. default:
  38. return QString();
  39. }
  40. }
  41. /*!
  42. * \brief Converts an integer of minutes as received from the Datbase to a String
  43. */
  44. inline const QString toString(int minutes_in, Opl::Time::FlightTimeFormat format = Opl::Time::Default)
  45. {
  46. switch (format) {
  47. case Opl::Time::Default:
  48. {
  49. QString hour = QString::number(minutes_in / 60);
  50. if (hour.size() < 2) {
  51. hour.prepend(QStringLiteral("0"));
  52. }
  53. QString minute = QString::number(minutes_in % 60);
  54. if (minute.size() < 2) {
  55. minute.prepend(QStringLiteral("0"));
  56. }
  57. return hour + ':' + minute;
  58. }
  59. case Opl::Time::Decimal:
  60. {
  61. int hour = minutes_in / 60;
  62. double minute = (minutes_in % 60) / 60.0;
  63. return QString::number((hour + minute), 'f', 2);
  64. }
  65. default:
  66. return QString();
  67. }
  68. }
  69. inline double toDecimalHours(const QTime &time){
  70. return (time.hour() * 60 + time.minute()) / 60.0;
  71. }
  72. inline int toMinutes(const QTime &time) {return time.hour() * 60 + time.minute();}
  73. inline QTime fromMinutes(int total_minutes)
  74. {
  75. int minute = total_minutes % 60;
  76. int hour = total_minutes / 60;
  77. return QTime(hour, minute, 0);
  78. }
  79. inline const QTime fromString(QString time_string, Opl::Time::FlightTimeFormat format = Opl::Time::Default)
  80. {
  81. switch (format) {
  82. case Opl::Time::Default:
  83. return QTime::fromString(time_string, QStringLiteral("hh:mm"));
  84. break;
  85. case Opl::Time::Decimal:
  86. {
  87. double decimal_time = time_string.toDouble();
  88. int hour = decimal_time;
  89. int minute = round((decimal_time - hour) * 60);
  90. return QTime(hour, minute, 0);
  91. break;
  92. }
  93. default:
  94. return QTime();
  95. }
  96. }
  97. inline const QTime fromString(const char* time_string, Opl::Time::FlightTimeFormat format = Opl::Time::Default)
  98. {
  99. switch (format) {
  100. case Opl::Time::Default:
  101. return QTime::fromString(time_string, QStringLiteral("hh:mm"));
  102. break;
  103. case Opl::Time::Decimal:
  104. {
  105. double decimal_time = QString(time_string).toDouble();
  106. int hour = decimal_time;
  107. int minute = round((decimal_time - hour) * 60);
  108. return QTime(hour, minute, 0);
  109. break;
  110. }
  111. default:
  112. return QTime();
  113. }
  114. }
  115. inline QTime blocktime(const QTime &tofb, const QTime &tonb)
  116. {
  117. QTime blocktime_out(0, 0); // initialise return value at midnight
  118. if (tonb > tofb) { // landing same day
  119. int block_seconds = tofb.secsTo(tonb);
  120. blocktime_out = blocktime_out.addSecs(block_seconds);
  121. } else { // landing next day
  122. QTime midnight(0, 0);
  123. int seconds = tofb.secsTo(midnight);
  124. blocktime_out = blocktime_out.addSecs(seconds);
  125. seconds = midnight.secsTo(tonb);
  126. blocktime_out = blocktime_out.addSecs(seconds);
  127. }
  128. return blocktime_out;
  129. }
  130. inline QTime blocktime(const QString& tofb, const QString& tonb)
  131. {
  132. QTime t_tofb = ATime::fromString(tofb);
  133. QTime t_tonb = ATime::fromString(tonb);
  134. return blocktime(t_tofb, t_tonb);
  135. }
  136. /*!
  137. * \brief blockMinutes calculates the total amount of minutes elapsed between
  138. * tofb and tonb
  139. */
  140. inline int blockMinutes(const QString& tofb, const QString& tonb)
  141. {
  142. const QTime t_tofb = ATime::fromString(tofb);
  143. const QTime t_tonb = ATime::fromString(tonb);
  144. if (t_tofb.isValid() && t_tonb.isValid()) {
  145. const auto tblk = ATime::blocktime(t_tofb, t_tonb);
  146. return ATime::toMinutes(tblk);
  147. } else
  148. return 0;
  149. }
  150. /*!
  151. * \brief blockMinutes calculates the total amount of minutes elapsed between
  152. * tofb and tonb
  153. */
  154. inline int blockMinutes(const QTime& tofb, const QTime& tonb)
  155. {
  156. if (tofb.isValid() && tonb.isValid()) {
  157. const auto tblk = ATime::blocktime(tofb, tonb);
  158. return ATime::toMinutes(tblk);
  159. } else
  160. return 0;
  161. }
  162. /*!
  163. * \brief verifies user input and formats to hh:mm
  164. * if the output is not a valid time, an empty string is returned. Accepts
  165. * input as hh:mm, h:mm, hhmm or hmm.
  166. * \param userinput from a QLineEdit
  167. * \return formatted QString "hh:mm" or Empty String
  168. */
  169. inline const QString formatTimeInput(QString user_input)
  170. {
  171. QTime temp_time; //empty time object is invalid by default
  172. bool contains_seperator = user_input.contains(':');
  173. if (user_input.length() == 4 && !contains_seperator) {
  174. temp_time = QTime::fromString(user_input, QStringLiteral("hhmm"));
  175. } else if (user_input.length() == 3 && !contains_seperator) {
  176. if (user_input.toInt() < 240) { //Qtime is invalid if time is between 000 and 240 for this case
  177. QString tempstring = user_input.prepend(QStringLiteral("0"));
  178. temp_time = QTime::fromString(tempstring, QStringLiteral("hhmm"));
  179. } else {
  180. temp_time = QTime::fromString(user_input, QStringLiteral("Hmm"));
  181. }
  182. } else if (user_input.length() == 4 && contains_seperator) {
  183. temp_time = QTime::fromString(user_input, QStringLiteral("h:mm"));
  184. } else if (user_input.length() == 5 && contains_seperator) {
  185. temp_time = QTime::fromString(user_input, QStringLiteral("hh:mm"));
  186. }
  187. auto output = temp_time.toString(QStringLiteral("hh:mm"));
  188. if (output.isEmpty()) {
  189. DEB << "Time input is invalid.";
  190. }
  191. return output;
  192. }
  193. } // namespace ATime
  194. #endif // ATIME_H