{"id":9448,"date":"2024-04-22T09:53:34","date_gmt":"2024-04-22T09:53:34","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=9448"},"modified":"2024-04-22T09:53:34","modified_gmt":"2024-04-22T09:53:34","slug":"c20-query-calendar-dates-and-ordinal-dates","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c20-query-calendar-dates-and-ordinal-dates\/","title":{"rendered":"C++20: Query Calendar Dates and Ordinal Dates"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The extended chrono library makes it relatively easy to ask for the time duration between calendar dates.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"411\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage.png\" alt=\"\" class=\"wp-image-9278\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage.png 960w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage-300x128.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage-768x329.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage-705x302.png 705w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">This post is the sixth in my detailed journey through the chrono extension in C++20:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/c20-basic-chrono-terminology\/\">Basic Chrono Terminology<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/c20-basic-chrono-terminology-with-time-duration-and-time-point\/\">Basic Chrono Terminology with Time Duration and Time Point<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/c20-time-of-day\/\">Time of Day: Details<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/c20-creating-calendar-dates\/\">C++20: Creating Calendar Dates<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/c20-displaying-and-checking-calendar-dates\/\" data-type=\"link\" data-id=\"https:\/\/www.modernescpp.com\/index.php\/c20-displaying-and-checking-calendar-dates\/\">C++20: Displaying and Checking Calendar Dates<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Query Calendar Dates<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Without further ado, the following program <code>queryCalendarDates.cpp <\/code>queries a few calendar dates.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ queryCalendarDates.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;chrono&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>floor;\n\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>January;\n\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>years;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>days;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>hours;\n\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year_month_day;\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year_month_weekday;\n\n    <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>sys_days;\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> now <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>system_clock<span style=\"color: #555555\">::<\/span>now();              \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;The current time is: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> now <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; UTC<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;                     <span style=\"color: #0099FF; font-style: italic\">\/\/ (1)   <\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;The current date is: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> floor<span style=\"color: #555555\">&lt;<\/span>days<span style=\"color: #555555\">&gt;<\/span>(now) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;The current date is: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> year_month_day{floor<span style=\"color: #555555\">&lt;<\/span>days<span style=\"color: #555555\">&gt;<\/span>(now)} \n              <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;The current date is: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> year_month_weekday{floor<span style=\"color: #555555\">&lt;<\/span>days<span style=\"color: #555555\">&gt;<\/span>(now)} \n              <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    \n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> currentDate <span style=\"color: #555555\">=<\/span> year_month_day(floor<span style=\"color: #555555\">&lt;<\/span>days<span style=\"color: #555555\">&gt;<\/span>(now));                         <span style=\"color: #0099FF; font-style: italic\">\/\/ (2)<\/span>\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> currentYear <span style=\"color: #555555\">=<\/span> currentDate.year();\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;The current year is &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> currentYear <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;    \n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> currentMonth <span style=\"color: #555555\">=<\/span> currentDate.month();\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;The current month is &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> currentMonth <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>; \n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> currentDay <span style=\"color: #555555\">=<\/span> currentDate.day();\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;The current day is &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> currentDay <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>; \n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n                                                               \n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> hAfter <span style=\"color: #555555\">=<\/span> floor<span style=\"color: #555555\">&lt;<\/span>hours<span style=\"color: #555555\">&gt;<\/span>(now) <span style=\"color: #555555\">-<\/span> sys_days(January<span style=\"color: #555555\">\/<\/span><span style=\"color: #FF6600\">1<\/span><span style=\"color: #555555\">\/<\/span>currentYear);           <span style=\"color: #0099FF; font-style: italic\">\/\/ (3)<\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;It has been &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> hAfter <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; since New Year!<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;  \n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> nextYear <span style=\"color: #555555\">=<\/span> currentDate.year() <span style=\"color: #555555\">+<\/span> years(<span style=\"color: #FF6600\">1<\/span>);             \n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> nextNewYear <span style=\"color: #555555\">=<\/span> sys_days(January<span style=\"color: #555555\">\/<\/span><span style=\"color: #FF6600\">1<\/span><span style=\"color: #555555\">\/<\/span>nextYear);\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> hBefore <span style=\"color: #555555\">=<\/span>  sys_days(January<span style=\"color: #555555\">\/<\/span><span style=\"color: #FF6600\">1<\/span><span style=\"color: #555555\">\/<\/span>nextYear) <span style=\"color: #555555\">-<\/span> floor<span style=\"color: #555555\">&lt;<\/span>hours<span style=\"color: #555555\">&gt;<\/span>(now);            <span style=\"color: #0099FF; font-style: italic\">\/\/ (4)<\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;It is &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> hBefore <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; before New Year!<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n                                                            \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;It has been &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> floor<span style=\"color: #555555\">&lt;<\/span>days<span style=\"color: #555555\">&gt;<\/span>(hAfter) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; since New Year!<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;  <span style=\"color: #0099FF; font-style: italic\">\/\/ (5)<\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;It is &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> floor<span style=\"color: #555555\">&lt;<\/span>days<span style=\"color: #555555\">&gt;<\/span>(hBefore) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot; before New Year!<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;      <span style=\"color: #0099FF; font-style: italic\">\/\/ (6)<\/span>\n    \n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    \n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">With the C++20 extension, you can directly display a time point, such as <code>now<\/code> (line 1). <code>std::chrono::floor<\/code> rounds the time point down to a day <code>std::chrono::sys_days<\/code>. This value can be used to initialize the calendar type <code>std::chrono::year_month_day<\/code>. Finally, when I put the value into a <code>std::chrono::year_month_weekday<\/code> calendar type, I get the answer that this specific day is the 3rd Tuesday in October.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Of course, I can also ask for a calendar date for its components, such as the current year, month, or day (line 2).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Line 3 is the most interesting one. When I subtract from the current date, using hour resolution, the first of January of the current year, I get the number of hours since the new year. Conversely, when I subtract from the first of January of the next year (line 4) the current date, I get the hours to the new year using hour resolution. Maybe you don&#8217;t like hour resolution. Lines 5 and 6 display the values using day resolution.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"771\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/queryCalendarDates-1030x771.png\" alt=\"\" class=\"wp-image-9455\" style=\"width:500px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/queryCalendarDates-1030x771.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/queryCalendarDates-300x224.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/queryCalendarDates-768x575.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/queryCalendarDates-705x527.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/queryCalendarDates.png 1441w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Now, I want to know the weekdays of my birthdays.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Query Weekdays<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks to the extended chrono library, getting the weekday of a given calendar date is pretty easy.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ weekdaysOfBirthdays.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;chrono&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;cstdlib&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #007788; font-weight: bold\">int<\/span> y;\n    <span style=\"color: #007788; font-weight: bold\">int<\/span> m;\n    <span style=\"color: #007788; font-weight: bold\">int<\/span> d;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Year: &quot;<\/span>;                                            <span style=\"color: #0099FF; font-style: italic\">\/\/ (1)                          <\/span>\n    std<span style=\"color: #555555\">::<\/span>cin <span style=\"color: #555555\">&gt;&gt;<\/span> y;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Month: &quot;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cin <span style=\"color: #555555\">&gt;&gt;<\/span> m;\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Day: &quot;<\/span>;\n    std<span style=\"color: #555555\">::<\/span>cin <span style=\"color: #555555\">&gt;&gt;<\/span> d;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> birthday <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year(y)<span style=\"color: #555555\">\/<\/span>std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>month(m)<span style=\"color: #555555\">\/<\/span>std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>day(d);  <span style=\"color: #0099FF; font-style: italic\">\/\/ (2)      <\/span>\n\n    <span style=\"color: #006699; font-weight: bold\">if<\/span> (not birthday.ok()) {                                         <span style=\"color: #0099FF; font-style: italic\">\/\/ (3)                     <\/span>\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> birthday <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n        std<span style=\"color: #555555\">::<\/span>exit(EXIT_FAILURE);\n    }\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Birthday: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> birthday <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> birthdayWeekday <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year_month_weekday(birthday); <span style=\"color: #0099FF; font-style: italic\">\/\/ (4)  <\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Weekday of birthday: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> birthdayWeekday.weekday() <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> currentDate <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year_month_day(\n        std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>floor<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>days<span style=\"color: #555555\">&gt;<\/span>(std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>system_clock<span style=\"color: #555555\">::<\/span>now()));  \n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> currentYear <span style=\"color: #555555\">=<\/span> currentDate.year();\n    \n    <span style=\"color: #006699; font-weight: bold\">auto<\/span> age <span style=\"color: #555555\">=<\/span> <span style=\"color: #006699; font-weight: bold\">static_cast<\/span><span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">&gt;<\/span>(currentDate.year()) <span style=\"color: #555555\">-<\/span> \n               <span style=\"color: #006699; font-weight: bold\">static_cast<\/span><span style=\"color: #555555\">&lt;<\/span><span style=\"color: #007788; font-weight: bold\">int<\/span><span style=\"color: #555555\">&gt;<\/span>(birthday.year());                     <span style=\"color: #0099FF; font-style: italic\">\/\/ (5)<\/span>\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Your age: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> age <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Weekdays for your next 10 birthdays&quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;  \n\n    <span style=\"color: #006699; font-weight: bold\">for<\/span> (<span style=\"color: #007788; font-weight: bold\">int<\/span> i <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">1<\/span>, newYear <span style=\"color: #555555\">=<\/span> (<span style=\"color: #007788; font-weight: bold\">int<\/span>)currentYear; i <span style=\"color: #555555\">&lt;=<\/span> <span style=\"color: #FF6600\">10<\/span>;  <span style=\"color: #555555\">++<\/span>i ) {    <span style=\"color: #0099FF; font-style: italic\">\/\/ (6)<\/span>\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;  Age &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span>  <span style=\"color: #555555\">++<\/span>age <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n        <span style=\"color: #006699; font-weight: bold\">auto<\/span> newBirthday <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year(<span style=\"color: #555555\">++<\/span>newYear)<span style=\"color: #555555\">\/<\/span>\n                           std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>month(m)<span style=\"color: #555555\">\/<\/span>std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>day(d);\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;    Birthday: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> newBirthday <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n        std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;    Weekday of birthday: &quot;<\/span> \n                  <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year_month_weekday(newBirthday).weekday() <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n    }\n\n    std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;\n\n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">First, the program asks you for your birthday&#8217;s year, month, and day (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 the calendar date to fill in the calendar type <code>std::chrono::year_month_weekday<\/code> (line 4). To get the <code>int<\/code> representation of the calendar type <code>year<\/code>, I must convert it to <code>int<\/code> (line 5). Now I can display your age. Finally, for each of your next ten birthdays (line 6), the for loop shows the following information: your age, the calendar date, and the weekday. I only have to increment the <code>age<\/code> and <code>newYear<\/code> variables.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is a run of the program with my birthday.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"620\" height=\"1030\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/weekdaysOfBirthdays-620x1030.png\" alt=\"\" class=\"wp-image-9463\" style=\"width:500px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/weekdaysOfBirthdays-620x1030.png 620w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/weekdaysOfBirthdays-180x300.png 180w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/weekdaysOfBirthdays-768x1277.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/weekdaysOfBirthdays-924x1536.png 924w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/weekdaysOfBirthdays-1232x2048.png 1232w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/weekdaysOfBirthdays-902x1500.png 902w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/weekdaysOfBirthdays-424x705.png 424w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/weekdaysOfBirthdays.png 1239w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Calculating Ordinal Dates<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As a last example of the new calendar facility, I want to present the online resource <a href=\"https:\/\/github.com\/HowardHinnant\/date\/wiki\/Examples-and-Recipes\">Examples and Recipes<\/a> 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&#8217;s 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To get an idea of Examples and Recipes, I want to present a slightly modified program by <a href=\"https:\/\/github.com\/rbock\" data-type=\"link\" data-id=\"https:\/\/github.com\/rbock\">Roland Bock<\/a> that calculates ordinal dates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&#8220;<em>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<\/em>:&#8221; (Roland Bock)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I added the necessary headers to Roland&#8217;s program.<\/p>\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #f0f3f3; overflow:auto;width:auto;gray;border-width:.1em .1em .1em .8em\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0099FF; font-style: italic\">\/\/ ordinalDate.cpp<\/span>\n\n<span style=\"color: #009999\">#include &quot;date.h&quot;<\/span>\n<span style=\"color: #009999\">#include &lt;iomanip&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>()\n{\n   <span style=\"color: #006699; font-weight: bold\">using<\/span> <span style=\"color: #006699; font-weight: bold\">namespace<\/span> date;\n   \n   <span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #006699; font-weight: bold\">auto<\/span> time <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>system_clock<span style=\"color: #555555\">::<\/span>now();\n   <span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #006699; font-weight: bold\">auto<\/span> daypoint <span style=\"color: #555555\">=<\/span> floor<span style=\"color: #555555\">&lt;<\/span>days<span style=\"color: #555555\">&gt;<\/span>(time);                    <span style=\"color: #0099FF; font-style: italic\">\/\/ (1) <\/span>\n   <span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #006699; font-weight: bold\">auto<\/span> ymd <span style=\"color: #555555\">=<\/span> year_month_day{daypoint};         \n   \n   <span style=\"color: #0099FF; font-style: italic\">\/\/ calculating the year and the day of the year<\/span>\n   <span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #006699; font-weight: bold\">auto<\/span> year <span style=\"color: #555555\">=<\/span> ymd.year();\n   <span style=\"color: #006699; font-weight: bold\">const<\/span> <span style=\"color: #006699; font-weight: bold\">auto<\/span> year_day <span style=\"color: #555555\">=<\/span> daypoint <span style=\"color: #555555\">-<\/span> sys_days{year<span style=\"color: #555555\">\/<\/span>January<span style=\"color: #555555\">\/<\/span><span style=\"color: #FF6600\">0<\/span>}; <span style=\"color: #0099FF; font-style: italic\">\/\/ (2) <\/span>\n                                                              <span style=\"color: #0099FF; font-style: italic\">\/\/ (3)<\/span>\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\">&#39;-&#39;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>setfill(<span style=\"color: #CC3300\">&#39;0&#39;<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>setw(<span style=\"color: #FF6600\">3<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> year_day.count() <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>endl;\n   \n   <span style=\"color: #0099FF; font-style: italic\">\/\/ inverse calculation and check<\/span>\n   assert(ymd <span style=\"color: #555555\">==<\/span> year_month_day{sys_days{year<span style=\"color: #555555\">\/<\/span>January<span style=\"color: #555555\">\/<\/span><span style=\"color: #FF6600\">0<\/span>} <span style=\"color: #555555\">+<\/span> year_day});\n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">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, <code>year_day.count()<\/code> inline (3) returns the time duration in days.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"794\" height=\"233\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/ordinalDate.png\" alt=\"\" class=\"wp-image-9469\" style=\"width:700px;height:auto\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/ordinalDate.png 794w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/ordinalDate-300x88.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/ordinalDate-768x225.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/04\/ordinalDate-705x207.png 705w\" sizes=\"auto, (max-width: 794px) 100vw, 794px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A key component in my posts to the extended chrono library in C++20 is still missing: time zones.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The extended chrono library makes it relatively easy to ask for the time duration between calendar dates. This post is the sixth in my detailed journey through the chrono extension in C++20: Query Calendar Dates Without further ado, the following program queryCalendarDates.cpp queries a few calendar dates. \/\/ queryCalendarDates.cpp #include &lt;chrono&gt; #include &lt;iostream&gt; int main() [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":9278,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[453],"class_list":["post-9448","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\/9448","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=9448"}],"version-history":[{"count":22,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9448\/revisions"}],"predecessor-version":[{"id":9479,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9448\/revisions\/9479"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/9278"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=9448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=9448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=9448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}