Chrono library in C++

C++Server Side ProgrammingProgramming

In this section we will see what is the Chrono library in C++. This Chrono library is used for date and time. Timers and clocks are different in different systems. So if we want to improve time over precision we can use this library.

In this library, it provides precision-neutral concept, by separating the durations and point of time.

The duration objects are used to express time span by means of a count like minute, two hours or ten minutes. For example, 30 seconds is represented by a duration consisting of 30 ticks of unit of 1 seconds.

Example Code

#include <iostream>
#include <chrono>
using namespace std;
int main () {
   using namespace std::chrono;
   // chrono::milliseconds is an instantiation of std::chrono::duration milliseconds mili(1000);
   mili = mili*60;
   cout << "Duration : ";
   cout << mili.count() << " milliseconds.\n";
   cout << "Duration : ";
   cout << (mili.count() * milliseconds::period::num / milliseconds::period::den);
   cout << " seconds.\n";


Duration : 60000 milliseconds.
Duration : 60 seconds.
Published on 04-Apr-2019 08:06:08