ALib C++ Library
Library Version: 2402 R1
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 module \alib_time of the \aliblong.
4 *
5 * \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6 * Published under \ref mainpage_license "Boost Software License".
7 **************************************************************************************************/
8#ifndef HPP_ALIB_TIME_TIMEPOINTBASE
9#define HPP_ALIB_TIME_TIMEPOINTBASE 1
10
11#if !defined(HPP_ALIB) && !defined(ALIB_DOX)
12# include "alib/alib.hpp"
13#endif
14
16
17#if ALIB_STRINGS && ALIB_ENUMS
18# if !defined(HPP_ALIB_LANG_COMMONENUMS)
20# endif
21#else
22# if !defined(HPP_ALIB_LANG_COMMONENUMS_DEFS)
24# endif
25#endif
26
27#if !defined(HPP_ALIB_LANG_INTEGERS)
28# include "alib/lang/integers.hpp"
29#endif
30
31
32#if !defined (_GLIBCXX_CHRONO) && !defined (_CHRONO_)
33 #include <chrono>
34#endif
35
36#if !defined (_GLIBCXX_CMATH) && !defined (_CMATH_)
37 #include <cmath>
38#endif
39
40#if !defined (_GLIBCXX_ALGORITHM) && !defined(_ALGORITHM_)
41# include <algorithm>
42#endif
43
44namespace alib { namespace time {
45
46/** ************************************************************************************************
47 * As explained in detail in the documentation of module \alib_time, a steady time model is
48 * supported with class \alib{time;Ticks} and a non-steady one representing the system clock with
49 * class \alib{time;DateTime}.
50 * Both types share this template class as their generic base.
51 *
52 * The common features that this class provides to its descendants are:
53 * - Type definition #TTimePoint used to store a point in time.
54 * - Method #Export, which returns the internal value of type #TTimePoint which is of C++
55 * standard type \c std::chrono::time_point.
56 * - Inner class \alib{time::TimePointBase;Duration} that represents the difference type of this
57 * class.
58 * - Static method #Now which creates an instance representing the current point in time.
59 * - Methods #Age and #Since.
60 * - Various overloaded arithmetic and comparison operators.
61 *
62 * \note
63 * The resolution and accuracy of the values is platform dependent. Especially nanoseconds are
64 * deemed to be inaccurate if below several hundreds (this was written and fact in 2013,
65 * reviewed 2019).
66 *
67 * \attention
68 * The dates storable in objects of this class are limited to a certain time range. In the
69 * current GNU/Linux and Windows implementations the range is roughly <c>+-292.27</c> years
70 * before and after the point in time that the system that the software is running on was
71 * initialized (bootstrapped). This value results from the following facts for these
72 * implementations:
73 * - the storage resolution is one nanosecond.
74 * - the storage size is 64 bits (63 plus the sign bit).
75 * - system dependent ticks counters are reset to \c 0 with the boot of a system.
76 *
77 * Now, dividing <c>2^63</c> by the number of nanoseconds of one year which
78 * consists of roughly 365.25 days, this results in 292.27 years.
79 *
80 * @tparam TClock The type of clock to use. This will be
81 * - \c std::chrono::system_clock with descendant class \alib{time;DateTime} and
82 * - \c std::chrono::steady_clock with descendant class \alib{time;Ticks}.
83 * @tparam TDerived The derived type itself, hence either \alib{time;DateTime} or
84 * \alib{time;Ticks}. This template parameter is needed to define the result
85 * type of various methods and operators.
86 **************************************************************************************************/
87template<typename TClock, typename TDerived>
89{
90 public:
91 /** The internal c++ type for time points. */
92 using TTimePoint= typename TClock::time_point;
93
94 /** Integral type used for exporting and importing values in raw units. */
95 using TRaw= typename TTimePoint::rep;
96
97
98 /** ********************************************************************************************
99 * This inner class of \b %TimePointBase represents durations, hence difference values of two
100 * values of the parent class.
101 *
102 * Often, objects of this class are generated by the subtraction of \b %TimePointBase values or
103 * by using methods \alib{time;TimePointBase::Age} and \alib{time;TimePointBase::Since}.
104 * Furthermore, class \alib{lang::system;CalendarDuration} (found in module \alib_basecamp)
105 * can be used to convert durations to and from human readable units (days, hours, minutes,
106 * etc.).
107 *
108 * ## Friends ##
109 * class \alib{time;TimePointBase}
110 **********************************************************************************************/
112 {
113 #if !defined(ALIB_DOX)
114 // In C++ inner classes need to be friend to access protected elements of the outer class.
115 friend class TimePointBase;
116 #endif
117
118
119 public:
120 /** The value type for time spans. */
121 using TDuration = typename std::chrono::steady_clock::duration;
122
123
124 // #########################################################################################
125 // protected fields
126 // #########################################################################################
127 protected:
128 /** The internal time value. */
130
131 /** ************************************************************************************
132 * Protected constructor.
133 *
134 * For construction with C++ library values, see method #Import.
135 * @param internalValue The value to copy into this.
136 **************************************************************************************/
137 Duration( TDuration internalValue )
138 : span( internalValue )
139 {}
140
141 // #########################################################################################
142 // Constructors
143 // #########################################################################################
144 public:
145
146 /** ************************************************************************************
147 * Creates a zero-length time span.
148 **************************************************************************************/
149 constexpr
151 : span(0)
152 {}
153
154 // #########################################################################################
155 // Interface
156 // #########################################################################################
157 public:
158 /** ************************************************************************************
159 * Returns the internal time span value using the C++ standard library format.
160 * @return The internal value.
161 **************************************************************************************/
162 TDuration Export() const { return span; }
163
164 /** ************************************************************************************
165 * Creates an instance representing the time span given in C++ standard library format.
166 * @param timeSpan The C++ stead clock time point value.
167 * @return A time span value representing the given externalized \p{timeSpan}.
168 **************************************************************************************/
169 static
170 Duration Import( TDuration timeSpan ) { return Duration ( timeSpan ); }
171
172
173 /** ************************************************************************************
174 * Sets this object's value to the given duration, in the case the given is shorter.
175 * @param other The time stamp to compare.
176 * @return A reference to this object.
177 **************************************************************************************/
178 Duration& SetMinimum( const Duration& other )
179 {
180 span= (std::min)(span, other.span);
181 return *this;
182 }
183
184 /** ************************************************************************************
185 * Sets this object's value to the given duration, in the case the given is longer.
186 * @param other The time stamp to compare.
187 * @return A reference to this object.
188 **************************************************************************************/
189 Duration& SetMaximum( const Duration& other )
190 {
191 span= (std::max)(span, other.span);
192 return *this;
193 }
194
195 /** ************************************************************************************
196 * Equal to operator.
197 * @param other The time stamp to compare.
198 * @return The result of the comparison.
199 **************************************************************************************/
200 bool operator==( const Duration& other )const { return span == other.span; }
201
202
203 /** ************************************************************************************
204 * Not equal to operator.
205 * @param other The time stamp to compare.
206 * @return The result of the comparison.
207 **************************************************************************************/
208 bool operator!=( const Duration& other )const { return span != other.span; }
209
210 /** ************************************************************************************
211 * Less than operator.
212 * @param other The time stamp to compare.
213 * @return A reference to this object.
214 **************************************************************************************/
215 bool operator<( const Duration& other ) const { return span < other.span; }
216
217 /** ************************************************************************************
218 * Less than or equal to operator.
219 * @param other The time stamp to compare.
220 * @return The result of the comparison.
221 **************************************************************************************/
222 bool operator<=( const Duration& other ) const { return span <= other.span; }
223
224 /** ************************************************************************************
225 * Greater than operator.
226 * @param other The time stamp to compare.
227 * @return The result of the comparison.
228 **************************************************************************************/
229 bool operator>( const Duration& other ) const { return span > other.span; }
230
231 /** ************************************************************************************
232 * Greater than or equal to operator.
233 * @param other The time stamp to compare.
234 * @return The result of the comparison.
235 **************************************************************************************/
236 bool operator>=( const Duration& other ) const { return span >= other.span; }
237
238 /** ************************************************************************************
239 * Addition operator.
240 * @param rhs The right-hand side time span to add.
241 * @return A time span object containing the sum.
242 **************************************************************************************/
243 Duration operator+( const Duration& rhs ) const { return Duration(span + rhs.span);}
244
245 /** ************************************************************************************
246 * Assignment by sum operator.
247 * @param other The time span object subtract.
248 * @return A reference to this object.
249 **************************************************************************************/
250 Duration& operator+=( const Duration& other ) { span+= other.span; return *this; }
251
252 /** ************************************************************************************
253 * Subtraction operator.
254 * @param rhs The right-hand side time span object to subtract.
255 * @return A time span object containing the sum.
256 **************************************************************************************/
257 Duration operator-( const Duration& rhs )const { return Duration(span - rhs.span);}
258
259 /** ************************************************************************************
260 * Assignment by difference operator.
261 * @param other The time span object subtract.
262 * @return A reference to this object.
263 **************************************************************************************/
264 Duration& operator-=( const Duration& other ) { span-= other.span; return *this; }
265
266 /** ************************************************************************************
267 * Multiply operator.
268 * @param multiplier The multiplier.
269 * @return A time span object containing the sum.
270 **************************************************************************************/
271 Duration operator*( double multiplier ) const
272 {
273 return std::chrono::duration_cast<TDuration>( (span * multiplier) );
274 }
275
276 /** ************************************************************************************
277 * Multiply operator.
278 * @param multiplier The multiplier.
279 * @return A time span object containing the sum.
280 **************************************************************************************/
281 Duration operator*( int64_t multiplier ) const
282 {
283 return std::chrono::duration_cast<TDuration>( (span * multiplier) );
284 }
285
286 /** ************************************************************************************
287 * Assignment by product operator.
288 * @param multiplier The multiplier.
289 * @return A reference to this object.
290 **************************************************************************************/
291 Duration& operator*=( double multiplier )
292 {
293 span= std::chrono::duration_cast<TDuration>( (span * multiplier) );
294 return *this;
295 }
296
297 /** ************************************************************************************
298 * Assignment by product operator.
299 * @param multiplier The multiplier.
300 * @return A reference to this object.
301 **************************************************************************************/
302 Duration& operator*=( int64_t multiplier )
303 {
304 span= std::chrono::duration_cast<TDuration>( (span * multiplier) );
305 return *this;
306 }
307
308 /** ************************************************************************************
309 * Divide operator.
310 * @param divisor The divisor.
311 * @return A time span object containing the sum.
312 **************************************************************************************/
313 Duration operator/( double divisor ) const
314 {
315 return std::chrono::duration_cast<TDuration>( (span / divisor) );
316 }
317
318 /** ************************************************************************************
319 * Divide operator.
320 * @param divisor The divisor.
321 * @return A time span object containing the sum.
322 **************************************************************************************/
323 Duration operator/( int64_t divisor ) const
324 {
325 return std::chrono::duration_cast<TDuration>( (span / divisor) );
326 }
327
328 /** ************************************************************************************
329 * Assignment by quotient operator.
330 * @param divisor The divisor.
331 * @return A reference to this object.
332 **************************************************************************************/
333 Duration& operator/=( double divisor )
334 {
335 span= std::chrono::duration_cast<TDuration>( (span / divisor) );
336 return *this;
337 }
338
339
340 /** ************************************************************************************
341 * Assignment by quotient operator.
342 * @param divisor The divisor.
343 * @return A reference to this object.
344 **************************************************************************************/
345 Duration& operator/=( int64_t divisor )
346 {
347 span= std::chrono::duration_cast<TDuration>( (span / divisor) );
348 return *this;
349 }
350
351
352 // #########################################################################################
353 // Conversion to/from time values (nanoseconds, milliseconds, microseconds, seconds, ...)
354 // #########################################################################################
355 public:
356
357 /** ************************************************************************************
358 * Converts the internal value to days.
359 * @return The internal value converted to days.
360 **************************************************************************************/
361 double InDays() const
362 {
363 return static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>( span ).count() )
364 / (1000000. * 3600. * 24. );
365 }
366
367 /** ************************************************************************************
368 * Converts the internal value to absolute days.
369 * @return The internal value converted to days.
370 **************************************************************************************/
372 {
373 return static_cast<integer>( std::chrono::duration_cast<std::chrono::hours>( span ).count()
374 / 24 );
375 }
376
377 /** ************************************************************************************
378 * Converts the internal value to hours.
379 * @return The internal value converted to hours.
380 **************************************************************************************/
381 double InHours() const
382 {
383 return static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>( span ).count() )
384 / (1000000. * 3600. );
385 }
386
387 /** ************************************************************************************
388 * Converts the internal value to absolute hours.
389 * @return The internal value converted to hours.
390 **************************************************************************************/
392 {
393 return static_cast<integer>( std::chrono::duration_cast<std::chrono::hours>( span ).count() );
394 }
395
396 /** ************************************************************************************
397 * Converts the internal value to minutes.
398 * @return The internal value converted to minutes.
399 **************************************************************************************/
400 double InMinutes() const
401 {
402 return static_cast<double>( std::chrono::duration_cast<std::chrono::microseconds>( span ).count() )
403 / (1000000. * 60. );
404 }
405
406 /** ************************************************************************************
407 * Converts the internal value to absolute minutes.
408 * @return The internal value converted to minutes.
409 **************************************************************************************/
410 int64_t InAbsoluteMinutes() const
411 {
412 return std::chrono::duration_cast<std::chrono::minutes>( span ).count();
413 }
414
415 /** ************************************************************************************
416 * Converts the internal value to seconds.
417 * @return The internal value converted to seconds.
418 **************************************************************************************/
419 double InSeconds() const
420 {
421 return static_cast<double>( std::chrono::duration_cast<std::chrono::nanoseconds>( span ).count() )
422 / (1000000000. );
423 }
424
425 /** ************************************************************************************
426 * Converts the internal value to absolute seconds.
427 * @return The internal value converted to seconds.
428 **************************************************************************************/
429 int64_t InAbsoluteSeconds() const
430 {
431 return std::chrono::duration_cast<std::chrono::seconds>( span ).count();
432 }
433
434 /** ************************************************************************************
435 * Converts the internal value to milliseconds.
436 * @return The internal value converted to milliseconds.
437 **************************************************************************************/
438 double InMilliseconds() const
439 {
440 return static_cast<double>( std::chrono::duration_cast<std::chrono::nanoseconds>( span ).count() )
441 / (1000000. );
442 }
443
444 /** ************************************************************************************
445 * Converts the internal value to absolute milliseconds.
446 * @return The internal value converted to milliseconds.
447 **************************************************************************************/
449 {
450 return std::chrono::duration_cast<std::chrono::milliseconds>( span ).count();
451 }
452
453 /** ************************************************************************************
454 * Converts the internal value to microseconds.
455 * @return The internal value converted to microseconds.
456 **************************************************************************************/
457 double InMicroseconds() const
458 {
459 return static_cast<double>(std::chrono::duration_cast<std::chrono::nanoseconds>( span ).count())
460 / (1000. );
461 }
462
463 /** ************************************************************************************
464 * Converts the internal value to absolute microseconds.
465 * @return The internal value converted to microseconds.
466 **************************************************************************************/
468 {
469 return std::chrono::duration_cast<std::chrono::microseconds>( span ).count();
470 }
471
472 /** ************************************************************************************
473 * Converts the internal value to nanoseconds.
474 * @return The internal value converted to nanoseconds.
475 **************************************************************************************/
476 int64_t InNanoseconds() const
477 {
478 return std::chrono::duration_cast<std::chrono::nanoseconds>( span ).count();
479 }
480
481 /** ************************************************************************************
482 * Returns 1 divided by internal value in seconds, hence the number of Hertz that this
483 * object represents when interpreted as a time span.
484 *
485 * @param qtyFractionalDigits Number of digits that the return value will be rounded to.
486 * Defaults to -1 which means no rounding.
487 * @return double value representing the frequency in hertz.
488 **************************************************************************************/
489 double InHertz( int qtyFractionalDigits= -1 ) const
490 {
491 // check
492 if ( span.count() == 0)
493 return 0.0;
494
495 // calc hertz
496 double hz= 1000000000.0 / static_cast<double>(InNanoseconds());
497
498 // no rounding? that's it
499 if ( qtyFractionalDigits < 0 )
500 return hz;
501
502 // round
503 double mag= pow( 10, qtyFractionalDigits );
504 return static_cast<int>( hz * mag + ( hz < 0 ? -0.5 : 0.5 ) )
505 / mag;
506 }
507
508
509 /** ************************************************************************************
510 * Sets the internal value to a time span provided in days.
511 * @param days The time span to set in days.
512 * @return \c *this to allow concatenated calls.
513 **************************************************************************************/
514 static
515 Duration FromDays( double days )
516 {
517 return std::chrono::duration_cast<TDuration>(
518 std::chrono::hours(24) * days );
519 }
520
521 /** ************************************************************************************
522 * Sets the internal value to a time span provided in days.
523 * @param days The time span to set in days.
524 * @return \c *this to allow concatenated calls.
525 **************************************************************************************/
526 static
528 {
529 return std::chrono::duration_cast<TDuration>(
530 std::chrono::hours(24) * days );
531 }
532
533 /** ************************************************************************************
534 * Sets the internal value to a time span provided in hours.
535 * @param hours The time span to set in hours.
536 * @return \c *this to allow concatenated calls.
537 **************************************************************************************/
538 static
539 Duration FromHours( double hours )
540 {
541 return std::chrono::duration_cast<TDuration>(
542 std::chrono::hours(1) * hours );
543 }
544
545 /** ************************************************************************************
546 * Sets the internal value to a time span provided in hours.
547 * @param hours The time span to set in hours.
548 * @return \c *this to allow concatenated calls.
549 **************************************************************************************/
550 static
552 {
553 return std::chrono::duration_cast<TDuration>(
554 std::chrono::hours(1) * hours );
555 }
556
557 /** ************************************************************************************
558 * Sets the internal value to a time span provided in hours.
559 * @param minutes The time span to set in minutes.
560 * @return \c *this to allow concatenated calls.
561 **************************************************************************************/
562 static
563 Duration FromMinutes(double minutes )
564 {
565 return std::chrono::duration_cast<TDuration>(
566 std::chrono::minutes(1) * minutes );
567 }
568
569 /** ************************************************************************************
570 * Sets the internal value to a time span provided in hours.
571 * @param minutes The time span to set in minutes.
572 * @return \c *this to allow concatenated calls.
573 **************************************************************************************/
574 static
576 {
577 return std::chrono::duration_cast<TDuration>(
578 std::chrono::minutes(1) * minutes );
579 }
580
581 /** ************************************************************************************
582 * Sets the internal value to a time span provided in seconds.
583 * @param seconds The time span to set in seconds.
584 * @return \c *this to allow concatenated calls.
585 **************************************************************************************/
586 static
587 Duration FromSeconds( double seconds )
588 {
589 return std::chrono::duration_cast<TDuration>(
590 std::chrono::seconds(1) * seconds );
591 }
592
593 /** ************************************************************************************
594 * Sets the internal value to a time span provided in seconds.
595 * @param seconds The time span to set in seconds.
596 * @return \c *this to allow concatenated calls.
597 **************************************************************************************/
598 static
599 Duration FromAbsoluteSeconds( int64_t seconds )
600 {
601 return std::chrono::duration_cast<TDuration>(
602 std::chrono::seconds(1) * seconds );
603 }
604
605 /** ************************************************************************************
606 * Sets the internal value to a time span provided in milliseconds.
607 * @param milliseconds The time span to set in milliseconds.
608 * @return \c *this to allow concatenated calls.
609 **************************************************************************************/
610 static
611 Duration FromMilliseconds( double milliseconds )
612 {
613 return std::chrono::duration_cast<TDuration>(
614 std::chrono::milliseconds(1) * milliseconds );
615 }
616
617 /** ************************************************************************************
618 * Sets the internal value to a time span provided in milliseconds.
619 * @param milliseconds The time span to set in milliseconds.
620 * @return \c *this to allow concatenated calls.
621 **************************************************************************************/
622 static
623 Duration FromAbsoluteMilliseconds( int64_t milliseconds )
624 {
625 return std::chrono::duration_cast<TDuration>(
626 std::chrono::milliseconds(1) * milliseconds );
627 }
628
629 /** ************************************************************************************
630 * Sets the internal value to a time span provided in microseconds.
631 * @param microseconds The time span to set in microseconds.
632 * @return \c *this to allow concatenated calls.
633 **************************************************************************************/
634 static
635 Duration FromMicroseconds( double microseconds )
636 {
637 return std::chrono::duration_cast<TDuration>(
638 std::chrono::microseconds(1) * microseconds );
639 }
640
641 /** ************************************************************************************
642 * Sets the internal value to a time span provided in microseconds.
643 * @param microseconds The time span to set in microseconds.
644 * @return \c *this to allow concatenated calls.
645 **************************************************************************************/
646 static
647 Duration FromAbsoluteMicroseconds( int64_t microseconds )
648 {
649 return std::chrono::duration_cast<TDuration>(
650 std::chrono::microseconds(1) * microseconds );
651 }
652
653 /** ************************************************************************************
654 * Sets the internal value to a time span provided in nanoseconds.
655 * @param nanoseconds The time span to set in nanoseconds.
656 * @return \c *this to allow concatenated calls.
657 **************************************************************************************/
658 static
659 Duration FromNanoseconds( int64_t nanoseconds )
660 {
661 return std::chrono::duration_cast<TDuration>(
662 std::chrono::nanoseconds( nanoseconds ) );
663 }
664 }; // inner class Duration
665
666 // #############################################################################################
667 // protected fields
668 // #############################################################################################
669
670 protected:
671 /** The internal timer value. This value can be accessed using methods #Export and
672 * #Import. */
674
675 // #############################################################################################
676 // Constructors
677 // #############################################################################################
678
679 public:
680 /** ****************************************************************************************
681 * Creates an instance representing the point in time when this constructor
682 * was invoked.
683 *
684 * The default invocation measures the current point in time, which introduces some
685 * effort. To avoid this in situations where an instance is overwritten by the result of
686 * static method #Now at a later point in time, parameter \p{init} can be used to suppress
687 * the initial measurement.
688 *
689 * @param init If \c Initialization::Perform, the current time is measured and set.
690 * If \c Initialization::Suppress, the time stamp will be initialized to
691 * represent the start of the epoch.<br>
692 * Defaults to \c Initialization::Perform.
693 ******************************************************************************************/
694 constexpr
700
701 /** ****************************************************************************************
702 * Returns an instance representing the actual point in time.
703 * @return A value type of \p{TDerived}. Usually this is assigned to a value variable which
704 * might have been constructed with measurement suppression.
705 ******************************************************************************************/
706 static
707 TDerived Now()
708 {
709 return TDerived(TClock::now());
710 }
711
712 /** ****************************************************************************************
713 * Resets this instance to the current point in time.
714 * @return A reference to \c this, (casted to the derived type) to allow concatenated
715 * operations.
716 ******************************************************************************************/
717 TDerived& Reset()
718 {
719 stamp= TClock::now();
720 return *static_cast<TDerived*>(this);
721 }
722
723 /** ****************************************************************************************
724 * Constructor using native C++ library values.
725 * \see Methods #Import and #Export.
726 *
727 * @param internalValue The value to copy into this.
728 ******************************************************************************************/
729 constexpr
730 TimePointBase( TTimePoint internalValue )
731 : stamp(internalValue)
732 {}
733
734 // #############################################################################################
735 // Interface
736 // #############################################################################################
737 public:
738
739 /** ****************************************************************************************
740 * Returns \c true if this object is not representing the start of the epoch.
741 * An uninitialized object that returns \c false can be created with provision of
742 * \c Initialization::Suppress on construction.
743 *
744 * @return \c true if this object is initialized, \c false otherwise.
745 ******************************************************************************************/
746 bool IsSet()
747 {
748 return stamp != TTimePoint();
749 }
750
751 /** ****************************************************************************************
752 * Unsets this object, hence makes this object representing the start of the epoch, as if
753 * it was constructed with parameter value \c Initialization::Suppress.
754 ******************************************************************************************/
755 void Unset()
756 {
757 stamp= TTimePoint();
758 }
759
760 /** ****************************************************************************************
761 * Copies the value from the given object.
762 * @param other The point in time to copy from.
763 ******************************************************************************************/
764 void SetAs( const TDerived& other )
765 {
766 stamp= other.stamp;
767 }
768
769 /** ****************************************************************************************
770 * Returns the internal time value in the C++ standard library type.
771 * @return The internal value.
772 ******************************************************************************************/
774 {
775 return stamp;
776 }
777
778 /** ****************************************************************************************
779 * Sets the internal time value given by a value of C++ standard library type.
780 * @param timePoint The value to set.
781 ******************************************************************************************/
782 void Import(TTimePoint timePoint)
783 {
784 stamp= timePoint;
785 }
786
787
788 /** ****************************************************************************************
789 * Returns the internal time value in the C++ standard library's tick unit.
790 *
791 * @return The internal value.
792 ******************************************************************************************/
793 TRaw ToRaw() const
794 {
795 return stamp.time_since_epoch().count();
796 }
797
798 /** ****************************************************************************************
799 * Sets the value from a value of C++ standard library's tick unit type.
800 * @param raw The time span to create in raw units.
801 ******************************************************************************************/
802 void SetFromRaw( TRaw raw )
803 {
804 stamp= TTimePoint( typename TClock::duration( raw ) );
805 }
806
807 /** ****************************************************************************************
808 * Creates an instance from a value of C++ standard library's tick unit type.
809 *
810 * @param raw The time span to create in raw units.
811 * @return The internal value.
812 ******************************************************************************************/
813 static
814 TDerived FromRaw( TRaw raw )
815 {
816 return TDerived( TTimePoint( typename TClock::duration( raw ) ) );
817 }
818
819 /** ****************************************************************************************
820 * Addition operator.
821 * @param timeSpan The time span to add.
822 * @return A time stamp object containing the sum.
823 ******************************************************************************************/
824 TDerived operator+( const Duration& timeSpan ) const
825 {
826 return stamp + std::chrono::duration_cast<typename TClock::duration>( timeSpan.span );
827 }
828
829 /** ****************************************************************************************
830 * Assignment by sum operator.
831 * @param timeSpan The time span to add.
832 * @return A reference to this object.
833 ******************************************************************************************/
834 TDerived operator+=( const Duration& timeSpan )
835 {
836 stamp+= std::chrono::duration_cast<typename TClock::duration>( timeSpan.span );
837 return stamp;
838 }
839
840 /** ****************************************************************************************
841 * Subtraction operator.
842 * @param timeSpan The time span to subtract.
843 * @return A time stamp object containing the sum.
844 ******************************************************************************************/
845 TDerived operator-( const Duration& timeSpan ) const
846 {
847 return stamp - std::chrono::duration_cast<typename TClock::duration>( timeSpan.span );
848 }
849
850 /** ****************************************************************************************
851 * Assignment by difference operator.
852 * @param timeSpan The time span to subtract.
853 * @return A reference to this object.
854 ******************************************************************************************/
855 TDerived operator-=( const Duration& timeSpan )
856 {
857 stamp-= timeSpan.span;
858 return stamp;
859 }
860
861 /** ****************************************************************************************
862 * Subtraction operator with other time span argument. If the given time stamp represents
863 * a point in type earlier than the one this object represents, the result is positive.
864 * @param other The time stamp to subtract.
865 * @return A time span object containing the difference.
866 ******************************************************************************************/
867 Duration operator-( const TDerived& other ) const
868 {
869 return Duration(stamp - other.stamp);
870 }
871
872 // #############################################################################################
873 // Interface Age, Since
874 // #############################################################################################
875
876 /** ****************************************************************************************
877 * Returns the time span between value represented by this instance and the current system
878 * time.
879 * If the internal value represents a historic point in time, the result is positive.
880 *
881 * @return The age of this instance stored in a new Duration.
882 ******************************************************************************************/
883 Duration Age() const
884 {
885 return Duration( TDerived().stamp - this->stamp );
886 }
887
888 /** ****************************************************************************************
889 * Returns the time span between the value represented by this instance and the given
890 * other time stamp. If the given time stamp represents an earlier point in time, the result
891 * is positive.
892 *
893 * @param other The value to compare this instance with
894 *
895 * @return The age of this instance stored in the given or created object.
896 ******************************************************************************************/
897 Duration Since( const TDerived& other ) const
898 {
899 return (*this) - other;
900 }
901
902 /** ****************************************************************************************
903 * Determines if this object's age is higher than a given time span.
904 *
905 * @param timeSpan A time span to compare.
906 * @return \c true if the given time span is smaller equal than the age of this object,
907 * hence to the time span passed since the point in time this object represents.
908 * \c false otherwise.
909 ******************************************************************************************/
910 bool IsOlderThan( Duration timeSpan ) const
911 {
912 return Age() > timeSpan;
913 }
914
915}; // class TimePointBase
916
917}} // namespace [alib::time]
918
919
920
921#endif // HPP_ALIB_TIME_TIMEPOINTBASE
Duration operator/(int64_t divisor) const
static Duration FromNanoseconds(int64_t nanoseconds)
Duration & SetMinimum(const Duration &other)
static Duration FromAbsoluteMicroseconds(int64_t microseconds)
Duration operator-(const Duration &rhs) const
Duration & SetMaximum(const Duration &other)
static Duration FromMicroseconds(double microseconds)
bool operator<=(const Duration &other) const
Duration operator+(const Duration &rhs) const
Duration operator*(int64_t multiplier) const
Duration & operator*=(double multiplier)
static Duration Import(TDuration timeSpan)
Duration & operator+=(const Duration &other)
Duration operator*(double multiplier) const
static Duration FromAbsoluteMilliseconds(int64_t milliseconds)
static Duration FromHours(double hours)
bool operator==(const Duration &other) const
static Duration FromMilliseconds(double milliseconds)
typename std::chrono::steady_clock::duration TDuration
Duration operator/(double divisor) const
bool operator!=(const Duration &other) const
static Duration FromAbsoluteSeconds(int64_t seconds)
static Duration FromAbsoluteHours(int64_t hours)
Duration & operator/=(int64_t divisor)
static Duration FromAbsoluteMinutes(int64_t minutes)
Duration & operator/=(double divisor)
bool operator<(const Duration &other) const
double InHertz(int qtyFractionalDigits=-1) const
Duration & operator*=(int64_t multiplier)
Duration & operator-=(const Duration &other)
bool operator>(const Duration &other) const
static Duration FromSeconds(double seconds)
static Duration FromMinutes(double minutes)
Duration(TDuration internalValue)
static Duration FromAbsoluteDays(int64_t days)
bool operator>=(const Duration &other) const
static Duration FromDays(double days)
TDerived operator-(const Duration &timeSpan) const
void SetAs(const TDerived &other)
Duration operator-(const TDerived &other) const
bool IsOlderThan(Duration timeSpan) const
TDerived operator+(const Duration &timeSpan) const
typename TClock::time_point TTimePoint
Duration Since(const TDerived &other) const
TDerived operator-=(const Duration &timeSpan)
TDerived operator+=(const Duration &timeSpan)
typename TTimePoint::rep TRaw
TTimePoint Export() const
void Import(TTimePoint timePoint)
constexpr TimePointBase(TTimePoint internalValue)
constexpr TimePointBase(lang::Initialization init=lang::Initialization::Perform)
static TDerived FromRaw(TRaw raw)
#define ALIB_ASSERT_MODULE(modulename)
Definition alib.hpp:190
Definition alib.cpp:57
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286