In my last post, “Calendar and Time Zone in C++20: Calendar Dates”, I presented the new calendar-related data types. Today, I go one step further and interact with them.
Assume you have a calendar date, such as
year(2100)/2/29. Your first question may be: Is this date valid?
Check if a Date is Valid
The various calendar types in C++20 have a function
ok. This function returns
true if the date is valid.
I checked in the program if a given day (line 1), a given month (line 2), or a given year (line 3) is valid. The range of a day is [1, 31], of a month [1, 12], and of a year [ -32767, 32767]. Consequently, the
ok-call on the corresponding values returns
false. Two facts are interesting when I output the various values. First, if the value is not valid, the output displays:
"is not a valid day“;
"is not a valid month“;
"is not a valid year“. Second, month values are displayed in string representation.
You can apply the
ok-call on a calendar date. Now it’s pretty easy to check if a specific calendar date is a leap day and, therefore, the corresponding year a leap year. In the worldwide used Gregorian calendar, the following rules apply:
Each year that is exactly divisible by 4 is a leap year.
- Except for years that are exactly divisible by 100. They are no leap years.
- Except for years that are exactly divisible by 400. They are leap years.
Too complicate? The program leapYears.cpp exemplifies this rule.
The extended chrono library makes it quite comfortable to ask for the duration between calendar dates.
Query Calendar Dates
Without further ado. The following program queries a few calendar dates.
With the C++20 extension, you can directly display a time point, such as now (line 1).
std::chrono::floor allows it to convert the time point to a day
std::chrono::sys_days. This value can be used to initialize the calendar type
std::chrono::year_month_day. Finally, when I put the value into a
std::chrono::year_month_weekday calendar type, I get the answer that this day is the 3rd Tuesday in October.
Of course, I can also ask for a calendar date for its components, such as the current year, month, or day (line 2).
Line (3) is the most interesting one. When subtracting from the current date in the hour’s resolution first January of the current year, I get the hour since the new year. On the contrary: When I subtract from the first January of the following year (line 4) the current date in hours resolution, I get the hours to the new year. Maybe you don’t like the hour’s resolution. Line 5 displays the values in days resolution.
I want to know the weekdays of my birthdays.
Thanks to the extended chrono library, getting the weekday of a given calendar date is pretty easy.
First, the program asks you for the year, month, and day of your birthday (line 1). Based on the input, a calendar date is created (line 2) and checked for validity (line 3). Now I display the weekday of your birthday. I use, therefore, the calendar date to fill the calendar type
std::chrono::year_month_weekday (line 4). To get the
int representation of the calendar-type year, I have to convert it to
int (line 5). Now I can display your age. Finally, the for-loop displays the following information for each of your next ten birthdays (line 6): your age, the calendar date, and the weekday. I only have to increment the
Here is a run of the program with my birthday.
A critical component in my posts to the extended Chrono library is still missing: time zones.
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,