ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
tickconverter.hpp
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header-file is part of the \aliblong. It does not belong to an \alibmod and is
4/// included in any \alibbuild.
5///
6/// Copyright 2013-2026 A-Worx GmbH, Germany.
7/// Published under #"mainpage_license".
8//==================================================================================================
9ALIB_EXPORT namespace alib { namespace time {
10
11//==================================================================================================
12/// As explained in detail in the documentation of the module \alib_time, a steady time model is
13/// supported with class #"Ticks" and a non-steady one representing the system clock with
14/// class #"time::DateTime".
15/// Only values of the latter type can be converted to human-readable (calendar) date and time values.
16///
17/// In some situations however,software that requires steady, monotonic time points, may also be
18/// required to present these time points in human-readable format. It is possible to do some sort
19/// of conversion. For that, simply both clocks need to be probed at the same point
20/// in time and then time points of both notions can be put in relation to these two probes.
21///
22/// The effect, however, is that the conversion results will change for all values as soon as
23/// the system clock is changed and the probe values of the two clocks are updated.
24/// This is true also for values that are "older" than the point in time that the clock change
25/// happened. The reason is quickly understood: The system clock's counter changes, while the steady
26/// clock's counter does not.
27///
28/// To give the user of \alib full control about how system clock changes are reflected,
29/// the conversion of time points is encapsulated by this class together with one pair of clock probe
30/// data. software can use one ore more instances of this class and update (synchronize) these
31/// instances independently.
32//==================================================================================================
34 protected:
35 /// Time point of steady clock of last invocation of #"SyncClocks".
37
38 /// Time point of system clock of last invocation of #"SyncClocks".
40
41 public:
42 /// Constructor. Invokes #"SyncClocks".
44
45 /// Generates a set of "probes" of the steady, monotonic clock and the system clock.
46 /// The measurement of both clocks is repeated the given number of times and the pair with
47 /// the smallest difference between both is chosen.
48 /// This approach mitigates the risk of using a pair for which thread execution had been
49 /// interrupted during the two measurements.
50 ///
51 /// Note that after a call to this method, the conversion methods may return slightly
52 /// different values than before the call, even if the system clock was not changed.
53 ///
54 /// If this method is not invoked after a change of the system clock, such change of the
55 /// system clock is not reflected by the conversion methods.
56 /// In other words, the conversion methods always work just as if the system clock had not
57 /// changed since the last invocation of this method.
58 ///
59 /// \note
60 /// On a GNU/Linux workstation (without workload), the error observed when doing only one
61 /// measurement was in the magnitude of several microseconds.
62 ///
63 /// @param qtyRepeats The number of measurements to perform. Defaults to \c 5.
65 void SyncClocks( int qtyRepeats = 5 );
66
67 /// Sets the pair of conversion times to equal the other converter object. This is useful
68 /// to avoid differences in conversion across converter instances used in software.
69 ///
70 /// @param other Another converter object to copy the synchronization information from.
76
77 /// Converts a #"%Ticks" object to a #"%time::DateTime" object.
78 /// @param ticks The ticks object to convert.
79 /// @return The date time object.
82 + std::chrono::duration_cast<std::chrono::system_clock::duration>(
83 ticks.Export() - steadyClockSyncTime ) );
84 }
85
86 /// Converts a #"%time::DateTime" object to a #"%Ticks" object.
87 /// @param dateTime The date/time object to convert.
88 /// @return The date time object.
90 { return Ticks( dateTime.Export() - systemClockSyncTime + steadyClockSyncTime); }
91};
92
93
94} // namespace alib[::time]
95
96/// Type alias in namespace #"%alib".
98
99} // namespace [alib]
#define ALIB_DLL
#define ALIB_EXPORT
DateTime ToDateTime(Ticks ticks)
DateTime::TTimePoint systemClockSyncTime
Time point of system clock of last invocation of #"SyncClocks".
void SetAs(const TickConverter &other)
Ticks::TTimePoint steadyClockSyncTime
Time point of steady clock of last invocation of #"SyncClocks".
void SyncClocks(int qtyRepeats=5)
TickConverter()
Constructor. Invokes #"SyncClocks".
Ticks ToTicks(DateTime dateTime)
TTimePoint Export() const
typename std::chrono::steady_clock::time_point TTimePoint
Definition alox.cpp:14
time::Ticks Ticks
Type alias in namespace #"%alib".
Definition ticks.hpp:86
time::TickConverter TickConverter
Type alias in namespace #"%alib".
time::DateTime DateTime
Type alias in namespace #"%alib".
Definition datetime.hpp:188