Converting between absolute and relative times with std::chrono
Until C++11, it was quite a hassle to take the wall clock time and just print it, because C++ did not have its own time library. It was always necessary to call functions of the C library, which looks very archaic, considering that such calls could be encapsulated nicely into their own classes.
Since C++11, the STL provides the chrono
library, which makes time-related tasks much easier to implement.
In this recipe, we are going to take the local time, print it, and play around by adding different time offsets, which is a really comfortable thing to do with std::chrono
.
How to do it...
We are going to save the current time and print it. Additionally, our program will add different offsets to the saved time point and print the resulting time points too:
- The typical include lines come first; then, we declare that we use the
std
namespace by default:
#include <iostream> #include <iomanip> #include <chrono...