Hello again!
In this video, we are going to start looking at the chrono library.
This is to do with handling times and dates.
C++ already had some features for doing this, which were inherited from C.
These are defined in the header. As you would expect for c,
these are low-level and, to be honest, they are not very well designed.
C++11 introduced the chrono library for handling times.
This is much more precise than the old C stuff, but the syntax is more complicated. And now, in
C++20,
the chrono library
can also handle dates as well.
I am not going to go into that, because the compiler support for C++20 is still not yet all there.
Let's remind ourselves how the traditional C++ time support works.
There is a clock() function, which returns the number of clock ticks
since the program started. These are stored in a variable of type clock_t, which is just a synonym
for integer.
These can be used for intervals of up to a few minutes.
The precision depends on the implementation, but usually it is to the nearest microsecond. And you do not
get the number of seconds directly.
You have to divide by a constant to get that.
The time() function takes a variable of type time_t, by address.
Again, that is a synonym for an integer.
It will set the argument to the number of seconds since midnight on the 1st of January 1970.
It was obviously a very good new year for somebody!
This can be used for longer intervals
than clock(), and you can go up to several decades.
But the precision is only to the nearest second.
C++11 provides a new library for managing times.
This is defined in the header, in the standard chrono namespace. So this actually has its own namespace,
underneath the standard namespace.
And there are three main concepts.
The first concept is of a clock.
This is something which has a start date, known as the "epoch", and a "tick rate".
So in this terminology, C has a clock which started on the 1st of January 1970.
So that is the epoch of that clock.
And the tick rate is once per second.
We have a time point, which is the number of clock ticks since the start of the epoch, at a given point
in time.
So that corresponds to the return value from calling the time() function in C. And we have a duration,
which is the interval between two time points, measured in clock ticks.
So that is equivalent to calling the C function time() twice and subtracting the return values.
Okay, so that is it for this video.
I will see you next time.
But until then, keep coding!