{"id":9276,"date":"2024-03-18T10:34:33","date_gmt":"2024-03-18T10:34:33","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=9276"},"modified":"2024-03-18T10:34:33","modified_gmt":"2024-03-18T10:34:33","slug":"c20-basic-chrono-terminology-with-time-duration-and-time-point","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c20-basic-chrono-terminology-with-time-duration-and-time-point\/","title":{"rendered":"C++20: Basic Chrono Terminology with Time Duration and Time Point"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Today, I continue my journey through the basic types with time durations and time points.<\/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<h2 class=\"wp-block-heading\">Time Durations<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">C++14 introduced helper types such as<code> std::chrono::seconds<\/code> for time durations and corresponding time literals such as <code>5s<\/code>. C++20 added new helper types. The following table shows all for completeness.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1002\" height=\"422\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimeDuration.png\" alt=\"\" class=\"wp-image-9280\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimeDuration.png 1002w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimeDuration-300x126.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimeDuration-768x323.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimeDuration-705x297.png 705w\" sizes=\"auto, (max-width: 1002px) 100vw, 1002px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Often the time duration<code> std::chrono::days <\/code>and the calendar date<code> std::chrono::day <\/code>are mixed up. The same holds for the time duration <code>std::chrono::years<\/code> and the calendar date <code>std::chrono::year<\/code>. &nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Distinguish between the time durations <code>std::chrono::days<\/code>, <code>std::chrono::years<\/code>, and the calendar types<code> std::chrono::day<\/code>, <code>std::chrono::year<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">C++20 added two new literals for new calendar types<code> std::chrono::day<\/code> and <code>std::chrono::year<\/code>. The literals <code>d <\/code>and<code> y<\/code> refer to a<code> std::chrono::day<\/code> and <code>std::chrono::year.<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"993\" height=\"192\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/CalendarTypes.png\" alt=\"\" class=\"wp-image-9284\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/CalendarTypes.png 993w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/CalendarTypes-300x58.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/CalendarTypes-768x148.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/CalendarTypes-705x136.png 705w\" sizes=\"auto, (max-width: 993px) 100vw, 993px\" \/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The day literal represents a day of the month and is unspecified if outside the range [0, 255].<\/li>\n\n\n\n<li>The year literal represents a year in the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Gregorian_calendar\">Gregorian calendar<\/a> and is unspecified if outside the range [-32767, 32767].<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The following program emphasizes the difference between <code>std::chrono::days<\/code> and <code>std::chrono::day<\/code> and, accordingly, <code>std::chrono::years <\/code>and <code>std::chrono::year<\/code>.<\/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\">\/\/ dayDays.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;chrono&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> <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>chrono<span style=\"color: #555555\">::<\/span>days days1 <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>day(<span style=\"color: #FF6600\">30<\/span>) <span style=\"color: #555555\">-<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>day(<span style=\"color: #FF6600\">25<\/span>);          <span style=\"color: #0099FF; font-style: italic\">\/\/ (1)<\/span>\n    std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>days days2 <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">30<\/span>d <span style=\"color: #555555\">-<\/span> <span style=\"color: #FF6600\">25<\/span>d;                                            <span style=\"color: #0099FF; font-style: italic\">\/\/ (3)<\/span>\n    <span style=\"color: #006699; font-weight: bold\">if<\/span> ( days1 <span style=\"color: #555555\">==<\/span> days2 <span style=\"color: #555555\">&amp;&amp;<\/span> \n         days1 <span style=\"color: #555555\">==<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>days(<span style=\"color: #FF6600\">5<\/span>)) std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Five days<\/span><span style=\"color: #CC3300; font-weight: bold\">\\n<\/span><span style=\"color: #CC3300\">&quot;<\/span>;\n\n    std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>years years1 <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year(<span style=\"color: #FF6600\">2021<\/span>) <span style=\"color: #555555\">-<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>year(<span style=\"color: #FF6600\">1998<\/span>);  <span style=\"color: #0099FF; font-style: italic\">\/\/ (2)<\/span>\n    std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>years years2<span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">2021<\/span>y <span style=\"color: #555555\">-<\/span> <span style=\"color: #FF6600\">1998<\/span>y;                                       <span style=\"color: #0099FF; font-style: italic\">\/\/ (4)<\/span>\n    <span style=\"color: #006699; font-weight: bold\">if<\/span> ( years1 <span style=\"color: #555555\">==<\/span> years2 <span style=\"color: #555555\">&amp;&amp;<\/span> \n         years1 <span style=\"color: #555555\">==<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>years(<span style=\"color: #FF6600\">23<\/span>)) std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;Twenty-three years<\/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}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">When you subtract two objects of type<code> std::chrono::day<\/code> (line 1), you get an object of type <code>std::chrono::days<\/code>. The same holds for the <code>std::chrono::year<\/code> (lines 2) and <code>std::chrono::years<\/code>. Thanks to the using declaration <code>using namespace std::chrono_literals<\/code>, I can directly specify the time literals for<code> std::chrono::day<\/code> and std::chrono::year (lines 3 and 4).<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"391\" height=\"184\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/dayDays.png\" alt=\"\" class=\"wp-image-9290\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/dayDays.png 391w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/dayDays-300x141.png 300w\" sizes=\"auto, (max-width: 391px) 100vw, 391px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Include Literals<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">There are various ways to include the literals.<\/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: #006699; font-weight: bold\">using<\/span> <span style=\"color: #006699; font-weight: bold\">namespace<\/span> std<span style=\"color: #555555\">::<\/span>literals;  \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;\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<span style=\"color: #006699; font-weight: bold\">using<\/span> <span style=\"color: #006699; font-weight: bold\">namespace<\/span> std<span style=\"color: #555555\">::<\/span>literals<span style=\"color: #555555\">::<\/span>chrono_literals;\n<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>using namespace std::literals<\/code>: includes all C++ literals<\/li>\n\n\n\n<li><code>using namespace std::chrono<\/code>: includes the entire namespace<code> std::chrono<\/code><\/li>\n\n\n\n<li><code>using namespace std::chrono_literals<\/code>: includes all time literals<\/li>\n\n\n\n<li><code>using namespace std::literals::chrono_literals<\/code>: includes all time literals<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The program<code> literals.cpp<\/code> shows the use of different using declarations.<\/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\">\/\/ literals.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;chrono&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;string&gt;<\/span>\n\n<span style=\"color: #007788; font-weight: bold\">int<\/span> <span style=\"color: #CC00FF\">main<\/span>() {\n\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>literals;\n\n        std<span style=\"color: #555555\">::<\/span>string cppString <span style=\"color: #555555\">=<\/span> <span style=\"color: #CC3300\">&quot;C++ string literal&quot;<\/span>s;     <span style=\"color: #0099FF; font-style: italic\">\/\/ (1)<\/span>\n        <span style=\"color: #006699; font-weight: bold\">auto<\/span> aMinute <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">60<\/span>s;                                <span style=\"color: #0099FF; font-style: italic\">\/\/ (2)<\/span>\n        <span style=\"color: #0099FF; font-style: italic\">\/\/ duration aHour = 0.25h + 15min + 1800s;<\/span>\n    }\n\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;\n\n        <span style=\"color: #0099FF; font-style: italic\">\/\/ std::string cppString = &quot;C++ string literal&quot;s;<\/span>\n        <span style=\"color: #006699; font-weight: bold\">auto<\/span> aMinute <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">60<\/span>s;\n        duration aHour <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">0.25<\/span>h <span style=\"color: #555555\">+<\/span> <span style=\"color: #FF6600\">15<\/span>min <span style=\"color: #555555\">+<\/span> <span style=\"color: #FF6600\">1800<\/span>s;            <span style=\"color: #0099FF; font-style: italic\">\/\/ (3)<\/span>\n    }\n\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        <span style=\"color: #0099FF; font-style: italic\">\/\/ std::string cppString = &quot;C++ String literal&quot;s;<\/span>\n        <span style=\"color: #006699; font-weight: bold\">auto<\/span> aMinute <span style=\"color: #555555\">=<\/span> <span style=\"color: #FF6600\">60<\/span>s;\n        <span style=\"color: #0099FF; font-style: italic\">\/\/ duration aHour = 0.25h + 15min + 1800s;<\/span>\n    }\n\n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The using namespace<code> std::literals declarations<\/code> enable it to use all built-in literals such as string literal (<code>\"C++ string literal\"s<\/code> in line 1) or the time literal (<code>60s<\/code> in line 2). `<code>std::chrono::duration<\/code>`cannot be used unqualified. On the contrary, the using declaration <code>using namespace std::chrono<\/code> allows it to use the time literals and the type <code>std::chrono::duration<\/code> (line 3) unqualified: d<code>uration aHour = &nbsp;0.25h + 15min + 1800s<\/code>. Thanks to the using declaration <code>using namespace::std::chrono::literals<\/code>, all time literals are available.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Time  Points<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Besides the clock and the time duration, the third fundamental type in C++11 was<code> std::chrono::time_point<\/code>.<\/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: #006699; font-weight: bold\">template<\/span><span style=\"color: #555555\">&lt;<\/span><span style=\"color: #006699; font-weight: bold\">typename<\/span> Clock, <span style=\"color: #006699; font-weight: bold\">typename<\/span> Duration <span style=\"color: #555555\">=<\/span> <span style=\"color: #006699; font-weight: bold\">typename<\/span> Clock<span style=\"color: #555555\">::<\/span>duration<span style=\"color: #555555\">&gt;<\/span> \n<span style=\"color: #006699; font-weight: bold\">class<\/span> <span style=\"color: #00AA88; font-weight: bold\">time_point<\/span>;\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">A<code> std::chrono::time_point<\/code> depends on the clock and the time duration. C++20 provides aliases for additional time points.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"613\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimePoints.png\" alt=\"\" class=\"wp-image-9298\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimePoints.png 960w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimePoints-300x192.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimePoints-768x490.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/03\/TimePoints-705x450.png 705w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">With the exception of <code>std::chrono::steady_clock<\/code>, you can define a time point with the specified time duration. All but not the clock <code>std::chrono::file_clock<\/code> enables it to specify it for seconds. Additionally, <code>std::chrono::local_t<\/code> and <code>std::chrono::system_clock<\/code> enables to specify it for days.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>std::chrono::hh_mm_ss<\/code> is the time duration since midnight, split into hours, minutes, seconds, and fractional seconds. This new data type in C++20 stands for the time of day.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, I continue my journey through the basic types with time durations and time points. Time Durations C++14 introduced helper types such as std::chrono::seconds for time durations and corresponding time literals such as 5s. C++20 added new helper types. The following table shows all for completeness. Often the time duration std::chrono::days and the calendar date [&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-9276","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\/9276","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=9276"}],"version-history":[{"count":25,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9276\/revisions"}],"predecessor-version":[{"id":9309,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9276\/revisions\/9309"}],"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=9276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=9276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=9276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}