C++20: Creating Calendar Dates
In this post, I will dive deeper into calendar dates and create them.
If this post is too overwhelming for you, read my previous ones:
- Basic Chrono Terminology
- Basic Chrono Terminology with Time Duration and Time Point
- Time of Day: Details
C++20 supports constants and literals to make using calendar-date types more convenient.
Constants and Literals for Calendar Types
Let me start with the constants for std::chrono::weekday
, and std::chrono::month
.
std::chrono::weekday
std::chrono::Monday std::chrono::Thuesday std::chrono::Wednesday std::chrono::Thursday std::chrono::Friday std::chrono::Saturday std::chrono::Sunday
std::chrono::month
std::chrono::January std::chrono::February std::chrono::March std::chrono::April std::chrono::May std::chrono::June std::chrono::July std::chrono::August std::chrono::September std::chrono::October std::chrono::November std::chrono::December
C++20 supports for calendar types std::chrono::day
and std::chrono::year
two new literals: d
and y
. You can read more details about it in the post Basic Chrono Terminology with Time Duration and Time Point.
Let me create a few calendar dates.
Create Calendar Dates
The program createCalendar.cpp
shows various ways to create calendar-related dates.
// createCalendar.cpp #include <chrono> #include <iostream> int main() { std::cout << '\n'; using namespace std::chrono_literals; using std::chrono::last; using std::chrono::year; using std::chrono::month; using std::chrono::day; using std::chrono::year_month; using std::chrono::year_month_day; using std::chrono::year_month_day_last; using std::chrono::year_month_weekday; using std::chrono::year_month_weekday_last; using std::chrono::month_weekday; using std::chrono::month_weekday_last; using std::chrono::month_day; using std::chrono::month_day_last; using std::chrono::weekday_last; using std::chrono::weekday; using std::chrono::January; using std::chrono::February; using std::chrono::June; using std::chrono::March; using std::chrono::October; using std::chrono::Monday; using std::chrono::Thursday; using std::chrono::Sunday; constexpr auto yearMonthDay{year(1940)/month(6)/day(26)}; // (1) std::cout << yearMonthDay << " "; std::cout << year_month_day(1940y, June, 26d) << '\n'; // (2) std::cout << '\n'; constexpr auto yearMonthDayLast{year(2010)/March/last}; // (3) std::cout << yearMonthDayLast << " "; std::cout << year_month_day_last(2010y, month_day_last(month(3))) << '\n'; constexpr auto yearMonthWeekday{year(2020)/March/Thursday[2]}; // (4) std::cout << yearMonthWeekday << " "; std::cout << year_month_weekday(2020y, month(March), Thursday[2]) << '\n'; constexpr auto yearMonthWeekdayLast{year(2010)/March/Monday[last]}; //(5) std::cout << yearMonthWeekdayLast << " "; std::cout << year_month_weekday_last(2010y, month(March), weekday_last(Monday)); std::cout << "\n\n"; constexpr auto day_{day(19)}; // (6) std::cout << day_ << " "; std::cout << day(19) << '\n'; constexpr auto month_{month(1)}; // (7) std::cout << month_ << " "; std::cout << month(1) << '\n'; constexpr auto year_{year(1988)}; // (8) std::cout << year_ << " "; std::cout << year(1988) << '\n'; constexpr auto weekday_{weekday(5)}; std::cout << weekday_ << " "; std::cout << weekday(5) << '\n'; constexpr auto yearMonth{year(1988)/1}; std::cout << yearMonth << " "; std::cout << year_month(year(1988), January) << '\n'; constexpr auto monthDay{10/day(22)}; std::cout << monthDay << " "; std::cout << month_day(October, day(22)) << '\n'; constexpr auto monthDayLast{June/last}; std::cout << monthDayLast << " "; std::cout << month_day_last(month(6)) << '\n'; constexpr auto monthWeekday{2/Monday[3]}; std::cout << monthWeekday << " "; std::cout << month_weekday(February, Monday[3]) << '\n'; constexpr auto monthWeekDayLast{June/Sunday[last]}; std::cout << monthWeekDayLast << " "; std::cout << month_weekday_last(June, weekday_last(Sunday)) << '\n'; std::cout << '\n'; }
There are two ways to create a calendar date. You can use the so-called cute syntax yearMonthDay{year(1940)/month(6)/day(26)}
(line 1), or you can use the explicit type date::year_month_day(1940y, June, 26d
) (line 2). To avoid overwhelming you, I will delay my explanation of the cute syntax to the next section. The explicit type is interesting because it uses the date-time literals 1940y
, 26d
, and the predefined constant June
. This was the obvious part of the program.
Line 3, line 4, and line 5 offer additional ways to create calendar dates.
- Line 3: the last day of March 2010:
{year(2010)/March/last}
oryear_month_day_last(2010y,month_day_last(month(3)))
- Line 4: the second Thursday of March 2020:
{year(2020)/March/Thursday[2]}
oryear_month_weekday(2020y, month(March), Thursday[2])
- Line 5: the last Monday of March 2010:
{year(2010)/March/Monday[last]}
oryear_month_weekday_last(2010y, month(March), weekday_last(Monday))
The remaining calendar types stand for a day (line 6), a month (line 7), or a year (line 8). You can combine them as basic building blocks for fully specified calendar dates, such as in lines 3, 4, or 5.
Modernes C++ Mentoring
Do you want to stay informed: Subscribe.
This is the output of the program:
As promised, let me write about the cute syntax.
Cute Syntax
The cute syntax consists of overloaded division operators to specify a calendar date. The overloaded operators support time literals (e.g., 2020y
, 31d
) and std::chrono::month
constants such as std::chrono::January
, std::chrono::February
, …, std::chrono::December
.
The following three combinations of year, month, and day are possible using the cute syntax.
year/month/day day/month/year month/day/year
These combinations are not chosen arbitrarily. They are the ones used most worldwide, and no other combination is allowed.
Consequently, when you choose the type year
, month
, or day
for the first argument, the type for the remaining two arguments is no longer necessary, and a number does the job.
// cuteSyntax.cpp #include <chrono> #include <iostream> int main() { std::cout << '\n'; constexpr auto yearMonthDay{std::chrono::year(1966)/6/26}; std::cout << yearMonthDay << '\n'; constexpr auto dayMonthYear{std::chrono::day(26)/6/1966}; std::cout << dayMonthYear << '\n'; constexpr auto monthDayYear{std::chrono::month(6)/26/1966}; std::cout << monthDayYear << '\n'; constexpr auto yearDayMonth{std::chrono::year(1966)/std::chrono::month(26)/6}; std::cout << yearDayMonth << '\n'; std::cout << '\n';
The last values for year/day/month are not allowed and cause a run-time message.
What’s Next
I assume you want to display a calendar date {year(2010)/March/last}
in a readable form, for example, 2020-03-31. This is a job for the local_days
or sys_days
operator.
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 |
Modernes C++ GmbH
Modernes C++ Mentoring (English)
Rainer Grimm
Yalovastraße 20
72108 Rottenburg
Mail: schulung@ModernesCpp.de
Mentoring: www.ModernesCpp.org
Modernes C++ Mentoring,