Chrono in C++

Chrono library is used to deal with date and time. This library was designed to deal with the fact that timers and clocks might be different on different systems and thus to improve over time in terms of precision. The unique thing about chrono is that it provides a precision-neutral concept by separating duration and point of time (“timepoint”) from specific clocks.

chrono is the name of a header and also of a sub-namespace: All the elements in this header (except for the common_type specializations) are not defined directly under the std namespace (like most of the standard library) but under the std::chrono namespace.
The elements in this header deal with time. This is done mainly by means of three concepts:

Duration

std::chrono::nanoseconds
std::chrono::microseconds

using namespace std::chrono;
using namespace std;
template <typename T>
void duration_int(duration<long long,T> d){
    qDebug()<<d.count();
}

template <typename T>
void duration_float(duration<float,T> d){
    qDebug()<<d.count();
}
int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    duration_int(100ms);//T=std::milli == std::chrono::milliseconds
    duration_int(345us);//T=std::micro ==std::chrono::microseconds
    duration_int(5ns);//T=std::nano =std::chrono::nanoseconds
    duration_int(345s);//std::chrono::seconds
    duration_int(35h);//T=ratio<3600> =std::chrono::hours

    duration_float<std::ratio<60,10>>(6500s);//minutes*10 (6500/60)*10=1083.333

    return app.exec();
}
constexpr auto year = 31556952ll; // seconds in average Gregorian year
 
int main()
{
    using shakes = std::chrono::duration<int, std::ratio<1, 100000000>>;
    using jiffies = std::chrono::duration<int, std::centi>;
    using microfortnights = std::chrono::duration<float, std::ratio<14*24*60*60, 1000000>>;
    using nanocenturies = std::chrono::duration<float, std::ratio<100*year, 1000000000>>;
 
    std::chrono::seconds sec(1);
 
    std::cout << "1 second is:\n";
 
    // integer scale conversion with no precision loss: no cast
    std::cout << std::chrono::microseconds(sec).count() << " microseconds\n"
              << shakes(sec).count() << " shakes\n"
              << jiffies(sec).count() << " jiffies\n";
 
    // integer scale conversion with precision loss: requires a cast
    std::cout << std::chrono::duration_cast<std::chrono::minutes>(sec).count()
              << " minutes\n";
 
    // floating-point scale conversion: no cast
    std::cout << microfortnights(sec).count() << " microfortnights\n"
              << nanocenturies(sec).count() << " nanocenturies\n";
}

Clock

A clock consists of a starting point (or epoch) and a tick rate. For example, a clock may have an epoch of February 22, 1996 and tick every second. C++ defines three clock types:

  • system_clock-It is the current time according to the system (regular clock which we see on the toolbar of the computer). It is written as- std::chrono::system_clock
  • steady_clock-It is a monotonic clock that will never be adjusted.It goes at a uniform rate. It is written as- std::chrono::steady_clock
  • high_resolution_clock– It provides the smallest possible tick period. It is written as-std::chrono::high_resolution_clock
now[static]returns a std::chrono::time_point representing the current point in time
(for all three types)
to_time_t[static]converts a system clock time point to std::time_t
(system_clock)
from_time_t[static]converts std::time_t to a system clock time point
(system_clock)
#include <iostream> 
#include <chrono> 
#include <ctime> 
  
// Function to calculate 
// Fibonacci series 
long fibonacci(unsigned n) 
{ 
    if (n < 2) return n; 
    return fibonacci(n-1) + fibonacci(n-2); 
} 
  
int main() 
{ 
    // Using time point and system_clock 
    std::chrono::time_point<std::chrono::system_clock> start, end; 
  
    start = std::chrono::system_clock::now(); 
    std::cout << "f(42) = " << fibonacci(42) << '\n'; 
    end = std::chrono::system_clock::now(); 
  
    std::chrono::duration<double> elapsed_seconds = end - start; 
    std::time_t end_time = std::chrono::system_clock::to_time_t(end); 
  
    std::cout << "finished computation at " << std::ctime(&end_time) 
              << "elapsed time: " << elapsed_seconds.count() << "s\n";
}
auto start = std::chrono::high_resolution_clock::now();
...
auto elapsed = std::chrono::high_resolution_clock::now() - start;
long long microseconds = std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t t_c = std::chrono::system_clock::to_time_t(now - std::chrono::hours(24));
std::cout << "24 hours ago, the time was "
          << std::put_time(std::localtime(&t_c), "%F %T") << '\n';
 
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
std::cout << "Hello World\n";
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout << "Printing took "
          << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count()
          << "us.\n";