This post concludes my introduction to the chrono extension in C++20. Today I present the time-zones functionality.
I should have written today I mainly present the time-zones functionality in C++20. Before I write about the time zones in C++20, I want to present the online resource Examples and Recipes from Howard Hinnant, which has about 40 examples of the new chrono functionality. Presumably, the chrono extension in C++20 is not easy to get; therefore it’s pretty essential to have so many examples. You should use these examples as a starting point for further experiments and sharpen your understanding. You can also add your recipes.
Calculating Ordinal Dates
“An ordinal date consists of a year and a day of year (1st of January being day 1, 31st of December being day 365 or day 366). The year can be obtained directly from year_month_day. And calculating the day is wonderfully easy. In the code below we make us of the fact that year_month_day can deal with invalid dates like the 0th of January:” (Roland Bock)
I added the necessary headers to Roland’s program.
I want to add a few remarks to the program. Line (1) truncates the current time point. The value is used in the following line to initialize a calendar date. Line (2) calculates the time duration between the two time points. Both time points have the resolution day. Finally,
year_day.count() inline (3) returns the time duration in days.
My following examples of time zones are also inspired by the already mentioned web resource Examples and Recipes.
First, a time zone is a region and its entire history of the date, such as daylight saving time or leap seconds. The time-zone library in C++20 is a complete parser of the IANA timezone database. The following table should give you a first idea of the new functionality.
I use in my examples the function
std::chrono::zones_time , which is essentially a time zone combined with a time point.
Before I show you two examples, I want to make a short remark. To compile a program using the time-zone library, you have to compile the
tz.cpp file and link it against the
curl library. The curl library is necessary to get the current IANA timezone database. The following command line for G++ should give you the idea:
My first program is straightforward. It displays the UTC and the local time.
UTC Time and Local Time
The UTC or Coordinated Univeral Time is the primary time standard worldwide. A computer uses Unix time which is a very close approximation of UTC. The UNIX time is the number of seconds since the Unix epoch. The Unix epoch is 00:00:00 UTC on 1 January 1970.
std::chrono::system_clock::now() inline (1) returns in the following program
localTime.cpp the Unix time.
I have not added too much to the program. The code block beginning with line (1) gets the current time point, truncates it to seconds, and displays it. The call
std::chrono::zoned_time localTime. The following call
localTime.get_local_time() returns the stored time point as a local time. This time point is also truncated to seconds.
localTime (line 3) can also be used to get information about the time zone. In this case, I’m interested in the offset to the UTC.
My last program answers a crucial question when I teach in a different time zone: When should I start my online class?
Various Time Zones for Online Classes
onlineClass.cpp answers the following question: How late is it in given time zones, when I start an online class at the
7h, 13h, or
17h local time (Germany)?
The online class should start on the 1st of February 2021, taking 4 hours. Because daylight saves time, the calendar date is essential to get the correct answer.
Before I dive into the functions
getMinutes (line 1) and
printStartEndTimes (line 2), let me say a few words about the
main function. The
main function defines the day of the class, the duration of the class, and all time zones. Finally, the range-based for-loop (line 3) iterates through all potential starting points for an online class. All necessary information is displayed thanks to the function
The few lines beginning with line (4) calculate the
endDate of my training by adding the start and class duration to the calendar date. Both values are displayed with the help of the function
getMinutes (line 1).
date::floor<std::chrono::minutes>(zonedTime.get_local_time()) gets the stored time point out of the
std::chrono::zoned_time and truncates the value to the minute resolution. To properly align the output of the program, line (5)
determines the size of the longest of all time-zone names. Line (6) iterates through all time zones and displays the name of the time-zone and the beginning and end of each online class. A few calendar dates even cross the day boundaries.
There is more to write about in the extended Chrono library. For example, C++20 offers new clocks such as
std::chrono::utc_clock that include leap seconds, or the
std::chrono::tai_clock that represents the International Atomic Time (TAI). Additionally, thanks to the new formatting library in C++20, time durations can be nicely formatted. This feature is not available so far. If you want to study the formatting rules for time durations, here are they: std::formatter.
You may have a lot of fun if you want to compare signed and unsigned integrals. This fun ends with C++20.
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, and Bhavith C Achar.
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,