The Time Library

A blog dealing with multithreading in modern C++ but not writing about the new time library is incomplete. Primarily because I often used the time library in my posts to measure the performance of shortcode snippets. Therefore, I give in this post an overview of the components of the time library: time point, time duration, and clock. I will write additional posts about each of these three components.

As ever before, the practice is the theory.

Time Point, Time Duration, and Clocks

The time library consists of three parts that are dependent on each other.

• Time point: The time point is given by its starting point – the so-called epoch – and the time duration, which refers to the epoch.
• Time duration: The time duration is the difference between two-time points. It’s measured in the number of time ticks.
• Clock: The clock consists of a starting point and a time tick. This information enables you to calculate the current time.

You can compare time points. You get a new time point when you add a time duration to a time point. The time tick is the clock’s accuracy in which you measure the time duration. The birth of Christ in my culture is the starting time point and a year, a typical time tick.

I will use the three concepts to present the lifetime of the 2011 dead father of the programming language C:  Dennis Ritchie. For simplicity reasons, I’m only interested in the years.

The time points 1941 and 2011 are defined by the birth of Christ and the time duration. Therefore, the birth of Christ is our epoch. Of course, the epoch is also a time point. When I subtract 1941 from 2011, I get the time duration. This dime duration is measured with the accuracy year. Dennis Ritchie became 70 years old.

Before I dive deeper into the time library in an upcoming post, a few remarks, a time point consists of a clock and a time duration. C++ has the clocks std::chrono::system_clock, std::chrono::steady_clock, and std::chrono::high_resolution_clock. The time duration can be positive and negative. The known time durations as std::chrono::seconds, std::chrono::minutes, or std::chrono::hours are predefined in C++. Additionally, C++14 offers time literals: 1s, 1min, or 1h. The clocks differ in their accuracy (time tick) and epoch. Although it’s not defined, std::chrono::system_clock usually starts at 1.1.1970. Each clock has a method now for returning the current time (time point).

What’s next?

In the next post, I will write about the concept of time points. Therefore, I will calculate in different time ticks how much time is passed since 1.1.1970.

Modernes C++ Mentoring

• "Fundamentals for C++ Professionals" (open)
• "Design Patterns and Architectural Patterns with C++" (open)
• "C++20: Get the Details" (open)
• "Concurrency with Modern C++" (open)
• "Generic Programming (Templates) with C++": October 2024
• "Embedded Programming with Modern C++": October 2024
• "Clean Code: Best Practices for Modern C++": March 2025
• Do you want to stay informed: Subscribe.

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, 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, Stephen Kelley, Kyle Dean, Tusar Palauri, 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, Philipp Lenk, Charles-Jianye Chen, Keith Jeffery,and Matt Godbolt.

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

Seminars

I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.

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++
• C++20

Contact Me

Modernes C++ Mentoring,

Tags:
0 replies