ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
tickconverter.cpp
1using namespace std::chrono;
2namespace alib::time {
3
4
5# include "ALib.Lang.CIMethods.H"
6
7//##################################################################################################
8// TickConverter
9//##################################################################################################
10void TickConverter::SyncClocks( int qtyRepeats ) {
11 Ticks::TTimePoint steadyClock;
12 DateTime::TTimePoint systemClock;
13 uint64_t lastDiff= 0;
14 for( int i= 0 ; i < qtyRepeats ; ++i ) {
15 systemClock= system_clock::now();
16 steadyClock= steady_clock::now();
17
18 auto systemCount= systemClock.time_since_epoch().count();
19 auto steadyCount= steadyClock.time_since_epoch().count();
20
21 // This cannot be optimized, because:
22 // a) we have to use an unsigned integer, and
23 // b) we have to take into account which clock was measured first and which last. If
24 // interrupted between the calls, the difference either shrinks or increases.
25 if( systemCount < steadyCount ) {
26 uint64_t diff= uint64_t( steadyCount - systemCount );
27 if( lastDiff == 0 || diff < lastDiff ) {
28 steadyClockSyncTime= steadyClock;
29 systemClockSyncTime= systemClock;
30 lastDiff= diff;
31 }
32 } else {
33 uint64_t diff= uint64_t( systemCount - steadyCount );
34 if( lastDiff == 0 || diff > lastDiff ) {
35 steadyClockSyncTime= steadyClock;
36 systemClockSyncTime= systemClock;
37 lastDiff= diff;
38} } } }
39
40} // namespace [alib::time]
DateTime::TTimePoint systemClockSyncTime
Time point of system clock of last invocation of #"SyncClocks".
Ticks::TTimePoint steadyClockSyncTime
Time point of steady clock of last invocation of #"SyncClocks".
void SyncClocks(int qtyRepeats=5)
typename std::chrono::steady_clock::time_point TTimePoint