API Documentation
Loading...
Searching...
No Matches
TimeSpan.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: TimeSpan
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/Time.h>
35#include <NDEVR/LibAssert.h>
36#include <NDEVR/ObjectInfo.h>
37namespace NDEVR
38{
39 /**--------------------------------------------------------------------------------------------------
40 \brief Stores a time span, or difference between two times, with an optional start time.
41
42 Note: timespans can be negative to represent times in the past from an optional start time or positive
43 to represent a time in the future from a given start time.
44 **/
46 {
47 public:
52 constexpr explicit TimeSpan(sint04 elapsed_nano_seconds)
54 , m_elapsed_time(elapsed_nano_seconds)
55 {}
56 constexpr explicit TimeSpan(const sint08 elapsed_nano_seconds)
58 , m_elapsed_time(elapsed_nano_seconds)
59 {}
60 constexpr explicit TimeSpan(uint04 elapsed_nano_seconds)
62 , m_elapsed_time(cast<sint08>(elapsed_nano_seconds))
63 {}
64 constexpr explicit TimeSpan(const uint08 elapsed_nano_seconds)
66 , m_elapsed_time(cast<sint08>(elapsed_nano_seconds))
67 {}
68 constexpr explicit TimeSpan(const fltp08 elapsed_seconds)
70 , m_elapsed_time(cast<sint08>(elapsed_seconds * Time::SECOND))
71 {}
72 TimeSpan(const Time& start, const Time& end)
73 : m_start_time(start)
74 , m_elapsed_time(cast<sint08>(end.getNanoSeconds()) - cast<sint08>(start.getNanoSeconds()))
75 {
76 }
77 constexpr TimeSpan(const Time& start, const sint08& elapsed_nano_seconds)
78 : m_start_time(start)
79 , m_elapsed_time(elapsed_nano_seconds)
80 {}
81
82 constexpr TimeSpan(const Time& start, const fltp08& elapsed_seconds)
83 : m_start_time(start)
84 , m_elapsed_time(cast<sint08>(elapsed_seconds* Time::SECOND))
85 {}
86
87 NDEVR_BASE_API fltp08 elapsedPercent(Time time, bool clip_time) const;
88 NDEVR_BASE_API String getTimeString(const String& format_string) const;
89
90 [[nodiscard]] constexpr Time startTime() const
91 {
92 return m_start_time;
93 }
94 constexpr void setStartTime(Time time)
95 {
96 m_start_time = time;
97 }
98 constexpr void setEndTime(Time time)
99 {
101 }
102 NDEVR_BASE_API bool contains(const Time& time) const;
103
104 [[nodiscard]] NDEVR_BASE_API Time endTime() const;
105
106 [[nodiscard]] constexpr sint08 elapsedNanoseconds() const
107 {
108 return m_elapsed_time;
109 }
110
111 [[nodiscard]] constexpr fltp08 elapsedSeconds() const
112 {
116 }
117
118 template<class t_type = fltp08>
119 [[nodiscard]] constexpr t_type elapsedMilliseconds() const
120 {
124 }
125
126 [[nodiscard]] constexpr fltp08 elapsedMinutes() const
127 {
131 }
134 constexpr bool operator==(const TimeSpan& time) const
135 {
136 return m_elapsed_time == time.m_elapsed_time;
137 }
138 constexpr bool operator!=(const TimeSpan& time) const
139 {
140 return m_elapsed_time != time.m_elapsed_time;
141 }
142 constexpr bool operator>(const TimeSpan& other) const
143 {
144 return m_elapsed_time > other.m_elapsed_time;
145 }
146 constexpr bool operator>=(const TimeSpan& other) const
147 {
148 return m_elapsed_time >= other.m_elapsed_time;
149 }
150 constexpr bool operator<(const TimeSpan& other) const
151 {
152 return m_elapsed_time < other.m_elapsed_time;
153 }
154 constexpr bool operator<=(const TimeSpan& other) const
155 {
156 return m_elapsed_time <= other.m_elapsed_time;
157 }
158
159 constexpr fltp08 operator/(const TimeSpan& time_2) const
160 {
162 }
163 constexpr TimeSpan operator/(const sint08& den) const
164 {
165 return TimeSpan(m_start_time, m_elapsed_time / den);
166 }
167 constexpr TimeSpan operator/(const fltp08& den) const
168 {
170 }
171
172 constexpr TimeSpan operator*(const sint08& mult) const
173 {
174 return TimeSpan(m_start_time, m_elapsed_time * mult);
175 }
176 constexpr TimeSpan operator*(const fltp08& mult) const
177 {
179 }
180 constexpr TimeSpan& operator*=(const sint08& mult)
181 {
183 return *this;
184 }
185 constexpr TimeSpan& operator*=(const fltp08& mult)
186 {
188 return *this;
189 }
190 constexpr TimeSpan operator+(const TimeSpan& other) const
191 {
192 return TimeSpan(m_elapsed_time + other.m_elapsed_time);
193 }
194 constexpr TimeSpan& operator+=(const TimeSpan& other)
195 {
197 return *this;
198 }
199 constexpr TimeSpan operator-(const TimeSpan& other) const
200 {
201 return TimeSpan(m_elapsed_time - other.m_elapsed_time);
202 }
203 constexpr TimeSpan operator-() const
204 {
205 return TimeSpan(-m_elapsed_time);
206 }
207 constexpr TimeSpan& operator-=(const TimeSpan& other)
208 {
210 return *this;
211 }
212 protected:
215 };
216
217 /**--------------------------------------------------------------------------------------------------
218 Struct: ObjectInfo<TimeSpan>
219
220 Information about the object.
221
222 Author: Tyler Parke
223
224 Date: 2020-09-13
225 **/
226 template<>
227 struct ObjectInfo<TimeSpan, false, false>
228 {
229 static const uint01 Dimensions = 0;
230 static const bool Vector = false;
231 static const bool Primitive = true;
232 static const bool Pointer = false;
233 static const bool Unsigned = false;
234 static const bool Float = true;
235 static const bool Integer = false;
236 static const bool Number = true;
237 static const bool String = false;
238 static const bool Color = false;
239 static const bool Buffer = false;
240 static const bool Boolean = false;
241 static constexpr ObjectInfo<TimeSpan, false, false> VectorSub() { return ObjectInfo<TimeSpan, false, false>(); }
242 };
243 NDEVR_BASE_API TimeSpan operator-(const Time& time, const Time& value);
244
245 NDEVR_BASE_API Time operator+(const Time& time, const TimeSpan& value);
246 NDEVR_BASE_API Time operator-(const Time& time, const TimeSpan& value);
249 constexpr TimeSpan abs(const TimeSpan& value)
250 {
251 return value >= TimeSpan(0) ? value : -value;
252 }
253 constexpr TimeSpan operator%(const Time& time, const TimeSpan& duration)
254 {
255 lib_assert(duration.elapsedNanoseconds() > 0, "Bad duration");
257 return TimeSpan(Time(time.getNanoSeconds() - nano), nano);
258 }
259
260
261
265}
#define NDEVR_BASE_API
Definition DLLInfo.h:57
#define lib_assert(expression, message)
Definition LibAssert.h:61
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:56
The core Color class in the NDEVR API. Colors can be defined in several ways. The ACIColor is compact...
Definition Color.h:41
Provides shared ownership of a dynamically allocated object.
Definition Pointer.hpp:71
The core String class for the NDEVR API.
Definition String.h:69
Represents a timestamp with utilities for manipulation and conversion.
Definition Time.h:54
static constexpr uint08 SECOND
Definition Time.h:84
constexpr uint08 getNanoSeconds() const
Gets the timestamp in nanoseconds.
Definition Time.h:185
static constexpr uint08 MINUTE
Definition Time.h:85
static constexpr uint08 MILLISECOND
Definition Time.h:83
Stores a time span, or difference between two times, with an optional start time.
Definition TimeSpan.h:46
constexpr sint08 elapsedNanoseconds() const
Definition TimeSpan.h:106
TimeSpan(const Time &start, const Time &end)
Definition TimeSpan.h:72
constexpr bool operator<=(const TimeSpan &other) const
Definition TimeSpan.h:154
constexpr TimeSpan(const Time &start, const fltp08 &elapsed_seconds)
Definition TimeSpan.h:82
constexpr bool operator==(const TimeSpan &time) const
Definition TimeSpan.h:134
constexpr TimeSpan(const uint08 elapsed_nano_seconds)
Definition TimeSpan.h:64
constexpr TimeSpan operator/(const fltp08 &den) const
Definition TimeSpan.h:167
constexpr TimeSpan()
Definition TimeSpan.h:48
bool contains(const Time &time) const
String getTimeString(const String &format_string) const
constexpr void setEndTime(Time time)
Definition TimeSpan.h:98
constexpr fltp08 elapsedSeconds() const
Definition TimeSpan.h:111
constexpr TimeSpan operator-(const TimeSpan &other) const
Definition TimeSpan.h:199
constexpr TimeSpan & operator+=(const TimeSpan &other)
Definition TimeSpan.h:194
constexpr TimeSpan & operator*=(const sint08 &mult)
Definition TimeSpan.h:180
constexpr t_type elapsedMilliseconds() const
Definition TimeSpan.h:119
constexpr bool operator<(const TimeSpan &other) const
Definition TimeSpan.h:150
constexpr TimeSpan(uint04 elapsed_nano_seconds)
Definition TimeSpan.h:60
constexpr fltp08 elapsedMinutes() const
Definition TimeSpan.h:126
Time m_start_time
Definition TimeSpan.h:213
constexpr TimeSpan(sint04 elapsed_nano_seconds)
Definition TimeSpan.h:52
Time endTime() const
constexpr TimeSpan operator+(const TimeSpan &other) const
Definition TimeSpan.h:190
constexpr bool operator!=(const TimeSpan &time) const
Definition TimeSpan.h:138
constexpr TimeSpan operator*(const fltp08 &mult) const
Definition TimeSpan.h:176
constexpr TimeSpan & operator*=(const fltp08 &mult)
Definition TimeSpan.h:185
sint08 m_elapsed_time
Definition TimeSpan.h:214
constexpr TimeSpan(const Time &start, const sint08 &elapsed_nano_seconds)
Definition TimeSpan.h:77
constexpr bool operator>=(const TimeSpan &other) const
Definition TimeSpan.h:146
constexpr Time startTime() const
Definition TimeSpan.h:90
sint08 elapsedDays() const
constexpr TimeSpan operator/(const sint08 &den) const
Definition TimeSpan.h:163
constexpr TimeSpan operator*(const sint08 &mult) const
Definition TimeSpan.h:172
constexpr TimeSpan & operator-=(const TimeSpan &other)
Definition TimeSpan.h:207
constexpr TimeSpan operator-() const
Definition TimeSpan.h:203
constexpr bool operator>(const TimeSpan &other) const
Definition TimeSpan.h:142
constexpr fltp08 operator/(const TimeSpan &time_2) const
Definition TimeSpan.h:159
constexpr void setStartTime(Time time)
Definition TimeSpan.h:94
constexpr TimeSpan(const fltp08 elapsed_seconds)
Definition TimeSpan.h:68
constexpr TimeSpan(const sint08 elapsed_nano_seconds)
Definition TimeSpan.h:56
fltp08 elapsedPercent(Time time, bool clip_time) const
sint08 elapsedMonths() const
A fixed-size array with better performance compared to dynamic containers.
Definition Vector.hpp:60
Definition ACIColor.h:37
int32_t sint04
-Defines an alias representing a 4 byte, signed integer. -Can represent exact integer values -2147483...
Definition BaseValues.hpp:64
constexpr bool IsInvalid(const t_type &value)
Query if 'value' is valid or invalid. Invalid values should return invalid if used for calculations o...
Definition BaseFunctions.hpp:170
int64_t sint08
-Defines an alias representing an 8 byte, signed integer -Can represent exact integer values -9223372...
Definition BaseValues.hpp:71
TimeSpan operator-(const Time &time, const Time &value)
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:80
String operator+(const String &string_a, const String &string_b)
Definition String.h:827
String & operator+=(String &string, const String &value)
Definition String.h:889
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
Definition BaseValues.hpp:106
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:375
constexpr Angle< t_angle_type > abs(const Angle< t_angle_type > &value)
Changes an input with a negative sign, to a positive sign.
Definition AngleFunctions.h:645
Time & operator-=(Time &time, const TimeSpan &value)
constexpr Vector< t_dims, Angle< t_angle_type > > operator%(const Vector< t_dims, Angle< t_angle_type > > &vec_a, const Vector< t_dims, Angle< t_angle_type > > &vec_b)
Definition AngleFunctions.h:617
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:149
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Definition BaseValues.hpp:233
Information about the object.
Definition ObjectInfo.h:54