A clock consists of a starting point and a time tick. C++ offers with std::chrono::system_clock , std::chrono::steady_clock, and std::chrono::high_resolution_clock three clocks.
The clocksBecause of three different clocks there is the question: What are the differences?
std::chrono::sytem_clock: Is the system-wide real time clock ( wall-clock ). The clock has the auxiliary functions to_time_t and from_time_t to convert time points into dates. std::chrono::steady_clock: Provides as only clock the guarantee that you can not adjust it. Therefore, std::chrono::steady_clock is the preferred clock to wait for a time duration or until a time point. std::chrono::high_resolution_clock: Is the clock with the highest accuracy but it can be a synonym for the clocks std::chrono::system_clock or std::chrono::steady_clock.The C++ standard provides no guarantee about the accuracy, the starting point or the valid time range of the clocks. Typically, the starting point of std::chrono:system_clock is the 1.1.1970, the so called UNIX-epoch . For std::chrono::steady_clock typically the boot time of your PC.
Accuracy and SteadinessIt's interesting to know, which clocks are steady and which accuracy they provide. You get the answers from the clocks.
// clockProperties.cpp#include <chrono>
#include <iomanip>
#include <iostream>
template <typename T>
void printRatio(){
std::cout << " precision: " << T::num << "/" << T::den << " second " << std::endl;
typedef typename std::ratio_multiply<T,std::kilo>::type MillSec;
typedef typename std::ratio_multiply<T,std::mega>::type MicroSec;
std::cout << std::fixed;
std::cout << " " << static_cast<double>(MillSec::num)/MillSec::den << " milliseconds " << std::endl;
std::cout << " " << static_cast<double>(MicroSec::num)/MicroSec::den << " microseconds " << std::endl;
}
int main(){
std::cout << std::boolalpha << std::endl;
std::cout << "std::chrono::system_clock: " << std::endl;
std::cout << " is steady: " << std::chrono::system_clock::is_steady << std::endl;
printRatio<std::chrono::system_clock::period>();
std::cout << std::endl;
std::cout << "std::chrono::steady_clock: " << std::endl;
std::cout << " is steady: " << std::chrono::steady_clock::is_steady << std::endl;
printRatio<std::chrono::steady_clock::period>();
std::cout << std::endl;
std::cout << "std::chrono::high_resolution_clock: " << std::endl;
std::cout << " is steady: " << std::chrono::high_resolution_clock::is_steady << std::endl;
printRatio<std::chrono::high_resolution_clock::period>();
std::cout << std::endl;
}
I display in the lines 22, 28, and 34 for each clock wheter it's continuous. My job in the function printRatio (line 7 - 15) is more challenging. First, I show the accuracy of the clocks in a fraction, second in a floating number. Therefore, I use the function template std::ratio_multiply and the constants std::kilo and std::mega to adjust the units to milliseconds and microseconds. You can get the details on the calculation at compile time at cppreference.com.
The output on linux differs from that on windows. std::chrono::system_clock is far more accurate on Linux; std::chrono::high_resultion_clock is steady on Windows.


Although the C++ standard doesn't specify the epoch of the clock you can calculate it.
EpochThanks to the auxiliary function time_since_epoch you get from each time point who much time has passed since the epoch.
// now.cpp#include <chrono>
#include <iomanip>
#include <iostream>
template <typename T>
void durationSinceEpoch(T dur){
std::cout << " Counts since epoch: " << dur.count() << std::endl;
typedef std::chrono::duration<double, std::ratio<60>> MyMinuteTick;
MyMinuteTick myMinute(dur);
std::cout << std::fixed;
std::cout << " Minutes since epoch: "<< myMinute.count() << std::endl;
typedef std::chrono::duration<double, std::ratio<60*60*24*365>> MyYearTick;
MyYearTick myYear(dur);
std::cout << " Years since epoch: " << myYear.count() << std::endl;
}
int main(){
std::cout << std::endl;
std::chrono::system_clock::time_point timeNowSysClock = std::chrono::system_clock::now();
std::chrono::system_clock::duration timeDurSysClock= timeNowSysClock.time_since_epoch();
std::cout << "std::chrono::system_clock: " << std::endl;
durationSinceEpoch(timeDurSysClock);
std::cout << std::endl;
auto timeNowStClock = std::chrono::steady_clock::now();
auto timeDurStClock= timeNowStClock.time_since_epoch();
std::cout << "std::chrono::steady_clock: " << std::endl;
durationSinceEpoch(timeDurStClock);
std::cout << std::endl;
auto timeNowHiRes = std::chrono::high_resolution_clock::now();
auto timeDurHiResClock= timeNowHiRes.time_since_epoch();
std::cout << "std::chrono::high_resolution_clock: " << std::endl;
durationSinceEpoch(timeDurHiResClock);
std::cout << std::endl;
}
The variables timeDurSysClock (line 24), timeNowStClock (line 31), and timeNowHiResClock (Zeile 38) hold for each clock, how much time has passed since the starting point of the clock. When I use no automatic type deduction with auto the explicit types of the time point and time duration are extremely verbose to write. I show the function durationSinceEpoch (line 7 - 17) I the time duration in different resolutions. First, I display the number of time ticks (line 9), than the number of minutes (line 13), and at the end the years (lines 16) since the epoch; all depending on the used clock. For simplicity reasons I ignore leap years and my year has 365 days.
The results are different on Linux and Windows.