Hello again!
In this video, we are going to look at the duration types in the chrono library.
The chrono header defines some integer types, which represent units of duration.
These are in the std::chrono namespace.
These are used for measuring time intervals.
We have hours, minutes, seconds, milliseconds, microseconds and nanoseconds.
And C++20 also has days, weeks, months and years.
These are default-initialized, just like built-in integer types.
So if we have a duration variable which is not initialized, then its value will be undefined.
The constructor takes a single argument.
So hours with an argument of 5 represents a five hour interval, and so on.
In C++14, the library provides suffixes for these units. So we can have literals, which represent 5
hours, 10 minutes, and so on.
These are in the standard literals namespace.
These duration types cannot be printed out directly.
There is no overload of the left shift operator.
Instead, there is a count() member function, which returns the numeric value of the duration.
So if we have an object which represents 2 seconds, then the count() member function will return 2. Normally,
You should not need to use this.
It's there for doing output.
And also, if you want to work with code which still uses the old, C way of doing things.
So let's try this out.
We include the header.
We use the std::chrono namespace and the std::literals namespace.
If we are using the literals with the user-defined suffixes.
We defined a seconds object, which is uninitialized.
Then we print out its value. Which might be interesting!
Then we create some duration variables, like the ones on the slide, and then we call their count() member
functions and print out the result.
So let's see what we get.
So yes, that is definitely undefined.
But the others all work as we would expect.
We can perform all the normal integer operations on duration types.
So for example, we can add them and subtract them.
So let's see what this does.
So if we add 2 seconds and 3 seconds, we get 5 seconds.
And then, if we add another 43 milliseconds, we get 5043 milliseconds.
So the result will be of the right type to store the data.
The duration constructor is explicit.
So, for example, we cannot implicitly convert from an integer to a seconds variable.
We have to use a seconds literal. And this also applies to the conversion operators.
So if we have a function which takes a duration type as an argument, then we cannot pass an integer
literal.
We have to pass a literal of the right duration type.
So this prevents scenarios like the Mars lander fiasco, where the call thought that a literal number
represented one unit and the function actually used a quite different unit.
So let's try this out.
If I call this function with a seconds literal, the function takes a seconds argument.
So that works.
If I try to call it with an int literal.
Then there is a compiler ever.
Constructor is declared explicit.
If we want to convert between duration types, we can do this implicitly, provided there is no loss
of data.
So for example, one hour can be converted to seconds, because an hour is 3600 seconds.
And that can be stored
exactly.
If we want to convert 5043 milliseconds to seconds, then we cannot do that
exactly.
And that would cause some data to be lost.
So that is not allowed.
If we want to do that, we have to use an explicit conversion, to say that we do not care about losing
these 43 milliseconds.
We do a duration_cast.
The type that we are converting to is the parameter, and the value that we are converting from is the
argument.
So this will convert 5043 milliseconds to 5 seconds. And incidentally, if you have a negative argument,
it is truncated towards zero.
So this would give -5 seconds, and not -6.
So let's try this out.
I'm conversing one hour to seconds, and then casting 5043 milliseconds, plus and minus, to seconds.
So there we are.
One hour is 3600 seconds. 5043 milliseconds is converted to 5 seconds.
If I try to do this without the cast,
then there's a compiler error. Cannot convert...
Okay.
So that is it for this video.
I will see you next time.
But until then, keep coding!