NDEVR
API Documentation
TimeSpan.h
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/BufferBase.h>
37namespace NDEVR
38{
46 {
47 public:
51 constexpr TimeSpan()
52 : m_start_time(Constant<Time>::Invalid)
53 , m_elapsed_time(Constant<sint08>::Invalid)
54 {}
55
59 constexpr explicit TimeSpan(sint04 elapsed_nano_seconds)
60 : m_start_time(Constant<Time>::Invalid)
61 , m_elapsed_time(elapsed_nano_seconds)
62 {}
63
67 constexpr explicit TimeSpan(const sint08 elapsed_nano_seconds)
68 : m_start_time(Constant<Time>::Invalid)
69 , m_elapsed_time(elapsed_nano_seconds)
70 {}
71
75 constexpr explicit TimeSpan(uint04 elapsed_nano_seconds)
76 : m_start_time(Constant<Time>::Invalid)
77 , m_elapsed_time(cast<sint08>(elapsed_nano_seconds))
78 {}
79
83 constexpr explicit TimeSpan(const uint08 elapsed_nano_seconds)
84 : m_start_time(Constant<Time>::Invalid)
85 , m_elapsed_time(cast<sint08>(elapsed_nano_seconds))
86 {}
87
91 constexpr explicit TimeSpan(const fltp08 elapsed_seconds)
92 : m_start_time(Constant<Time>::Invalid)
93 , m_elapsed_time(cast<sint08>(elapsed_seconds * Time::SECOND))
94 {}
95
100 TimeSpan(const Time& start, const Time& end)
101 : m_start_time(start)
102 , m_elapsed_time(cast<sint08>(end.getNanoSeconds()) - cast<sint08>(start.getNanoSeconds()))
103 {
104 }
105
110 constexpr TimeSpan(const Time& start, const sint08& elapsed_nano_seconds)
111 : m_start_time(start)
112 , m_elapsed_time(elapsed_nano_seconds)
113 {}
114
120 constexpr TimeSpan(const Time& start, const fltp08& elapsed_seconds)
121 : m_start_time(start)
122 , m_elapsed_time(cast<sint08>(elapsed_seconds* Time::SECOND))
123 {}
124
131 NDEVR_BASE_API fltp08 elapsedPercent(Time time, bool clip_time) const;
137 NDEVR_BASE_API String getTimeString(const StringView& format_string) const;
138
143 [[nodiscard]] constexpr Time startTime() const
144 {
145 return m_start_time;
146 }
147
151 constexpr void setStartTime(Time time)
152 {
153 m_start_time = time;
154 }
155
159 constexpr void setEndTime(Time time)
160 {
161 m_elapsed_time = (time.getNanoSeconds() - m_start_time.getNanoSeconds());
162 }
163
167 constexpr void setElapsedSeconds(fltp08 elapsed_seconds)
168 {
169 m_elapsed_time = cast<sint08>(elapsed_seconds * Time::SECOND);
170 }
171
175 constexpr void setElapsedMinutes(fltp08 elapsed_minutes)
176 {
177 m_elapsed_time = cast<sint08>(elapsed_minutes * Time::MINUTE);
178 }
179
183 constexpr void setElapsedHours(fltp08 elapsed_minutes)
184 {
185 m_elapsed_time = cast<sint08>(elapsed_minutes * Time::HOUR);
186 }
187
192 NDEVR_BASE_API bool contains(const Time& time) const;
193
198 [[nodiscard]] NDEVR_BASE_API Time endTime() const;
199
204 [[nodiscard]] constexpr sint08 elapsedNanoseconds() const
205 {
206 return m_elapsed_time;
207 }
208
213 [[nodiscard]] constexpr fltp08 elapsedSeconds() const
214 {
216 return Constant<fltp08>::Invalid;
217 return cast<fltp08>(m_elapsed_time) / cast<fltp08>(Time::SECOND);
218 }
219
224 template<class t_type = fltp08>
225 [[nodiscard]] constexpr t_type elapsedMilliseconds() const
226 {
228 return Constant<fltp08>::Invalid;
229 return cast<t_type>(m_elapsed_time) / cast<t_type>(Time::MILLISECOND);
230 }
231
236 [[nodiscard]] constexpr fltp08 elapsedMinutes() const
237 {
239 return Constant<fltp08>::Invalid;
240 return cast<fltp08>(m_elapsed_time) / cast<fltp08>(Time::MINUTE);
241 }
242
246 [[nodiscard]] constexpr fltp08 elapsedHours() const
247 {
249 return Constant<fltp08>::Invalid;
250 return cast<fltp08>(m_elapsed_time) / cast<fltp08>(Time::HOUR);
251 }
252
256 NDEVR_BASE_API sint08 elapsedDays() const;
261 NDEVR_BASE_API sint08 elapsedMonths() const;
267 constexpr bool operator==(const TimeSpan& time) const
268 {
269 return m_start_time == time.m_start_time
271 }
272
277 constexpr bool operator!=(const TimeSpan& time) const
278 {
279 return m_start_time != time.m_start_time
281 }
282
287 constexpr bool operator>(const TimeSpan& other) const
288 {
289 return m_elapsed_time > other.m_elapsed_time;
290 }
291
296 constexpr bool operator>=(const TimeSpan& other) const
297 {
298 return m_elapsed_time >= other.m_elapsed_time;
299 }
300
305 constexpr bool operator<(const TimeSpan& other) const
306 {
307 return m_elapsed_time < other.m_elapsed_time;
308 }
309
314 constexpr bool operator<=(const TimeSpan& other) const
315 {
316 return m_elapsed_time <= other.m_elapsed_time;
317 }
318
324 constexpr fltp08 operator/(const TimeSpan& time_2) const
325 {
327 }
328
333 constexpr TimeSpan operator/(const sint08& den) const
334 {
335 return TimeSpan(m_start_time, m_elapsed_time / den);
336 }
337
342 constexpr TimeSpan operator/(const fltp08& den) const
343 {
345 }
346
352 constexpr TimeSpan operator*(const sint08& mult) const
353 {
354 return TimeSpan(m_start_time, m_elapsed_time * mult);
355 }
356
361 constexpr TimeSpan operator*(const fltp08& mult) const
362 {
364 }
365
370 constexpr TimeSpan& operator*=(const sint08& mult)
371 {
373 return *this;
374 }
375
380 constexpr TimeSpan& operator*=(const fltp08& mult)
381 {
383 return *this;
384 }
385
390 constexpr TimeSpan operator+(const TimeSpan& other) const
391 {
392 return TimeSpan(m_elapsed_time + other.m_elapsed_time);
393 }
394
399 constexpr TimeSpan& operator+=(const TimeSpan& other)
400 {
402 return *this;
403 }
404
409 constexpr TimeSpan operator-(const TimeSpan& other) const
410 {
411 return TimeSpan(m_elapsed_time - other.m_elapsed_time);
412 }
413
417 constexpr TimeSpan operator-() const
418 {
419 return TimeSpan(-m_elapsed_time);
420 }
421
426 constexpr TimeSpan& operator-=(const TimeSpan& other)
427 {
429 return *this;
430 }
431 protected:
434 };
435
445 template<>
446 struct ObjectInfo<TimeSpan, false, false>
447 {
448 static const uint01 Dimensions = 0;
449 static const bool Vector = false;
450 static const bool Primitive = true;
451 static const bool Pointer = false;
452 static const bool Unsigned = false;
453 static const bool Float = true;
454 static const bool Integer = false;
455 static const bool Number = true;
456 static const bool String = false;
457 static const bool Color = false;
458 static const bool Buffer = false;
459 static const bool Boolean = false;
465 };
466
472 NDEVR_BASE_API TimeSpan operator-(const Time& time, const Time& value);
473
480 NDEVR_BASE_API Time operator+(const Time& time, const TimeSpan& value);
487 NDEVR_BASE_API Time operator-(const Time& time, const TimeSpan& value);
494 NDEVR_BASE_API Time& operator+=(Time& time, const TimeSpan& value);
501 NDEVR_BASE_API Time& operator-=(Time& time, const TimeSpan& value);
506 constexpr TimeSpan abs(const TimeSpan& value)
507 {
508 return value >= TimeSpan(0) ? value : -value;
509 }
510
517 constexpr TimeSpan operator%(const Time& time, const TimeSpan& duration)
518 {
519 lib_assert(duration.elapsedNanoseconds() > 0, "Bad duration");
521 return TimeSpan(Time(time.getNanoSeconds() - nano), nano);
522 }
523
524 template<> constexpr TimeSpan Constant<TimeSpan>::Invalid = TimeSpan(Constant<sint08>::Invalid);
525 template<> constexpr TimeSpan Constant<TimeSpan>::Max = TimeSpan(Constant<sint08>::Max);
526 template<> constexpr TimeSpan Constant<TimeSpan>::Min = TimeSpan(Constant<sint08>::Min);
527}
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
Stores a time span, or difference between two times, with an optional start time.
Definition TimeSpan.h:46
constexpr TimeSpan(const uint08 elapsed_nano_seconds)
Constructs a TimeSpan from an unsigned 64-bit nanosecond duration with no start time.
Definition TimeSpan.h:83
constexpr TimeSpan(const Time &start, const sint08 &elapsed_nano_seconds)
Constructs a TimeSpan from a start time and an elapsed nanosecond duration.
Definition TimeSpan.h:110
constexpr TimeSpan operator*(const sint08 &mult) const
Multiplies the elapsed duration by an integer factor.
Definition TimeSpan.h:352
constexpr TimeSpan operator/(const fltp08 &den) const
Divides the elapsed duration by a floating-point denominator.
Definition TimeSpan.h:342
constexpr bool operator==(const TimeSpan &time) const
Checks equality of both start time and elapsed duration.
Definition TimeSpan.h:267
Time m_start_time
The optional start time of this span. Invalid if not set.
Definition TimeSpan.h:432
constexpr sint08 elapsedNanoseconds() const
Returns the elapsed duration in nanoseconds.
Definition TimeSpan.h:204
constexpr TimeSpan & operator*=(const sint08 &mult)
Multiplies the elapsed duration in place by an integer factor.
Definition TimeSpan.h:370
constexpr void setStartTime(Time time)
Sets the start time of this span without modifying the elapsed duration.
Definition TimeSpan.h:151
constexpr bool operator<=(const TimeSpan &other) const
Compares elapsed durations, ignoring start time.
Definition TimeSpan.h:314
constexpr void setEndTime(Time time)
Sets the end time by recomputing the elapsed duration from the current start time.
Definition TimeSpan.h:159
constexpr bool operator>(const TimeSpan &other) const
Compares elapsed durations, ignoring start time.
Definition TimeSpan.h:287
constexpr fltp08 elapsedHours() const
Returns the elapsed duration in hours.
Definition TimeSpan.h:246
constexpr TimeSpan & operator+=(const TimeSpan &other)
Adds another span's elapsed duration to this one in place.
Definition TimeSpan.h:399
sint08 m_elapsed_time
The elapsed duration in nanoseconds. Can be negative for past spans.
Definition TimeSpan.h:433
constexpr fltp08 elapsedSeconds() const
Returns the elapsed duration in seconds.
Definition TimeSpan.h:213
Time endTime() const
Computes the end time by adding the elapsed duration to the start time.
constexpr t_type elapsedMilliseconds() const
Returns the elapsed duration in milliseconds.
Definition TimeSpan.h:225
constexpr TimeSpan & operator*=(const fltp08 &mult)
Multiplies the elapsed duration in place by a floating-point factor.
Definition TimeSpan.h:380
constexpr TimeSpan(sint04 elapsed_nano_seconds)
Constructs a TimeSpan from a signed 32-bit nanosecond duration with no start time.
Definition TimeSpan.h:59
constexpr TimeSpan(const sint08 elapsed_nano_seconds)
Constructs a TimeSpan from a signed 64-bit nanosecond duration with no start time.
Definition TimeSpan.h:67
constexpr TimeSpan operator-(const TimeSpan &other) const
Subtracts another span's elapsed duration from this one.
Definition TimeSpan.h:409
constexpr bool operator!=(const TimeSpan &time) const
Checks inequality of start time or elapsed duration.
Definition TimeSpan.h:277
constexpr void setElapsedHours(fltp08 elapsed_minutes)
Sets the elapsed duration in hours.
Definition TimeSpan.h:183
constexpr fltp08 elapsedMinutes() const
Returns the elapsed duration in minutes.
Definition TimeSpan.h:236
constexpr bool operator>=(const TimeSpan &other) const
Compares elapsed durations, ignoring start time.
Definition TimeSpan.h:296
constexpr TimeSpan(const Time &start, const fltp08 &elapsed_seconds)
Constructs a TimeSpan from a start time and an elapsed duration in seconds.
Definition TimeSpan.h:120
String getTimeString(const StringView &format_string) const
Formats the time span as a human-readable string using the given format.
constexpr TimeSpan(const fltp08 elapsed_seconds)
Constructs a TimeSpan from a duration in seconds with no start time.
Definition TimeSpan.h:91
constexpr Time startTime() const
Returns the start time of this span.
Definition TimeSpan.h:143
fltp08 elapsedPercent(Time time, bool clip_time) const
Computes the percentage of elapsed time at a given point within this span.
sint08 elapsedMonths() const
Returns the elapsed duration in whole months, accounting for calendar boundaries.
constexpr TimeSpan(uint04 elapsed_nano_seconds)
Constructs a TimeSpan from an unsigned 32-bit nanosecond duration with no start time.
Definition TimeSpan.h:75
constexpr bool operator<(const TimeSpan &other) const
Compares elapsed durations, ignoring start time.
Definition TimeSpan.h:305
constexpr TimeSpan & operator-=(const TimeSpan &other)
Subtracts another span's elapsed duration from this one in place.
Definition TimeSpan.h:426
sint08 elapsedDays() const
Returns the elapsed duration in whole days, accounting for calendar boundaries.
constexpr TimeSpan operator*(const fltp08 &mult) const
Multiplies the elapsed duration by a floating-point factor.
Definition TimeSpan.h:361
constexpr TimeSpan operator-() const
Negates the elapsed duration of this time span.
Definition TimeSpan.h:417
constexpr fltp08 operator/(const TimeSpan &time_2) const
Divides this span by another, returning the ratio of elapsed durations.
Definition TimeSpan.h:324
constexpr TimeSpan operator+(const TimeSpan &other) const
Adds two time spans, combining their elapsed durations.
Definition TimeSpan.h:390
bool contains(const Time &time) const
Checks whether the given time falls within this time span.
constexpr void setElapsedMinutes(fltp08 elapsed_minutes)
Sets the elapsed duration in minutes.
Definition TimeSpan.h:175
constexpr TimeSpan()
Constructs an invalid TimeSpan with no start time and an invalid elapsed duration.
Definition TimeSpan.h:51
constexpr void setElapsedSeconds(fltp08 elapsed_seconds)
Sets the elapsed duration in seconds.
Definition TimeSpan.h:167
TimeSpan(const Time &start, const Time &end)
Constructs a TimeSpan from a start and end time, computing the elapsed duration.
Definition TimeSpan.h:100
constexpr TimeSpan operator/(const sint08 &den) const
Divides the elapsed duration by an integer denominator.
Definition TimeSpan.h:333
Represents a timestamp with utilities for manipulation and conversion.
Definition Time.h:62
constexpr uint08 getNanoSeconds() const
Gets the time in nanoseconds since 1970.
Definition Time.h:267
The primary namespace for the NDEVR SDK.
static constexpr Angle< t_angle_type > & operator-=(Angle< t_angle_type > &angle, const Angle< t_angle_type > &sub)
Subtraction assignment operator for Angles.
constexpr Angle< t_angle_type > abs(const Angle< t_angle_type > &value)
Changes an input with a negative sign, to a positive sign.
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
double fltp08
Defines an alias representing an 8 byte floating-point number.
int32_t sint04
-Defines an alias representing a 4 byte, signed integer.
static constexpr Angle< t_type > operator-(const Angle< t_type > &angle_a, const Angle< t_type > &angle_b)
Subtraction operator.
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
static constexpr Angle< t_angle_type > & operator+=(Angle< t_angle_type > &angle, const Angle< t_angle_type > &add)
Addition assignment operator for Angles.
static constexpr bool IsInvalid(const Angle< t_type > &value)
Checks whether the given Angle holds an invalid value.
Definition Angle.h:388
static constexpr Angle< t_type > operator+(const Angle< t_type > &angle_a, const Angle< t_type > &angle_b)
Addition operator.
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)
Element-wise modulo operator for two Vectors of Angles.
int64_t sint08
-Defines an alias representing an 8 byte, signed integer -Can represent exact integer values -9223372...
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
Definition Angle.h:408
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
static const bool Float
Whether this type behaves as a floating-point value.
Definition TimeSpan.h:453
static const bool Buffer
Whether this type is a buffer.
Definition TimeSpan.h:458
static const bool Unsigned
Whether this type is unsigned.
Definition TimeSpan.h:452
static const uint01 Dimensions
The number of dimensions (0 for scalar types).
Definition TimeSpan.h:448
static constexpr ObjectInfo< TimeSpan, false, false > VectorSub()
Returns the ObjectInfo for the vector sub-element type (identity for scalars).
Definition TimeSpan.h:464
static const bool Primitive
Whether this type is a primitive.
Definition TimeSpan.h:450
static const bool String
Whether this type is a string.
Definition TimeSpan.h:456
static const bool Vector
Whether this type is a vector.
Definition TimeSpan.h:449
static const bool Color
Whether this type is a color.
Definition TimeSpan.h:457
static const bool Boolean
Whether this type is a boolean.
Definition TimeSpan.h:459
static const bool Number
Whether this type is numeric.
Definition TimeSpan.h:455
static const bool Pointer
Whether this type is a pointer.
Definition TimeSpan.h:451
static const bool Integer
Whether this type is an integer.
Definition TimeSpan.h:454
Information about the object.
Definition ObjectInfo.h:55