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