Time duration is the difference between two time points. It will be measured in time ticks.
Time duration is a class template. std::chrono::duration consists of the type of the tick Rep and the length of a tick Period.
The tick length is by default std::ratio<1>. std::ratio<1> stands for a second and can also be written as std::ratio<1,1>. Therefore, it’s quite easy. std::ratio<60> is a minute and std::ratio<1,1000> a millisecond. When Rep is a floating point number, you can use it to hold fractions of time ticks.
C++11 predefines the most important time durations:
How much time has passed since the UNIX epoch (1.1.1970)? Thanks to type aliases for the different time durations, I can answer the question quite easily. For simplicity reasons, I ignore leap years and assume that a year has 365 days.
In addition to the typical time duration’s microsecond (line 16), millisecond (line 20), second (line 24), minute (line 28), hour (line 32), and year (line 36), I define the German school hour (45 min) in line 45.
It’s quite convenient to calculate with time durations.
Calculations with time durations
The time durations support basic mathematics. In the case of multiplication or division, you can multiply or divide a time duration by a number. Of course, you can compare time durations. I explicitly want to emphasize that all these calculations and comparisons respect the units of the time durations.
It gets even better with C++14 because C++14 has a bunch of predefined time literals.
How much time does my 16 year old son need for his typical school day? I will answer the question in the example and show the result in different time durations.
The time literals are constant expressions; therefore, they can be evaluated at compile time. I have time durations for a German school hour (line 12), for a short break (line 14), for a long break (line 15), for his way to the school (line 17), and his homework (line 18). The result of the calculation schoolDaysInSeconds (line 20) is available at compile time.
Impressed? I’m not. His daily duty is only about 7 1/2 hours.
The used clock gives the accuracy of the time tick. In C++, we have the clocks std::chrono::system_clock, std::chrono::steady_clock, and std::chrono::high_resolution_clock. The three clocks will be the topic of my next post.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, moon, and Philipp Lenk.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
|My special thanks to SHAVEDYAKS|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,