ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
tickconverter.cpp
1// #################################################################################################
2// ALib C++ Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14// ====================================== Global Fragment ======================================
15#include "alib/alib.inl"
16// =========================================== Module ==========================================
17#if ALIB_C20_MODULES
18 module ALib.Time;
19#else
20# include "ALib.Time.H"
21#endif
22// ====================================== Implementation =======================================
23using namespace std::chrono;
24namespace alib::time {
25
26
27# include "ALib.Lang.CIMethods.H"
28
29// #################################################################################################
30// TickConverter
31// #################################################################################################
32void TickConverter::SyncClocks( int qtyRepeats )
33{
34 Ticks::TTimePoint steadyClock;
35 DateTime::TTimePoint systemClock;
36 uint64_t lastDiff= 0;
37 for( int i= 0 ; i < qtyRepeats ; ++i )
38 {
39 systemClock= system_clock::now();
40 steadyClock= steady_clock::now();
41
42 auto systemCount= systemClock.time_since_epoch().count();
43 auto steadyCount= steadyClock.time_since_epoch().count();
44
45 // This cannot be optimized, because:
46 // a) we have to use an unsigned integer, and
47 // b) we have to take into account which clock was measured first and which last. If
48 // interrupted between the calls, the difference either shrinks or increases.
49 if( systemCount < steadyCount )
50 {
51 uint64_t diff= uint64_t( steadyCount - systemCount );
52 if( lastDiff == 0 || diff < lastDiff )
53 {
54 steadyClockSyncTime= steadyClock;
55 systemClockSyncTime= systemClock;
56 lastDiff= diff;
57 }
58 }
59 else
60 {
61 uint64_t diff= uint64_t( systemCount - steadyCount );
62 if( lastDiff == 0 || diff > lastDiff )
63 {
64 steadyClockSyncTime= steadyClock;
65 systemClockSyncTime= systemClock;
66 lastDiff= diff;
67 }
68 }
69 }
70}
71
72} // namespace [alib::time]
73
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.
ALIB_DLL void SyncClocks(int qtyRepeats=5)
typename std::chrono::steady_clock::time_point TTimePoint