WEBVTT

00:00.120 --> 00:00.690
Hello again!

00:00.960 --> 00:04.020
In this video, we are going to start looking at the chrono library.

00:04.350 --> 00:06.840
This is to do with handling times and dates.

00:09.150 --> 00:13.230
C++ already had some features for doing this, which were inherited from C.

00:13.890 --> 00:17.790
These are defined in the <ctime> header. As you would expect for c,

00:17.790 --> 00:21.750
these are low-level and, to be honest, they are not very well designed.

00:22.830 --> 00:26.970
C++11 introduced the chrono library for handling times.

00:27.780 --> 00:34.830
This is much more precise than the old C stuff, but the syntax is more complicated. And now, in

00:34.830 --> 00:35.250
C++20,

00:35.610 --> 00:36.450
the chrono library

00:36.450 --> 00:38.040
can also handle dates as well.

00:38.700 --> 00:44.550
I am not going to go into that, because the compiler support for C++20 is still not yet all there.

00:46.970 --> 00:51.380
Let's remind ourselves how the traditional C++ time support works.

00:52.040 --> 00:55.730
There is a clock() function, which returns the number of clock ticks

00:56.060 --> 01:02.690
since the program started. These are stored in a variable of type clock_t, which is just a synonym

01:02.690 --> 01:03.380
for integer.

01:05.090 --> 01:07.400
These can be used for intervals of up to a few minutes.

01:07.940 --> 01:13.940
The precision depends on the implementation, but usually it is to the nearest microsecond. And you do not

01:13.940 --> 01:15.740
get the number of seconds directly.

01:15.740 --> 01:17.810
You have to divide by a constant to get that.

01:19.920 --> 01:23.790
The time() function takes a variable of type time_t, by address.

01:24.450 --> 01:26.940
Again, that is a synonym for an integer.

01:27.720 --> 01:32.640
It will set the argument to the number of seconds since midnight on the 1st of January 1970.

01:33.150 --> 01:34.740
It was obviously a very good new year for somebody!

01:36.000 --> 01:37.660
This can be used for longer intervals

01:37.660 --> 01:39.930
than clock(), and you can go up to several decades.

01:40.320 --> 01:43.170
But the precision is only to the nearest second.

01:46.400 --> 01:50.050
C++11 provides a new library for managing times.

01:50.570 --> 01:56.630
This is defined in the <chrono> header, in the standard chrono namespace. So this actually has its own namespace,

01:57.080 --> 01:58.880
underneath the standard namespace.

01:59.630 --> 02:01.400
And there are three main concepts.

02:04.850 --> 02:06.860
The first concept is of a clock.

02:07.250 --> 02:11.990
This is something which has a start date, known as the "epoch", and a "tick rate".

02:12.590 --> 02:17.330
So in this terminology, C has a clock which started on the 1st of January 1970.

02:17.750 --> 02:19.610
So that is the epoch of that clock.

02:20.270 --> 02:22.220
And the tick rate is once per second.

02:23.090 --> 02:28.970
We have a time point, which is the number of clock ticks since the start of the epoch, at a given point

02:28.970 --> 02:29.450
in time.

02:29.930 --> 02:35.900
So that corresponds to the return value from calling the time() function in C. And we have a duration,

02:36.230 --> 02:39.890
which is the interval between two time points, measured in clock ticks.

02:40.400 --> 02:45.380
So that is equivalent to calling the C function time() twice and subtracting the return values.

02:46.550 --> 02:47.930
Okay, so that is it for this video.

02:48.260 --> 02:49.070
I will see you next time.

02:49.070 --> 02:51.080
But until then, keep coding!
