ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
timepointbase.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of the \aliblong. It does not belong to an \alibmod and is
4/// included in any \alibbuild.
5///
6/// Copyright 2013-2026 A-Worx GmbH, Germany.
7/// Published under #"mainpage_license".
8//==================================================================================================
9ALIB_EXPORT namespace alib::time {
10
11#if !DOXYGEN
12# define sc std::chrono::
13#endif
14
15//==================================================================================================
16/// As explained in detail in the documentation of module \alib_time, a steady time model is
17/// supported with class #"Ticks" and a non-steady one representing the system clock with
18/// class #"time::DateTime".
19/// Both types share this template class as their generic base.
20///
21/// The common features that this class provides to its descendants are:
22/// - Type definition #"TTimePoint" used to store a point in time.
23/// - Method #"Export", which returns the internal value of type #"TTimePoint" which is of C++
24/// standard type \c std::chrono::time_point.
25/// - Inner class #"TimePointBase::Duration" that represents the difference type of this
26/// class.
27/// - Static method #"Now" which creates an instance representing the current point in time.
28/// - Methods #"Age" and #"Since".
29/// - Various overloaded arithmetic and comparison operators.
30///
31/// \note
32/// The resolution and accuracy of the values is platform-dependent. Especially nanoseconds are
33/// deemed to be inaccurate if below several hundreds (this was written and fact in 2013,
34/// reviewed 2019).
35///
36/// \attention
37/// The dates storable in objects of this class are limited to a certain time range. In the
38/// current GNU/Linux and Windows implementations the range is roughly <c>+-292.27</c> years
39/// before and after the point in time that the system that the software is running on was
40/// initialized (bootstrapped). This value results from the following facts for these
41/// implementations:
42/// - the storage resolution is one nanosecond.
43/// - the storage size is 64 bits (63 plus the sign bit).
44/// - system-dependent ticks counters are reset to \c 0 with the boot of a system.
45///
46/// Now, dividing <c>2^63</c> by the number of nanoseconds of one year which
47/// consists of roughly 365.25 days, this results in 292.27 years.
48///
49/// @tparam TClock The type of clock to use. This will be
50/// - \c std::chrono::system_clock with descendant class #"time::DateTime" and
51/// - \c std::chrono::steady_clock with descendant class #"Ticks".
52/// @tparam TDerived The derived type itself, hence either #"time::DateTime" or
53/// #"Ticks". This template parameter is needed to define the result
54/// type of various methods and operators.
55//==================================================================================================
56template<typename TClock, typename TDerived>
58 public:
59 /// The internal c++ type for time points.
60 using TTimePoint= typename TClock::time_point;
61
62 /// Integral type used for exporting and importing values in raw units.
63 using TRaw= typename TTimePoint::rep;
64
65
66 /// This inner class of #"%TimePointBase" represents durations, hence difference values of two
67 /// values of the parent class.
68 ///
69 /// Often, objects of this class are generated by the subtraction of #"%TimePointBase" values or
70 /// by using methods #"TimePointBase::Age;*" and #"TimePointBase::Since;*".
71 /// Furthermore, the class #"util::CalendarDuration" (found in the module
72 /// \alib_strings) can be used to convert durations to and from human-readable units (days,
73 /// hours, minutes, etc.).
74 ///
75 /// ## Friends ##
76 /// class #"TimePointBase"
77 class Duration {
78 #if !DOXYGEN
79 // In C++ inner classes need to be friend to access protected elements of the outer class.
80 friend class TimePointBase;
81 #endif
82
83
84 public:
85 /// The value type for time spans.
86 using TDuration = sc steady_clock::duration;
87
88
89 //############################################################################################
90 // protected fields
91 //############################################################################################
92 protected:
93 /// The internal time value.
95
96 //############################################################################################
97 // Constructors
98 //############################################################################################
99 public:
100
101 /// Creates a zero-length time span.
102 constexpr
104 : span(0) {}
105
106 /// Constructs an instance from C++ library values.
107 /// @see Methods #".Import" and #".Export".
108 /// @param stdLibValue The value to copy into this.
109 Duration( const TDuration& stdLibValue )
110 : span( stdLibValue ) {}
111
112 /// Constructs an instance from C++ library values.
113 /// @see Methods #".Import" and "#".Export"".
114 /// @param stdLibValue The value to copy into this.
115 /// @return A reference to this object.
116 Duration& operator=( const TDuration& stdLibValue ) { span= stdLibValue; return *this; }
117
118 //############################################################################################
119 // Interface
120 //############################################################################################
121
122 /// Returns the internal time span value using the C++ standard library format.
123 /// @return The internal value.
124 TDuration Export() const { return span; }
125
126 /// Creates an instance representing the time span given in C++ standard library format.
127 /// @param timeSpan The C++ time point value.
128 /// @return A time span value representing the given externalized \p{timeSpan}.
129 static
130 Duration Import( const TDuration& timeSpan ) { return Duration ( timeSpan ); }
131
132
133 /// Sets this object's value to the given duration, in the case the given is shorter.
134 /// @param other The time stamp to compare.
135 /// @return A reference to this object.
136 Duration& SetMinimum( const Duration& other )
137 {
138 span= (std::min)(span, other.span);
139 return *this;
140 }
141
142 /// Sets this object's value to the given duration, in the case the given is longer.
143 /// @param other The time stamp to compare.
144 /// @return A reference to this object.
145 Duration& SetMaximum( const Duration& other )
146 {
147 span= (std::max)(span, other.span);
148 return *this;
149 }
150
151 /// @return \c true if this is a positive duration, \c false otherwise.
152 bool IsPositive() const { return span.count() > 0; }
153
154 /// @return \c true if this is a zero-length time-span, \c false otherwise.
155 bool IsZero() const { return span.count() == 0; }
156
157 /// Equal to operator.
158 /// @param other The duration to compare.
159 /// @return The result of the comparison.
160 bool operator==( const Duration& other ) const { return span == other.span; }
161
162
163 /// Equal to operator.
164 /// @param other The duration to compare.
165 /// @return The result of the comparison.
166 bool operator==( const TDuration& other ) const { return span == other; }
167
168 /// Not equal to operator.
169 /// @param other The duration to compare.
170 /// @return The result of the comparison.
171 bool operator!=( const Duration& other ) const { return span != other.span; }
172
173 /// Not equal to operator.
174 /// @param other The duration to compare.
175 /// @return The result of the comparison.
176 bool operator!=( const TDuration& other ) const { return span != other; }
177
178 /// Less than operator.
179 /// @param other The duration to compare.
180 /// @return A reference to this object.
181 bool operator<( const Duration& other ) const { return span < other.span; }
182
183 /// Less than operator.
184 /// @param other The duration to compare.
185 /// @return A reference to this object.
186 bool operator<( const TDuration& other ) const { return span < other; }
187
188 /// Less than or equal to operator.
189 /// @param other The duration to compare.
190 /// @return The result of the comparison.
191 bool operator<=( const Duration& other ) const { return span <= other.span; }
192
193 /// Less than or equal to operator.
194 /// @param other The duration to compare.
195 /// @return The result of the comparison.
196 bool operator<=( const TDuration& other ) const { return span <= other; }
197
198 /// Greater than operator.
199 /// @param other The duration to compare.
200 /// @return The result of the comparison.
201 bool operator>( const Duration& other ) const { return span > other.span; }
202
203 /// Greater than operator.
204 /// @param other The duration to compare.
205 /// @return The result of the comparison.
206 bool operator>( const TDuration& other ) const { return span > other; }
207
208 /// Greater than or equal to operator.
209 /// @param other The duration to compare.
210 /// @return The result of the comparison.
211 bool operator>=( const Duration& other ) const { return span >= other.span; }
212
213 /// Greater than or equal to operator.
214 /// @param other The duration to compare.
215 /// @return The result of the comparison.
216 bool operator>=( const TDuration& other ) const { return span >= other; }
217
218 /// Addition operator.
219 /// @param rhs The right-hand side time span to add.
220 /// @return A time span containing the sum.
221 Duration operator+( const Duration& rhs ) const { return Duration(span + rhs.span); }
222
223 /// Addition operator.
224 /// @param rhs The right-hand side time span to add.
225 /// @return A time span containing the sum.
226 Duration operator+( const TDuration& rhs ) const { return Duration(span + rhs); }
227
228 /// Assignment by sum operator.
229 /// @param other The time span to subtract.
230 /// @return A reference to this object.
231 Duration& operator+=( const Duration& other ) { span+= other.span; return *this; }
232
233 /// Assignment by sum operator.
234 /// @param other The time span to subtract.
235 /// @return A reference to this object.
236 Duration& operator+=( const TDuration& other ) { span+= other; return *this; }
237
238 /// Subtraction operator.
239 /// @param rhs The right-hand side time span to subtract.
240 /// @return A time span containing the sum.
241 Duration operator-( const Duration& rhs ) const { return Duration(span - rhs.span); }
242
243 /// Subtraction operator.
244 /// @param rhs The right-hand side time span to subtract.
245 /// @return A time span containing the sum.
246 Duration operator-( const TDuration& rhs ) const { return Duration(span - rhs); }
247
248 /// Assignment by difference operator.
249 /// @param other The time span subtract.
250 /// @return A reference to this object.
251 Duration& operator-=( const Duration& other ) { span-= other.span; return *this; }
252
253 /// Assignment by difference operator.
254 /// @param other The time span subtract.
255 /// @return A reference to this object.
256 Duration& operator-=( const TDuration& other ) { span-= other; return *this; }
257
258 /// Multiply operator.
259 /// @param multiplier The multiplier.
260 /// @return A time span containing the sum.
261 Duration operator*( double multiplier ) const
262 { return sc duration_cast<TDuration>( (span * multiplier) ); }
263
264 /// Multiply operator.
265 /// @param multiplier The multiplier.
266 /// @return A time span containing the sum.
267 Duration operator*( int64_t multiplier ) const
268 { return sc duration_cast<TDuration>( (span * multiplier) ); }
269
270 /// Assignment by product operator.
271 /// @param multiplier The multiplier.
272 /// @return A reference to this object.
273 Duration& operator*=( double multiplier )
274 { span= sc duration_cast<TDuration>( (span * multiplier) ); return *this; }
275
276 /// Assignment by product operator.
277 /// @param multiplier The multiplier.
278 /// @return A reference to this object.
279 Duration& operator*=( int64_t multiplier )
280 { span= sc duration_cast<TDuration>( (span * multiplier) ); return *this; }
281
282 /// Divide operator.
283 /// @param divisor The divisor.
284 /// @return A time span containing the sum.
285 Duration operator/( double divisor ) const
286 { return sc duration_cast<TDuration>( (span / divisor) ); }
287
288 /// Divide operator.
289 /// @param divisor The divisor.
290 /// @return A time span object containing the sum.
291 Duration operator/( int64_t divisor ) const
292 { return sc duration_cast<TDuration>( (span / divisor) ); }
293
294 /// Assignment by quotient operator.
295 /// @param divisor The divisor.
296 /// @return A reference to this object.
297 Duration& operator/=( double divisor )
298 { span= sc duration_cast<TDuration>( (span / divisor) ); return *this; }
299
300
301 /// Assignment by quotient operator.
302 /// @param divisor The divisor.
303 /// @return A reference to this object.
304 Duration& operator/=( int64_t divisor )
305 { span= sc duration_cast<TDuration>( (span / divisor) ); return *this; }
306
307
308 //############################################################################################
309 // Conversion to/from time values (nanoseconds, milliseconds, microseconds, seconds, ...)
310 //############################################################################################
311 public:
312
313 /// Converts the internal value to days.
314 /// @return The internal value converted to days.
315 double InDays() const {
316 return double( sc duration_cast<sc microseconds>( span ).count() )
317 / (1000000. * 3600. * 24. );
318 }
319
320 /// Converts the internal value to absolute days.
321 /// @return The internal value converted to days.
323 return integer( sc duration_cast<sc hours>( span ).count()
324 / 24 );
325 }
326
327 /// Converts the internal value to hours.
328 /// @return The internal value converted to hours.
329 double InHours() const {
330 return double( sc duration_cast<sc microseconds>( span ).count() )
331 / (1000000. * 3600. );
332 }
333
334 /// Converts the internal value to absolute hours.
335 /// @return The internal value converted to hours.
337 { return integer( sc duration_cast<sc hours>( span ).count() ); }
338
339 /// Converts the internal value to minutes.
340 /// @return The internal value converted to minutes.
341 double InMinutes() const {
342 return double( sc duration_cast<sc microseconds>( span ).count() )
343 / (1000000. * 60. );
344 }
345
346 /// Converts the internal value to absolute minutes.
347 /// @return The internal value converted to minutes.
348 int64_t InAbsoluteMinutes() const
349 { return sc duration_cast<sc minutes>( span ).count(); }
350
351 /// Converts the internal value to seconds.
352 /// @return The internal value converted to seconds.
353 double InSeconds() const {
354 return double( sc duration_cast<sc nanoseconds>( span ).count() )
355 / (1000000000. );
356 }
357
358 /// Converts the internal value to absolute seconds.
359 /// @return The internal value converted to seconds.
360 int64_t InAbsoluteSeconds() const
361 { return sc duration_cast<sc seconds>( span ).count(); }
362
363 /// Converts the internal value to milliseconds.
364 /// @return The internal value converted to milliseconds.
365 double InMilliseconds() const {
366 return double( sc duration_cast<sc nanoseconds>( span ).count() )
367 / (1000000. );
368 }
369
370 /// Converts the internal value to absolute milliseconds.
371 /// @return The internal value converted to milliseconds.
373 { return sc duration_cast<sc milliseconds>( span ).count(); }
374
375 /// Converts the internal value to microseconds.
376 /// @return The internal value converted to microseconds.
377 double InMicroseconds() const {
378 return double(sc duration_cast<sc nanoseconds>( span ).count())
379 / (1000. );
380 }
381
382 /// Converts the internal value to absolute microseconds.
383 /// @return The internal value converted to microseconds.
385 { return sc duration_cast<sc microseconds>( span ).count(); }
386
387 /// Converts the internal value to nanoseconds.
388 /// @return The internal value converted to nanoseconds.
389 int64_t InNanoseconds() const
390 { return sc duration_cast<sc nanoseconds>( span ).count(); }
391
392 /// Returns 1 divided by internal value in seconds, hence the number of Hertz that this
393 /// object represents when interpreted as a time span.
394 ///
395 /// @param qtyFractionalDigits Number of digits that the return value will be rounded to.
396 /// Defaults to -1 which means no rounding.
397 /// @return double value representing the frequency in hertz.
398 double InHertz( int qtyFractionalDigits= -1 ) const {
399 // check
400 if ( span.count() == 0)
401 return 0.0;
402
403 // calc hertz
404 double hz= 1000000000.0 / double(InNanoseconds());
405
406 // no rounding? that's it
407 if ( qtyFractionalDigits < 0 )
408 return hz;
409
410 // round
411 double mag= pow( 10, qtyFractionalDigits );
412 return int( hz * mag + ( hz < 0 ? -0.5 : 0.5 ) ) / mag;
413 }
414
415
416 /// Sets the internal value to a time span provided in days.
417 /// @param days The time span to set in days.
418 /// @return \c *this to allow concatenated calls.
419 static
420 Duration FromDays( double days )
421 { return sc duration_cast<TDuration>(sc hours(24) * days); }
422
423 /// Sets the internal value to a time span provided in days.
424 /// @param days The time span to set in days.
425 /// @return \c *this to allow concatenated calls.
426 static
428 { return sc duration_cast<TDuration>(sc hours(24) * days); }
429
430 /// Sets the internal value to a time span provided in hours.
431 /// @param hours The time span to set in hours.
432 /// @return \c *this to allow concatenated calls.
433 static
434 Duration FromHours( double hours )
435 { return sc duration_cast<TDuration>(sc hours(1) * hours); }
436
437 /// Sets the internal value to a time span provided in hours.
438 /// @param hours The time span to set in hours.
439 /// @return \c *this to allow concatenated calls.
440 static
442 { return sc duration_cast<TDuration>( sc hours(1) * hours ); }
443
444 /// Sets the internal value to a time span provided in hours.
445 /// @param minutes The time span to set in minutes.
446 /// @return \c *this to allow concatenated calls.
447 static
448 Duration FromMinutes(double minutes )
449 { return sc duration_cast<TDuration>( sc minutes(1) * minutes ); }
450
451 /// Sets the internal value to a time span provided in hours.
452 /// @param minutes The time span to set in minutes.
453 /// @return \c *this to allow concatenated calls.
454 static
456 { return sc duration_cast<TDuration>( sc minutes(1) * minutes ); }
457
458 /// Sets the internal value to a time span provided in seconds.
459 /// @param seconds The time span to set in seconds.
460 /// @return \c *this to allow concatenated calls.
461 static
462 Duration FromSeconds( double seconds )
463 { return sc duration_cast<TDuration>(sc seconds(1) * seconds ); }
464
465 /// Sets the internal value to a time span provided in seconds.
466 /// @param seconds The time span to set in seconds.
467 /// @return \c *this to allow concatenated calls.
468 static
469 Duration FromAbsoluteSeconds( int64_t seconds )
470 { return sc duration_cast<TDuration>(sc seconds(1) * seconds); }
471
472 /// Sets the internal value to a time span provided in milliseconds.
473 /// @param milliseconds The time span to set in milliseconds.
474 /// @return \c *this to allow concatenated calls.
475 static
476 Duration FromMilliseconds( double milliseconds )
477 { return sc duration_cast<TDuration>(sc milliseconds(1) * milliseconds); }
478
479 /// Sets the internal value to a time span provided in milliseconds.
480 /// @param milliseconds The time span to set in milliseconds.
481 /// @return \c *this to allow concatenated calls.
482 static
483 Duration FromAbsoluteMilliseconds( int64_t milliseconds )
484 { return sc duration_cast<TDuration>( sc milliseconds(1) * milliseconds ); }
485
486 /// Sets the internal value to a time span provided in microseconds.
487 /// @param microseconds The time span to set in microseconds.
488 /// @return \c *this to allow concatenated calls.
489 static
490 Duration FromMicroseconds( double microseconds )
491 { return sc duration_cast<TDuration>(sc microseconds(1) * microseconds ); }
492
493 /// Sets the internal value to a time span provided in microseconds.
494 /// @param microseconds The time span to set in microseconds.
495 /// @return \c *this to allow concatenated calls.
496 static
497 Duration FromAbsoluteMicroseconds( int64_t microseconds )
498 { return sc duration_cast<TDuration>( sc microseconds(1) * microseconds ); }
499
500 /// Sets the internal value to a time span provided in nanoseconds.
501 /// @param nanoseconds The time span to set in nanoseconds.
502 /// @return \c *this to allow concatenated calls.
503 static
504 Duration FromNanoseconds( int64_t nanoseconds )
505 { return sc duration_cast<TDuration>(sc nanoseconds( nanoseconds )); }
506 }; // inner class Duration
507
508 //################################################################################################
509 // protected fields
510 //################################################################################################
511
512 protected:
513 /// The internal timer value. This value can be accessed using methods #".Export" and
514 /// #".Import".
516
517 //################################################################################################
518 // Constructors
519 //################################################################################################
520
521 public:
522 /// Constructor. With parameter \p{init} being defaulted, this constructor acts as
523 /// the default constructor, which creates an instance representing the point in time when
524 /// this constructor was invoked.
525 /// This measures the current point in time, which introduces some effort.
526 ///
527 /// To avoid this in situations where an instance is overwritten after construction
528 /// (i.e., before it is read), parameter \p{init} can be passed to either #"%Suppress"
529 /// or #"%Nulled". Both values set this instance to a \e nulled state, as complete
530 /// suppression is not available. Inn many situations the compiler will optimize out
531 /// a nulled construction.
532 ///
533 /// If not initialized, the method #".IsSet" will return \c false.
534 ///
535 /// @param init If \c Initialization::Default, the current time is measured and set.
536 /// Defaults to \c Initialization::Default.
537 constexpr
539 {
541 stamp= TClock::now();
542 }
543
544 /// Returns an instance representing the actual point in time.
545 /// @return The current point in time time.
546 static
547 TDerived Now() { return TDerived(TClock::now()); }
548
549 /// Returns an instance representing the beginning of the epoch.
550 /// No time point represented by this type can be further in the past.
551 /// @return The earliest point in time.
552 static
553 TDerived BeginningOfEpoch() { return TDerived((TClock::time_point::min)()); }
554
555 /// Returns an instance representing the end of the epoch.
556 /// No time point represented by this type can be further in the future.
557 /// @return A maximum point in time.
558 static
559 TDerived EndOfEpoch() { return TDerived((TClock::time_point::max)()); }
560
561 /// Resets this instance to the current point in time.
562 /// @return A reference to \c this, (cast to the derived type) to allow concatenated
563 /// operations.
564 TDerived& Reset() { stamp= TClock::now(); return *static_cast<TDerived*>(this); }
565
566 /// Constructor using native C++ library values.
567 /// \see Methods #".Import" and #".Export".
568 ///
569 /// @param internalValue The value to copy into this.
570 constexpr
571 TimePointBase( TTimePoint internalValue )
572 : stamp(internalValue) {}
573
574 //################################################################################################
575 // Interface
576 //################################################################################################
577 public:
578
579 /// Returns \c true if this object is not representing the start of the epoch.
580 /// An uninitialized object that returns \c false can be created with provision of
581 /// \c Initialization::Suppress on construction.
582 ///
583 /// @return \c true if this object is initialized, \c false otherwise.
584 bool IsSet() { return stamp != TTimePoint(); }
585
586 /// Unsets this object, hence makes this object representing the start of the epoch, as if
587 /// it was constructed with parameter value \c Initialization::Suppress.
588 void Unset() { stamp= TTimePoint(); }
589
590 /// Copies the value from the given object.
591 /// @param other The point in time to copy from.
592 void SetAs( const TDerived& other ) { stamp= other.stamp; }
593
594 /// Returns the internal time value in the C++ standard library type.
595 /// @return The internal value.
596 TTimePoint Export() const { return stamp; }
597
598 /// Sets the internal time value given by a value of C++ standard library type.
599 /// @param timePoint The value to set.
600 void Import(TTimePoint timePoint) { stamp= timePoint; }
601
602
603 /// Returns the internal time value in the C++ standard library's tick unit.
604 ///
605 /// @return The internal value.
606 TRaw ToRaw() const { return stamp.time_since_epoch().count(); }
607
608 /// Sets the value from a value of C++ standard library's tick unit type.
609 /// @param raw The time span to create in raw units.
610 void SetFromRaw( TRaw raw ) { stamp= TTimePoint( typename TClock::duration( raw ) ); }
611
612 /// Creates an instance from a value of C++ standard library's tick unit type.
613 ///
614 /// @param raw The time span to create in raw units.
615 /// @return The internal value.
616 static
617 TDerived FromRaw( TRaw raw )
618 { return TDerived( TTimePoint( typename TClock::duration( raw ) ) ); }
619
620 /// Addition operator.
621 /// @param timeSpan The time span to add.
622 /// @return A time stamp object containing the sum.
623 TDerived operator+( const Duration& timeSpan ) const
624 { return stamp + sc duration_cast<typename TClock::duration>( timeSpan.span ); }
625
626 /// Addition operator.
627 /// @param timeSpan The time span to add.
628 /// @return A time stamp object containing the sum.
629 TDerived operator+( const typename Duration::TDuration& timeSpan ) const
630 { return stamp + timeSpan; }
631
632 /// Assignment by sum operator.
633 /// @param timeSpan The time span to add.
634 /// @return A reference to this object.
635 TDerived operator+=( const Duration& timeSpan )
636 {
637 stamp+= sc duration_cast<typename TClock::duration>( timeSpan.span );
638 return stamp;
639 }
640
641 /// Assignment by sum operator.
642 /// @param timeSpan The time span to add.
643 /// @return A reference to this object.
644 TDerived operator+=( const typename Duration::TDuration& timeSpan )
645 { stamp+= timeSpan; return stamp; }
646
647 /// Subtraction operator.
648 /// @param timeSpan The time span to subtract.
649 /// @return A time stamp object containing the sum.
650 TDerived operator-( const Duration& timeSpan ) const
651 { return stamp - sc duration_cast<typename TClock::duration>( timeSpan.span ); }
652
653 /// Subtraction operator.
654 /// @param timeSpan The time span to subtract.
655 /// @return A time stamp object containing the sum.
656 TDerived operator-( const typename Duration::TDuration& timeSpan ) const
657 { return stamp - timeSpan; }
658
659 /// Assignment by difference operator.
660 /// @param timeSpan The time span to subtract.
661 /// @return A reference to this object.
662 TDerived operator-=( const Duration& timeSpan ) { stamp-= timeSpan.span; return stamp; }
663
664 /// Assignment by difference operator.
665 /// @param timeSpan The time span to subtract.
666 /// @return A reference to this object.
667 TDerived operator-=( const typename Duration::TDuration& timeSpan )
668 { stamp-= timeSpan; return stamp; }
669
670 /// Subtraction operator with other time span argument. If the given time stamp represents
671 /// a point in type earlier than the one this object represents, the result is positive.
672 /// @param other The time stamp to subtract.
673 /// @return A time span object containing the difference.
674 Duration operator-( const TDerived& other ) const { return Duration(stamp - other.stamp); }
675
676 //################################################################################################
677 // Interface Age, Since
678 //################################################################################################
679
680 /// Returns the time span between the value represented by this instance and the current
681 /// system time.
682 /// If the internal value represents a historic point in time, the result is positive.
683 ///
684 /// @return The age of this instance stored in a new Duration.
685 Duration Age() const { return Duration( TDerived().stamp - this->stamp ); }
686
687 /// Returns the time span between the value represented by this instance and the given
688 /// other time stamp. If the given time stamp represents an earlier point in time, the result
689 /// is positive.
690 ///
691 /// @param other The value to compare this instance with
692 ///
693 /// @return The age of this instance stored in the given or created object.
694 Duration Since( const TDerived& other ) const { return (*this) - other; }
695
696 /// Determines if this object's age is higher than a given time span.
697 ///
698 /// @param timeSpan A time span to compare.
699 /// @return \c true if the given time span is smaller equal than the age of this object,
700 /// hence to the time span passed since the point in time this object represents.
701 /// \c false otherwise.
702 bool IsOlderThan( const Duration& timeSpan ) const { return Age() > timeSpan; }
703
704}; // class TimePointBase
705
706} // namespace [alib::time]
707
708#undef sc
#define ALIB_EXPORT
double InHertz(int qtyFractionalDigits=-1) const
static Duration FromAbsoluteMicroseconds(int64_t microseconds)
bool operator!=(const TDuration &other) const
Duration & operator=(const TDuration &stdLibValue)
bool operator!=(const Duration &other) const
bool operator<(const TDuration &other) const
Duration & operator/=(int64_t divisor)
TDuration span
The internal time value.
Duration & operator/=(double divisor)
Duration operator/(double divisor) const
Duration & operator*=(double multiplier)
Duration operator+(const Duration &rhs) const
static Duration FromAbsoluteMinutes(int64_t minutes)
bool operator>(const TDuration &other) const
Duration & operator+=(const Duration &other)
static Duration FromMinutes(double minutes)
static Duration FromMicroseconds(double microseconds)
Duration & operator+=(const TDuration &other)
static Duration FromAbsoluteSeconds(int64_t seconds)
sc steady_clock::duration TDuration
The value type for time spans.
bool operator==(const TDuration &other) const
static Duration FromSeconds(double seconds)
Duration operator-(const TDuration &rhs) const
bool operator<(const Duration &other) const
Duration operator+(const TDuration &rhs) const
bool operator<=(const TDuration &other) const
Duration(const TDuration &stdLibValue)
Duration & SetMinimum(const Duration &other)
bool operator>=(const TDuration &other) const
static Duration FromAbsoluteHours(int64_t hours)
static Duration FromDays(double days)
bool operator>=(const Duration &other) const
constexpr Duration()
Creates a zero-length time span.
Duration operator/(int64_t divisor) const
bool operator<=(const Duration &other) const
Duration & SetMaximum(const Duration &other)
Duration operator*(int64_t multiplier) const
static Duration FromMilliseconds(double milliseconds)
Duration & operator-=(const Duration &other)
bool operator>(const Duration &other) const
static Duration Import(const TDuration &timeSpan)
static Duration FromAbsoluteDays(int64_t days)
bool operator==(const Duration &other) const
static Duration FromHours(double hours)
Duration operator-(const Duration &rhs) const
Duration operator*(double multiplier) const
Duration & operator*=(int64_t multiplier)
Duration & operator-=(const TDuration &other)
static Duration FromNanoseconds(int64_t nanoseconds)
static Duration FromAbsoluteMilliseconds(int64_t milliseconds)
Duration operator-(const TDerived &other) const
TTimePoint Export() const
TDerived operator-(const typename Duration::TDuration &timeSpan) const
constexpr TimePointBase(const lang::Initialization init=lang::Initialization::Default)
TDerived operator-=(const typename Duration::TDuration &timeSpan)
constexpr TimePointBase(TTimePoint internalValue)
void Import(TTimePoint timePoint)
static TDerived EndOfEpoch()
TDerived operator+=(const Duration &timeSpan)
static TDerived FromRaw(TRaw raw)
TDerived operator+(const typename Duration::TDuration &timeSpan) const
TDerived operator+(const Duration &timeSpan) const
TDerived operator-=(const Duration &timeSpan)
bool IsOlderThan(const Duration &timeSpan) const
TDerived operator+=(const typename Duration::TDuration &timeSpan)
typename TClock::time_point TTimePoint
The internal c++ type for time points.
Duration Since(const TDerived &other) const
TDerived operator-(const Duration &timeSpan) const
static TDerived BeginningOfEpoch()
void SetAs(const TDerived &other)
typename TTimePoint::rep TRaw
Integral type used for exporting and importing values in raw units.
Initialization
Used, for example, with constructors that allow to suppress initialization of members.
lang::integer integer
Type alias in namespace #"%alib".
Definition integers.hpp:149