ALib C++ Library
Library Version: 2402 R1
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 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_TICKSCONVERTER
9#define HPP_ALIB_TIME_TICKSCONVERTER 1
10
11#ifndef HPP_ALIB_TIME_TICKS
12# include "alib/time/ticks.hpp"
13#endif
14
15#ifndef HPP_ALIB_TIME_DATETIME
16# include "alib/time/datetime.hpp"
17#endif
18
19namespace alib { namespace time {
20
21/** ************************************************************************************************
22 * As explained in detail in the documentation of module \alib_time, a steady time model is
23 * supported with class \alib{time;Ticks} and a non-steady one representing the system clock with
24 * class \alib{time;DateTime}.
25 * Only values of the latter type can be converted to human readable (calendar) date and time values.
26 *
27 * In some situations however, a software that requires steady, monotonic time points, may also be
28 * required to present these time points in human readable format. It is of-course possible
29 * to do some sort of conversion. For that, simply both clocks need to be probed at the same point
30 * in time and then time points of both notions can be put in relation to these two probes.
31 *
32 * The effect however is that the conversion results will change for all values as soon as
33 * the system clock is changed and the probe values of the two clocks are updated.
34 * This is true also for values that are "older" than the point in time that the clock change
35 * happened. The reason is quickly understood: The system clock's counter changes, while the steady
36 * clock's counter does not.
37 *
38 * To give the user of \alib full control about how system clock changes are reflected,
39 * the conversion of time points is encapsulated by this class together with one pair of clock probe
40 * data. A software can use one ore more instances of this class and update (synchronize) these
41 * instances independently.
42 **************************************************************************************************/
44{
45 protected:
46 /** Time point of steady clock of last invocation of #SyncClocks. */
48
49 /** Time point of system clock of last invocation of #SyncClocks. */
51
52 public:
53 /** ****************************************************************************************
54 * Constructor. Invokes #SyncClocks.
55 ******************************************************************************************/
57 {
58 SyncClocks();
59 }
60
61 /** ****************************************************************************************
62 * Generates a set of "probes" of the steady, monotonic clock and the system clock.
63 * The measurement of both clocks is repeated the given number of times and the pair with
64 * the smallest difference between both is chosen.
65 * This approach mitigates the risk of using a pair for which thread execution had been
66 * interrupted during the two measurements.
67 *
68 * Note that after a call to this method, the conversion methods may return slightly
69 * different values than before the call, even if the system clock was not changed.
70 *
71 * If this method is not invoked after a change of the system clock, such change of the
72 * system clock is not reflected by the conversion methods.
73 * In other words, the conversion methods always work just as if the system clock had not
74 * changed since the last invocation of this method.
75 *
76 * \note
77 * On a GNU/Linux workstation (without workload), the error observed when doing only one
78 * measurement was in the magnitude of several microseconds.
79 *
80 * @param qtyRepeats The number of measurements to perform. Defaults to \c 5.
81 ******************************************************************************************/
83 void SyncClocks( int qtyRepeats = 5 );
84
85 /** ****************************************************************************************
86 * Sets the pair of conversion times to equal the other converter object. This is useful
87 * to avoid differences in conversion across converter instances used in a software.
88 *
89 * @param other Another converter object to copy the synchronization information from.
90 ******************************************************************************************/
96
97 /** ****************************************************************************************
98 * Converts a \b %Ticks object to a \b %DateTime object.
99 * @param ticks The ticks object to convert.
100 * @return The date time object.
101 ******************************************************************************************/
103 {
105 + std::chrono::duration_cast<std::chrono::system_clock::duration>(
106 ticks.Export() - steadyClockSyncTime ) );
107 }
108
109 /** ****************************************************************************************
110 * Converts a \b %DateTime object to a \b %Ticks object.
111 * @param dateTime The date/time object to convert.
112 * @return The date time object.
113 ******************************************************************************************/
115 {
116 return Ticks( dateTime.Export() - systemClockSyncTime + steadyClockSyncTime);
117 }
118};
119
120
121} // namespace alib[::time]
122
123/// Type alias in namespace \b alib.
125
126} // namespace [alib]
127
128
129#endif // HPP_ALIB_TIME_TICKSCONVERTER
Ticks ToTicks(DateTime dateTime)
Ticks::TTimePoint steadyClockSyncTime
DateTime ToDateTime(Ticks ticks)
ALIB_API void SyncClocks(int qtyRepeats=5)
Definition time.cpp:81
void SetAs(const TickConverter &other)
DateTime::TTimePoint systemClockSyncTime
TTimePoint Export() const
#define ALIB_API
Definition alib.hpp:538
Definition alib.cpp:57
time::Ticks Ticks
Type alias in namespace alib.
Definition ticks.hpp:114
time::DateTime DateTime
Type alias in namespace alib.
Definition datetime.hpp:226