Enroute Flight Navigation
A navigation app for VFR pilots
Timespan.h
1/***************************************************************************
2 * Copyright (C) 2019-2024 by Stefan Kebekus *
3 * stefan.kebekus@gmail.com *
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, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21#pragma once
22
23#include <QObject>
24#include <QQmlEngine>
25#include <QtMath>
26
27
28namespace Units {
29
35class Timespan {
36 Q_GADGET
37 QML_VALUE_TYPE(time)
38
39 public:
46 static constexpr auto fromH(double timeInH) -> Timespan {
47 Timespan result;
48 result.m_timeInS = timeInH*Seconds_per_Hour;
49 return result;
50 }
51
58 static constexpr Timespan fromMS(double timeInMS)
59 {
60 Timespan result;
61 result.m_timeInS = timeInMS/1000.0;
62 return result;
63 }
64
71 static constexpr auto fromS(double timeInS)
72 {
73 Timespan result;
74 result.m_timeInS = timeInS;
75 return result;
76 }
77
82 [[nodiscard]] Q_INVOKABLE bool isFinite() const
83 {
84 return std::isfinite(m_timeInS);
85 }
86
91 [[nodiscard]] Q_INVOKABLE bool isNegative() const
92 {
93 return m_timeInS < 0.0;
94 }
95
103 {
104 m_timeInS += other.m_timeInS;
105 return *this;
106 }
107
114 [[nodiscard]] Q_INVOKABLE std::partial_ordering operator<=>(const Units::Timespan& rhs) const = default;
115
120 [[nodiscard]] Q_INVOKABLE double toS() const
121 {
122 return m_timeInS;
123 }
124
129 [[nodiscard]] Q_INVOKABLE double toM() const
130 {
131 return m_timeInS / Seconds_per_Minute;
132 }
133
138 [[nodiscard]] Q_INVOKABLE double toH() const
139 {
140 return m_timeInS / Seconds_per_Hour;
141 }
142
147 [[nodiscard]] Q_INVOKABLE QString toHoursAndMinutes() const;
148
149 private:
150 static constexpr double Seconds_per_Minute = 60.0;
151 static constexpr double Seconds_per_Hour = 60.0 * 60.0;
152
153 // Speed in meters per second
154 double m_timeInS {NAN};
155 };
156} // namespace Units
157
158
159// Declare meta types
160Q_DECLARE_METATYPE(Units::Timespan)
Convenience class for time computations.
Definition Timespan.h:35
static constexpr auto fromH(double timeInH) -> Timespan
Constructs a time.
Definition Timespan.h:46
Q_INVOKABLE QString toHoursAndMinutes() const
Convert time to string.
static constexpr auto fromS(double timeInS)
Constructs a time.
Definition Timespan.h:71
static constexpr Timespan fromMS(double timeInMS)
Constructs a time.
Definition Timespan.h:58
Q_INVOKABLE Units::Timespan & operator+=(Units::Timespan other)
Add time to this time.
Definition Timespan.h:102
Q_INVOKABLE bool isNegative() const
Checks if the time is negative.
Definition Timespan.h:91
Q_INVOKABLE double toM() const
Convert time to minutes.
Definition Timespan.h:129
Q_INVOKABLE double toS() const
Convert time to seconds.
Definition Timespan.h:120
Q_INVOKABLE std::partial_ordering operator<=>(const Units::Timespan &rhs) const =default
Comparison.
Q_INVOKABLE bool isFinite() const
Checks if the time is valid.
Definition Timespan.h:82
Q_INVOKABLE double toH() const
Convert time to hours.
Definition Timespan.h:138
Conversion between units used in aviation.
Definition Angle.h:34