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