The starting point (epoch) and the additional time duration define the time point. It consists of two components, clock and time duration.
Time point is a class template. std::chrono::time_point requires a clock. By default, the time duration is derived from the clock.
There a four special time points depending on the clock.
- epoch: The starting point of the clock.
- now: The current time.
- min: The minimum time point that the clock can display.
- max: The maximum time point that the clock can display.
The accuracy of the values depends on the used clock: std::system::system_clock, std::chrono::steady_clock, or std::chrono::high_resolution_clock.
C++ gives no guarantee about a clock’s accuracy, starting point, or valid time range. The starting point of std::chrono::system_clock is typically the 1.1.1970, the so-called UNIX-epoch. It holds further that the std::chrono::high_resolution clock has the highest accuracy.
Displaying a time duration as a date
If the time point uses internally std::chrono::system_clock, you can convert the time point with the help of std::chrono::system_clock::to_time_t in an object of type std::time_t. Thanks to further conversions with the functions std::gmtime and std::asctime, you have the time points as dates in textual representation available.
The output of the program shows the valid range of std::chrono::system_clock. std::chrono::system_clock has the UNIX-epoch as starting point and can have time points between 1677 and 2262.
You can add time durations to time points to get new time points. Now I’m curious. What will happen if I’m out of the valid range of the time points?
Beyond the boundaries of the valid time range
My experiment uses the current time and adds 1000 years or subtracts 1000 years from it. I ignore leap years and assume that the year has 365 days for simplicity.
For the sake of readability, I introduced the namespace std::chrono. The program’s output shows that an overflow of the time points in lines 24 and 27 causes wrong results. Subtracting 1000 years from the current time point gives a time point in the future; adding 1000 years to the current time point gives a time point in the past, respectively.
The difference between two time points is the time duration. Time durations support the basic arithmetic and can be displayed in different time ticks. How? Wait for the 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, and Marco Parri Empoli.
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|
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,