{"id":6011,"date":"2020-11-01T06:10:48","date_gmt":"2020-11-01T06:10:48","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/calendar-and-time-zone-in-c-20-calendar-dates\/"},"modified":"2023-06-26T09:37:59","modified_gmt":"2023-06-26T09:37:59","slug":"calendar-and-time-zone-in-c-20-calendar-dates","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/calendar-and-time-zone-in-c-20-calendar-dates\/","title":{"rendered":"Calendar and Time-Zones in C++20: Calendar Dates"},"content":{"rendered":"<p>A new type of the chrono extension in C++20 is a calendar date. C++20 offers various ways to create a calendar date and interact with them.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5945\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/TimelineCpp20CoreLanguage.png\" alt=\"TimelineCpp20CoreLanguage\" width=\"650\" height=\"278\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/TimelineCpp20CoreLanguage.png 950w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/TimelineCpp20CoreLanguage-300x128.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/07\/TimelineCpp20CoreLanguage-768x327.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>It took me a while to understand the almost twenty calendar-related data types. For that reason, I start with a calendar date. As in my last post, &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/std-format-in-c-20\/calendar-and-time-zone-in-c-20-calendar-dates\">Calendar and Time-Zones in C++20: Time of Day<\/a> &#8220;, I use the <a href=\"https:\/\/github.com\/HowardHinnant\/date\">date <\/a>library from Howard Hinnant as the prototype of the new <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/chrono\">chrono <\/a>extension.<\/p>\n<p>&nbsp;<\/p>\n<h2>Calendar Date<\/h2>\n<p>A calendar date is a date that consists of a year, a month, and a day. Consequently, C++20 has a specific data type<code> std::chrono::year_month_day<\/code>. C++20 has way more to offer. Both tables present the first overview.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6006\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/calendar1.PNG\" alt=\"calendar1\" width=\"550\" height=\"333\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/calendar1.PNG 1025w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/calendar1-300x182.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/calendar1-768x465.png 768w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6007\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/calendar2.PNG\" alt=\"calendar2\" width=\"550\" height=\"281\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/calendar2.PNG 978w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/calendar2-300x153.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/calendar2-768x391.png 768w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Let&#8217;s start simple. The following program<code> createCalendar.cpp<\/code> shows various ways to create calendar-related dates.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ createCalendar.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include \"date.h\"<\/span>\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span> date;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> yearMonthDay{year(<span style=\"color: #ff6600;\">1940<\/span>)<span style=\"color: #555555;\">\/<\/span>month(<span style=\"color: #ff6600;\">6<\/span>)<span style=\"color: #555555;\">\/<\/span>day(<span style=\"color: #ff6600;\">26<\/span>)};            <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> yearMonthDay <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> date<span style=\"color: #555555;\">::<\/span>year_month_day(<span style=\"color: #ff6600;\">1940<\/span>_y, June, <span style=\"color: #ff6600;\">26<\/span>_d) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> yearMonthDayLast{year(<span style=\"color: #ff6600;\">2010<\/span>)<span style=\"color: #555555;\">\/<\/span>March<span style=\"color: #555555;\">\/<\/span>last};              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> yearMonthDayLast <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> date<span style=\"color: #555555;\">::<\/span>year_month_day_last(<span style=\"color: #ff6600;\">2010<\/span>_y, month_day_last(month(<span style=\"color: #ff6600;\">3<\/span>))) <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> yearMonthWeekday{year(<span style=\"color: #ff6600;\">2020<\/span>)<span style=\"color: #555555;\">\/<\/span>March<span style=\"color: #555555;\">\/<\/span>Thursday[<span style=\"color: #ff6600;\">2<\/span>]};       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> yearMonthWeekday <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> date<span style=\"color: #555555;\">::<\/span>year_month_weekday(<span style=\"color: #ff6600;\">2020<\/span>_y, month(March), Thursday[<span style=\"color: #ff6600;\">2<\/span>]) <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> yearMonthWeekdayLast{year(<span style=\"color: #ff6600;\">2010<\/span>)<span style=\"color: #555555;\">\/<\/span>March<span style=\"color: #555555;\">\/<\/span>Monday[last]};  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> yearMonthWeekdayLast <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> date<span style=\"color: #555555;\">::<\/span>year_month_weekday_last(<span style=\"color: #ff6600;\">2010<\/span>_y, month(March), weekday_last(Monday)) <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> day_{day(<span style=\"color: #ff6600;\">19<\/span>)};          <span style=\"color: #0099ff; font-style: italic;\">\/\/ (5)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> day_  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> date<span style=\"color: #555555;\">::<\/span>day(<span style=\"color: #ff6600;\">19<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> month_{month(<span style=\"color: #ff6600;\">1<\/span>)};       <span style=\"color: #0099ff; font-style: italic;\">\/\/ (6)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> month_  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> date<span style=\"color: #555555;\">::<\/span>month(<span style=\"color: #ff6600;\">1<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> year_{year(<span style=\"color: #ff6600;\">1988<\/span>)};      <span style=\"color: #0099ff; font-style: italic;\">\/\/ (7)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> year_  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> date<span style=\"color: #555555;\">::<\/span>year(<span style=\"color: #ff6600;\">1988<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> weekday_{weekday(<span style=\"color: #ff6600;\">5<\/span>)};\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> weekday_  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> date<span style=\"color: #555555;\">::<\/span>weekday(<span style=\"color: #ff6600;\">5<\/span>) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n \r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> yearMonth{year(<span style=\"color: #ff6600;\">1988<\/span>)<span style=\"color: #555555;\">\/<\/span><span style=\"color: #ff6600;\">1<\/span>};\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> yearMonth  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> date<span style=\"color: #555555;\">::<\/span>year_month(year(<span style=\"color: #ff6600;\">1988<\/span>), January) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n \r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> monthDay{<span style=\"color: #ff6600;\">10<\/span><span style=\"color: #555555;\">\/<\/span>day(<span style=\"color: #ff6600;\">22<\/span>)};\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> monthDay <span style=\"color: #555555;\">&lt;&lt;<\/span>  <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> date<span style=\"color: #555555;\">::<\/span>month_day(October, day(<span style=\"color: #ff6600;\">22<\/span>)) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> monthDayLast{June<span style=\"color: #555555;\">\/<\/span>last};\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> monthDayLast <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> date<span style=\"color: #555555;\">::<\/span>month_day_last(month(<span style=\"color: #ff6600;\">6<\/span>)) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n \r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> monthWeekday{<span style=\"color: #ff6600;\">2<\/span><span style=\"color: #555555;\">\/<\/span>Monday[<span style=\"color: #ff6600;\">3<\/span>]};\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> monthWeekday <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> date<span style=\"color: #555555;\">::<\/span>month_weekday(February, Monday[<span style=\"color: #ff6600;\">3<\/span>]) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n \r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> monthWeekDayLast{June<span style=\"color: #555555;\">\/<\/span>Sunday[last]};\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> monthWeekDayLast <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\" \"<\/span>;\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> date<span style=\"color: #555555;\">::<\/span>month_weekday_last(June, weekday_last(Sunday)) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>There are essentially two ways to create a calendar date. You can use the so-called cute syntax<span style=\"color: #000000;\"><code> yearMonthDay{year(1940)\/month(6)\/day(26)}<\/code> (line 1), or you can use the explicit type&nbsp;<code>date::year_month_day(1940_y, June, 26_d)<\/code> (line 2). To not overwhelm you, I am delaying my explanation of the cute syntax to the next section of this post. The explicit type is quite interesting because it uses the <a href=\"https:\/\/github.com\/HowardHinnant\/date\">date <\/a>time literals 1940_y, 26_d, and the predefined constant <code>June<\/code>. With C++20, the date literals <code>1940_y<\/code> and<code> 26_d<\/code> are written without an underscore: <code>1940y<\/code>&nbsp; and <code>26d<\/code>. This was the prominent part. <br \/><\/span><\/p>\n<p>Line (3), line (4), and line (5) offer convenient ways to create calendar dates.<\/p>\n<ul>\n<li>Line (3): the last day of March 2010: <code>{year(2010)\/March\/last}<\/code> or <code>year_month_day_last(2010_y, month_day_last(month(3))<\/code><\/li>\n<li>Line (4): the second Thursday of March 2020: <code>{year(2020)\/March\/Thursday[2]}<\/code> or <code>year_month_weekday(2020_y, month(March), Thursday[2])<\/code><\/li>\n<li>Line (5): the last Monday of March 2010: {year(2010)\/March\/Monday[last]} or <code>year_month_weekday_last(2010_y, month(March), weekday_last(Monday))<\/code><\/li>\n<\/ul>\n<p>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 lines (3) to (4).<\/p>\n<p><span style=\"color: #000000;\">Before I dive more into the details, here is the program&#8217;s output.<br \/><\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6008\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/calendarDate.png\" alt=\"calendarDate\" width=\"500\" height=\"459\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/calendarDate.png 611w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/calendarDate-300x275.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>As promised, let me write about the cute syntax.<\/p>\n<h3>Cute Syntax<\/h3>\n<p>The cute syntax consists of overloaded division operators to specify a calendar date. The overloaded operators support time literals (e.g.: <code>2020_y, 31_d<\/code>) and constants (<code>January, February, March, April, May, June, July, August, September, October, November, December<\/code>).<\/p>\n<p>The following three combinations of year, month, and day are possible using the cute syntax.<\/p>\n<ol>\n<li>year\/month\/day<\/li>\n<li>day\/month\/year<\/li>\n<li>month\/day\/year<\/li>\n<\/ol>\n<p>These combinations are not arbitrarily chosen because they are the used ones worldwide. Each other combination is not allowed<\/p>\n<p>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 an integral would do the job.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ cuteSyntax.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include \"date.h\"<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span> date;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> yearMonthDay{year(<span style=\"color: #ff6600;\">1966<\/span>)<span style=\"color: #555555;\">\/<\/span><span style=\"color: #ff6600;\">6<\/span><span style=\"color: #555555;\">\/<\/span><span style=\"color: #ff6600;\">26<\/span>};\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> yearMonthDay <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> dayMonthYear{day(<span style=\"color: #ff6600;\">26<\/span>)<span style=\"color: #555555;\">\/<\/span><span style=\"color: #ff6600;\">6<\/span><span style=\"color: #555555;\">\/<\/span><span style=\"color: #ff6600;\">1966<\/span>};\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> dayMonthYear <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> monthDayYear{month(<span style=\"color: #ff6600;\">6<\/span>)<span style=\"color: #555555;\">\/<\/span><span style=\"color: #ff6600;\">26<\/span><span style=\"color: #555555;\">\/<\/span><span style=\"color: #ff6600;\">1966<\/span>};\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> monthDayYear <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> yearDayMonth{year(<span style=\"color: #ff6600;\">1966<\/span>)<span style=\"color: #555555;\">\/<\/span>month(<span style=\"color: #ff6600;\">26<\/span>)<span style=\"color: #555555;\">\/<\/span><span style=\"color: #ff6600;\">6<\/span>};  <span style=\"color: #0099ff; font-style: italic;\">\/\/(1)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> yearDayMonth <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The combination year\/day\/month (line 1) is not allowed and causes a run-time message.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6009\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/cuteSyntax.png\" alt=\"cuteSyntax\" width=\"400\" height=\"259\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/cuteSyntax.png 517w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/cuteSyntax-300x194.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/>I assume you want to display a calendar date<span style=\"color: #000000;\"> <code>{year(2010)\/March\/last}<\/code> <\/span>in a readable form, such as 2020-03-31. This is a job for the <code><\/code><code>local_days<\/code> or <code>sys_days<\/code> operator.<\/p>\n<h3>Displaying Calendar Dates<\/h3>\n<p>Thanks to <code>std::chrono::local_days<\/code> or <code>std::chrono::sys_days<\/code>, you can convert calendar dates to a <code>std::chrono::time_point<\/code> representing the same date as this<code> year_month_day.&nbsp;<\/code>I use <code>std::chrono::sys_days<\/code> in my example.<code> std::chrono::sys_days<\/code> is based on <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/chrono\/system_clock\"><code>std::chrono::system_clock<\/code><\/a>. Let me convert the calendar dates (line (3) &#8211; line (5) from the previous program<code> createCalendar.cpp.<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ sysDays.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include \"date.h\"<\/span>\r\n \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n    \r\n    <span style=\"color: #006699; font-weight: bold;\">using<\/span> <span style=\"color: #006699; font-weight: bold;\">namespace<\/span> date;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> yearMonthDayLast{year(<span style=\"color: #ff6600;\">2010<\/span>)<span style=\"color: #555555;\">\/<\/span>March<span style=\"color: #555555;\">\/<\/span>last};\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sys_days(yearMonthDayLast): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sys_days(yearMonthDayLast)  <span style=\"color: #555555;\">&lt;&lt;<\/span>  std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> yearMonthWeekday{year(<span style=\"color: #ff6600;\">2020<\/span>)<span style=\"color: #555555;\">\/<\/span>March<span style=\"color: #555555;\">\/<\/span>Thursday[<span style=\"color: #ff6600;\">2<\/span>]};\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sys_days(yearMonthWeekday): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span>  sys_days(yearMonthWeekday) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> yearMonthWeekdayLast{year(<span style=\"color: #ff6600;\">2010<\/span>)<span style=\"color: #555555;\">\/<\/span>March<span style=\"color: #555555;\">\/<\/span>Monday[last]};\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sys_days(yearMonthWeekdayLast): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sys_days(yearMonthWeekdayLast) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> leapDate{year(<span style=\"color: #ff6600;\">2012<\/span>)<span style=\"color: #555555;\">\/<\/span>February<span style=\"color: #555555;\">\/<\/span>last};                <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sys_days(leapDate): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sys_days(leapDate) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    constexpr <span style=\"color: #006699; font-weight: bold;\">auto<\/span> noLeapDate{year(<span style=\"color: #ff6600;\">2013<\/span>)<span style=\"color: #555555;\">\/<\/span>February<span style=\"color: #555555;\">\/<\/span>last};              <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"sys_day(noLeapDate): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> sys_days(noLeapDate) <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n}   \r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The <code>std::chrono::last<\/code> constant let me quickly determine how many days a month has. Consequently, the output shows that 2012 is a leap year but not 2013.<\/p>\n<h2><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6010\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/sysDays.png\" alt=\"sysDays\" width=\"550\" height=\"334\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/sysDays.png 631w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2020\/11\/sysDays-300x182.png 300w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/h2>\n<h2>What&#8217;s next?<\/h2>\n<p>Working with calendar dates becomes powerful when you check if a calendar date is valid or when you add a time duration.<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A new type of the chrono extension in C++20 is a calendar date. C++20 offers various ways to create a calendar date and interact with them.<\/p>\n","protected":false},"author":21,"featured_media":5945,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[453],"class_list":["post-6011","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-20","tag-time"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6011","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6011"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6011\/revisions"}],"predecessor-version":[{"id":6724,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6011\/revisions\/6724"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5945"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}