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