{"id":9315,"date":"2024-03-25T10:08:58","date_gmt":"2024-03-25T10:08:58","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=9315"},"modified":"2024-03-25T10:08:59","modified_gmt":"2024-03-25T10:08:59","slug":"c20-time-of-day","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c20-time-of-day\/","title":{"rendered":"C++20: Time of Day"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This new data type <code>std::chrono::hh_mm_ss<\/code> in C++20 stands for the time of day.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"411\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage-1.png\" alt=\"\" class=\"wp-image-9316\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage-1.png 960w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage-1-300x128.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage-1-768x329.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimelineCpp20CoreLanguage-1-705x302.png 705w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Time of Day<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>std::chrono::hh_mm_ss<\/code> is the duration since midnight split into hours, minutes, seconds, and fractional seconds. This type is typically used as a formatting tool. First, the following table gives you a concise overview of <code>std::chrono::hh_mm_ss<\/code> instance<code> tOfDay.<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"881\" height=\"604\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/tmeOfDay.png\" alt=\"\" class=\"wp-image-9362\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/tmeOfDay.png 881w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/tmeOfDay-300x206.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/tmeOfDay-768x527.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/tmeOfDay-705x483.png 705w\" sizes=\"auto, (max-width: 881px) 100vw, 881px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Depending on the used time duration, the static member provides the appropriate <code>tOfDay<\/code>.<code>fractional_width<\/code>. If no such value of <code>fractional_width<\/code> in the range <code>[0, 18]<\/code> exists, then <code>fractional_width<\/code> is <code>6<\/code>. See for example, <code>std::chrono::duration&lt;int, std::ratio&lt;1, 3&gt;&gt;<\/code> in the following table.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"840\" height=\"408\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/fractional.png\" alt=\"\" class=\"wp-image-9324\" style=\"width:650px;height:auto\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/fractional.png 840w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/fractional-300x146.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/fractional-768x373.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/fractional-705x342.png 705w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The use of the chrono type <code>std::chrono::hh_mm_ss<\/code> is straightforward.<\/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\">\/\/ timeOfDay.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     <span style=\"color: #006699; font-weight: bold\">using<\/span> <span style=\"color: #006699; font-weight: bold\">namespace<\/span> std<span style=\"color: #555555\">::<\/span>chrono_literals; \n\n     std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>boolalpha <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> timeOfDay <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>hh_mm_ss(<span style=\"color: #FF6600\">10.5<\/span>h <span style=\"color: #555555\">+<\/span> <span style=\"color: #FF6600\">98<\/span>min <span style=\"color: #555555\">+<\/span> <span style=\"color: #FF6600\">2020<\/span>s <span style=\"color: #555555\">+<\/span> <span style=\"color: #FF6600\">0.5<\/span>s);         <span style=\"color: #0099FF; font-style: italic\">\/\/ (1)<\/span>\n    \n     std<span style=\"color: #555555\">::<\/span>cout<span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;timeOfDay: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> timeOfDay <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;                               <span style=\"color: #0099FF; font-style: italic\">\/\/ (2)<\/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;timeOfDay.hours(): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> timeOfDay.hours() <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;              <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;timeOfDay.minutes(): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> timeOfDay.minutes() <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;          <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;timeOfDay.seconds(): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> timeOfDay.seconds() <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;          <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;timeOfDay.subseconds(): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> timeOfDay.subseconds() <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;    <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;timeOfDay.to_duration(): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> timeOfDay.to_duration() <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;  <span style=\"color: #0099FF; font-style: italic\">\/\/ (5)<\/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;std::chrono::hh_mm_ss(45700.5s): &quot;<\/span> \n               <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>hh_mm_ss(<span style=\"color: #FF6600\">45700.5<\/span>s) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/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     std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::is_am(5h): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>is_am(<span style=\"color: #FF6600\">5<\/span>h) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;     <span style=\"color: #0099FF; font-style: italic\">\/\/ (7)    <\/span>\n     std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::is_am(15h): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>is_am(<span style=\"color: #FF6600\">15<\/span>h) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;   <span style=\"color: #0099FF; font-style: italic\">\/\/ (7)<\/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;std::chrono::make12(5h): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>make12(<span style=\"color: #FF6600\">5<\/span>h) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;   <span style=\"color: #0099FF; font-style: italic\">\/\/ (7)   <\/span>\n     std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;std::chrono::make12(15h): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>make12(<span style=\"color: #FF6600\">15<\/span>h) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>; <span style=\"color: #0099FF; font-style: italic\">\/\/ (7) <\/span>\n\n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In line (1), I create a new instance of <code>std::chrono::hh_mm_ss: timeOfDay<\/code>. Thanks to the chrono literals from C++14, I can add a few time durations to initialize a time-of-day object. With C++20, you can directly output <code>timeOfDay<\/code> (line 2). The rest should be straightforward to read. Lines (4) display the components of the time since midnight in hours, minutes, seconds, and fractional seconds. Line (5) returns the time duration since midnight in seconds. Line (6) is more interesting: the given seconds correspond to the time displayed in line (2). Line (7) returns if the given hour is a.m. Line (8) finally returns the 12-hour equivalent of the given hour.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the output of the program:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"564\" height=\"504\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/timeOfDayCompiler.png\" alt=\"\" class=\"wp-image-9330\" style=\"width:450px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/timeOfDayCompiler.png 564w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/timeOfDayCompiler-300x268.png 300w\" sizes=\"auto, (max-width: 564px) 100vw, 564px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Calendar Date<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A new type of the chrono extension in C++20 is a calendar date. C++20 supports various ways to create a calendar date and interact with them. First of all: What is a calendar date?<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>&nbsp;A <strong>calendar date<\/strong> 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. The following table should give you an overview of calendar types before I show you various use cases.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"601\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/CalendarDate-1030x601.png\" alt=\"\" class=\"wp-image-9334\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/CalendarDate-1030x601.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/CalendarDate-300x175.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/CalendarDate-768x448.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/CalendarDate-705x411.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/CalendarDate.png 1088w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks to the cute syntax, you can use <code>&nbsp;std::chrono::operator \/<\/code> to create Gregorian calendar dates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The calendar data types support various operations. The following table gives an overview. For readability reasons, I partially ignore the namespace <code>std::chrono<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1028\" height=\"538\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/calendar.png\" alt=\"\" class=\"wp-image-9337\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/calendar.png 1028w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/calendar-300x157.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/calendar-768x402.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/calendar-705x369.png 705w\" sizes=\"auto, (max-width: 1028px) 100vw, 1028px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The increment and decrement operations <code>++\/--<\/code> are supported in the prefix and postfix version. Adding or subtraction <code>+\/-<\/code> requires objects of type<code> std::chrono::duration<\/code>. That means when you build the difference of two objects of calendar type <code>std::chrono::day<\/code> you get an object of type <code>std::chrono::days. &lt;=&gt;<\/code> is the new three-way comparison operator.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following program uses the operations on the calendar types.<\/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\">\/\/ calendarOperations.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>Monday;\n  <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>Saturday;\n\n  <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>March;\n  <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>June;\n  <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>July;\n\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>months;\n  <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>years;\n\n  <span style=\"color: #006699; font-weight: bold\">using<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>last;\n\n  <span style=\"color: #006699; font-weight: bold\">using<\/span> <span style=\"color: #006699; font-weight: bold\">namespace<\/span> std<span style=\"color: #555555\">::<\/span>chrono_literals;\n\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>boolalpha;\n\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;March: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> March <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;March + months(3): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> March <span style=\"color: #555555\">+<\/span> months(<span style=\"color: #FF6600\">3<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/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;March - months(25): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> March <span style=\"color: #555555\">-<\/span> months(<span style=\"color: #FF6600\">25<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/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;July - June: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span>  July <span style=\"color: #555555\">-<\/span> June <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;                                 <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;June &lt; July: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> (June <span style=\"color: #555555\">&lt;<\/span> July) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;                                <span style=\"color: #0099FF; font-style: italic\">\/\/ (7)<\/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;Saturday: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> Saturday <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;Saturday + days(3): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> Saturday <span style=\"color: #555555\">+<\/span> days(<span style=\"color: #FF6600\">3<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;                    <span style=\"color: #0099FF; font-style: italic\">\/\/ (2)<\/span>\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Saturday - days(22): &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> Saturday <span style=\"color: #555555\">-<\/span> days(<span style=\"color: #FF6600\">22<\/span>) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;                  <span style=\"color: #0099FF; font-style: italic\">\/\/ (8)<\/span>\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Saturday - Monday: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span>  Saturday <span style=\"color: #555555\">-<\/span> Monday <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;                     <span style=\"color: #0099FF; font-style: italic\">\/\/ (9)<\/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;2021y\/March: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #FF6600\">2021<\/span>y<span style=\"color: #555555\">\/<\/span>March <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;                                  <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;2021y\/March + years(3) - months(35): &quot;<\/span>                                  <span style=\"color: #0099FF; font-style: italic\">\/\/ (10)<\/span>\n            <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #FF6600\">2021<\/span>y<span style=\"color: #555555\">\/<\/span>March <span style=\"color: #555555\">+<\/span> years(<span style=\"color: #FF6600\">3<\/span>) <span style=\"color: #555555\">-<\/span> months(<span style=\"color: #FF6600\">35<\/span>) <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;2022y\/July - 2021y\/June: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #FF6600\">2022<\/span>y<span style=\"color: #555555\">\/<\/span>July <span style=\"color: #555555\">-<\/span> <span style=\"color: #FF6600\">2021<\/span>y<span style=\"color: #555555\">\/<\/span>June <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;         <span style=\"color: #0099FF; font-style: italic\">\/\/ (11)<\/span>\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;2021y\/June &gt; 2021y\/July: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> (<span style=\"color: #FF6600\">2021<\/span>y<span style=\"color: #555555\">\/<\/span>June <span style=\"color: #555555\">&gt;<\/span> <span style=\"color: #FF6600\">2021<\/span>y<span style=\"color: #555555\">\/<\/span>July) <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;       <span style=\"color: #0099FF; font-style: italic\">\/\/ (12)<\/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;2021y\/March\/Saturday[last]: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #FF6600\">2021<\/span>y<span style=\"color: #555555\">\/<\/span>March<span style=\"color: #555555\">\/<\/span>Saturday[last] <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&#39;\\n&#39;<\/span>;   <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;2021y\/March\/Saturday[last] + months(13) + years(3): &quot;<\/span> \n            <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #FF6600\">2021<\/span>y<span style=\"color: #555555\">\/<\/span>March<span style=\"color: #555555\">\/<\/span>Saturday[last] <span style=\"color: #555555\">+<\/span> months(<span style=\"color: #FF6600\">13<\/span>) <span style=\"color: #555555\">+<\/span> years(<span style=\"color: #FF6600\">3<\/span>) <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;2021y\/July\/Saturday[last] - months(1) == 2021y\/June\/Saturday[last]: &quot;<\/span>\n            <span style=\"color: #555555\">&lt;&lt;<\/span> (<span style=\"color: #FF6600\">2021<\/span>y<span style=\"color: #555555\">\/<\/span>July<span style=\"color: #555555\">\/<\/span>Saturday[last] <span style=\"color: #555555\">-<\/span> months(<span style=\"color: #FF6600\">1<\/span>) <span style=\"color: #555555\">==<\/span> <span style=\"color: #FF6600\">2021<\/span>y<span style=\"color: #555555\">\/<\/span>June<span style=\"color: #555555\">\/<\/span>Saturday[last]) \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<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The program performs operations on <code>std::chrono::month<\/code> (line 1), <code>std::chrono::weekday<\/code> (line 2), <code>std::chrono::year_month <\/code>(line 3), and<code> std::chrono::year_month_weekday_last<\/code> (line 4).<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"698\" height=\"493\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/calendarOperations.png\" alt=\"\" class=\"wp-image-9342\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/calendarOperations.png 698w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/calendarOperations-300x212.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/calendarOperations-260x185.png 260w\" sizes=\"auto, (max-width: 698px) 100vw, 698px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Adding or subtracting the time duration <code>std::chrono::months<\/code> automatically applies modulo operations (lines 5 and 6). Subtracting two <code>std::chrono::month<\/code> objects returns <code>1<\/code> month. One month has <code>2629746<\/code> seconds (line 7). Accordingly, you can add or subtract a time duration <code>std::chrono::days<\/code> to or from a calendar data <code>std::chrono::day<\/code> (lines 8 and 9). Subtracting two <code>std::chrono::day<\/code> objects returns a <code>std::chrono::days<\/code> object. <code>std::chrono::year_month<\/code> allows the subtraction (line 10), the difference (line 11), and the comparison of time points (line 12). Objects of type <code>std::chrono::weekday_last<\/code> allow the addition\/subtraction of the time durations<code> std::chrono::months<\/code> and <code>std::chrono::years<\/code>. In addition, these <code>std::chrono::weekday_last<\/code> objects can be compared.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">C++20 supports constants and literals to make using calendar-date types more convenient.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This new data type std::chrono::hh_mm_ss in C++20 stands for the time of day. Time of Day std::chrono::hh_mm_ss is the duration since midnight split into hours, minutes, seconds, and fractional seconds. This type is typically used as a formatting tool. First, the following table gives you a concise overview of std::chrono::hh_mm_ss instance tOfDay. Depending on the [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":9316,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[453],"class_list":["post-9315","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\/9315","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=9315"}],"version-history":[{"count":30,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9315\/revisions"}],"predecessor-version":[{"id":9363,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9315\/revisions\/9363"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/9316"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=9315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=9315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=9315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}