ALib C++ Library
Library Version: 2511 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 Ticks::TTimePoint steadyClock;
34 DateTime::TTimePoint systemClock;
35 uint64_t lastDiff= 0;
36 for( int i= 0 ; i < qtyRepeats ; ++i ) {
37 systemClock= system_clock::now();
38 steadyClock= steady_clock::now();
39
40 auto systemCount= systemClock.time_since_epoch().count();
41 auto steadyCount= steadyClock.time_since_epoch().count();
42
43 // This cannot be optimized, because:
44 // a) we have to use an unsigned integer, and
45 // b) we have to take into account which clock was measured first and which last. If
46 // interrupted between the calls, the difference either shrinks or increases.
47 if( systemCount < steadyCount ) {
48 uint64_t diff= uint64_t( steadyCount - systemCount );
49 if( lastDiff == 0 || diff < lastDiff ) {
50 steadyClockSyncTime= steadyClock;
51 systemClockSyncTime= systemClock;
52 lastDiff= diff;
53 }
54 } else {
55 uint64_t diff= uint64_t( systemCount - steadyCount );
56 if( lastDiff == 0 || diff > lastDiff ) {
57 steadyClockSyncTime= steadyClock;
58 systemClockSyncTime= systemClock;
59 lastDiff= diff;
60} } } }
61
62} // 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.
ALIB_DLL void SyncClocks(int qtyRepeats=5)
typename std::chrono::steady_clock::time_point TTimePoint