{"id":9219,"date":"2024-03-04T08:42:00","date_gmt":"2024-03-04T08:42:00","guid":{"rendered":"https:\/\/www.modernescpp.com\/?p=9219"},"modified":"2024-03-04T08:42:01","modified_gmt":"2024-03-04T08:42:01","slug":"c20-basic-chrono-terminology","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c20-basic-chrono-terminology\/","title":{"rendered":"C++20: Basic Chrono Terminology"},"content":{"rendered":"\n<p>To get the most out of the chrono extension in C++20, a basic understanding of the chrono terminology is essential.<\/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\/02\/TimelineCpp20CoreLanguage.png\" alt=\"\" class=\"wp-image-9222\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/TimelineCpp20CoreLanguage.png 960w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/TimelineCpp20CoreLanguage-300x128.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/TimelineCpp20CoreLanguage-768x329.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/TimelineCpp20CoreLanguage-705x302.png 705w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Chrono Terminology<\/h2>\n\n\n\n<p>Essentially, the time-zone functionality (C++20) is based on the calendar functionality (C++20) and the calendar functionality (C++20), which are based on the chrono functionality (C++11). Consequently, this basic chrono terminology starts with the three C++11 components time point, time duration, and clock.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A <strong>time point<\/strong>&nbsp;has a starting point, the so-called epoch, and an additional time duration since the epoch.<\/li>\n\n\n\n<li>A <strong>time duration<\/strong> is the difference between two time points. The number of ticks of the clock defines the time duration.<\/li>\n\n\n\n<li>A <strong>clock<\/strong> has a starting point (epoch) and a tick to calculate the current time point.<\/li>\n<\/ul>\n\n\n\n<p>You can subtract time points and get a time duration. You get a new time point when you add a time duration to a time point. A year is the typical accuracy of the clock and the measure of the time duration.<\/p>\n\n\n\n<p>I will use the three concepts to present the lifetime of the 2011 died father of the programming language C: &nbsp;Dennis Ritchie. For simplicity reasons, I&#8217;m only interested in the years.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"951\" height=\"289\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/DennisRitchie.png\" alt=\"\" class=\"wp-image-9227\" style=\"width:497px\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/DennisRitchie.png 951w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/DennisRitchie-300x91.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/DennisRitchie-768x233.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/DennisRitchie-705x214.png 705w\" sizes=\"auto, (max-width: 951px) 100vw, 951px\" \/><\/figure>\n\n\n\n<p>Dennis Ritchie became 70 years old. The birth of Christ is the epoch. Combining the epoch with the time duration gives me the time points 1941 and 2011. Subtraction of the time point 1941 from 2011 provides the time duration in Year&#8217;s accuracy.<\/p>\n\n\n\n<p>C++11 has the three clocks <code>std::chrono::system_clock<\/code>, <code>std::chrono::steady_clock, <\/code>and <code>std::chrono::high_resolution_clock<\/code>. The time duration can be positive and negative. &nbsp;Each of the three clocks has a member function <code>now <\/code>for returning the current time point.<\/p>\n\n\n\n<p>If you want more details about time point, time duration, and clock, read my previous posts:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/time-point\">Time point<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/time-duration\">Time duration<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/the-three-clocks\">Clock<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.modernescpp.com\/index.php\/sleep-and-wait\">Wait and Sleep<\/a><\/li>\n<\/ul>\n\n\n\n<p>C++20 adds new components to the chrono library:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <strong>time of day<\/strong> is the time duration since midnight, split into hours, minutes, seconds, and fractional seconds.<\/li>\n\n\n\n<li><strong>Calendar<\/strong> stands for various calendar dates such as year, month, weekday, or the nth day of a week.<\/li>\n\n\n\n<li>A<strong> time zone<\/strong> represents a time specific to a geographic area.<\/li>\n\n\n\n<li>A <strong>zoned time<\/strong> combines a time point with a time zone.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Time is a Mysterium <\/h3>\n\n\n\n<p>Honestly, time, for me, is a mystery. On the one hand, each of us has an intuitive idea of time; conversely, defining it formally is exceptionally challenging. For example, the three components, time point, time duration, and clock, depend on each other.<\/p>\n\n\n\n<p>First, let me present the basic types and literals.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Types and Literals<\/h2>\n\n\n\n<p>This section includes the basic types and literals of the previous C++ standards for completeness reasons.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Clocks<\/h3>\n\n\n\n<p>Besides the wall clock <a href=\"https:\/\/www.modernescpp.com\/index.php\/the-three-clocks\">std::chrono::system_clock<\/a>, the monotonic clock <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/chrono\/steady_clock\">std::chrono::steady_clock<\/a>, and the most precise clock <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/chrono\/high_resolution_clock\">std::chrono::high_resolution_clock<\/a> in C++11, C++20 supports five additional clocks. The following table shows the characteristics of all C++ clocks and their epoch.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1030\" height=\"544\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/clock-1030x544.png\" alt=\"\" class=\"wp-image-9248\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/clock-1030x544.png 1030w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/clock-300x158.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/clock-768x405.png 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/clock-710x375.png 710w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/clock-705x372.png 705w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/clock.png 1097w\" sizes=\"auto, (max-width: 1030px) 100vw, 1030px\" \/><\/figure>\n\n\n\n<p>The clocks<code> std::chrono::steady_clock<\/code>, and <code>std::chrono::file_clock<\/code> have an implementation specified epoch. The epochs of <code>std::chrono::system_clock, std::chrono::gps_clock, std::chrono::tai_clock<\/code>, and <code>std::chrono::utc_clock<\/code> start at 00:00:00.<code> std::chrono::file_clock <\/code>is the clock for file system entries.<\/p>\n\n\n\n<p>Additionally, C++11 supports the <code>std::chrono::high_resolution_clock<\/code>. This clock is on all implementations not implemented and is only an alias for <code>std::chrono::steady_clock <\/code>or <code>std::chrono::high_resolution_clock.<\/code><\/p>\n\n\n\n<p>You can convert a time point between the clocks.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Conversion of time points between clocks<\/strong><\/h4>\n\n\n\n<p>Thanks to the function <code>std::chrono::clock_cast,<\/code> you can convert time points between the clocks having an epoch. These are the clocks<code> std::chrono::system_clock, std::chrono::utc_clock, std::chrono::gps_clock<\/code>, and <code>std::chrono::tai_clock<\/code>. Additionally, <code>std::chrono::file_clock<\/code> supports conversion.<\/p>\n\n\n\n<p>The following program converts the time point <code>2021-8-5 17:00:00 <\/code>between the various clocks.<\/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\">\/\/ convertClocks.cpp<\/span>\n\n<span style=\"color: #009999\">#include &lt;iostream&gt;<\/span>\n<span style=\"color: #009999\">#include &lt;sstream&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>literals;\n\n  std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>utc_time<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>utc_clock<span style=\"color: #555555\">::<\/span>duration<span style=\"color: #555555\">&gt;<\/span> timePoint;\n  std<span style=\"color: #555555\">::<\/span>istringstream{<span style=\"color: #CC3300\">&quot;2021-8-5 17:00:00&quot;<\/span>} <span style=\"color: #555555\">&gt;&gt;<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>parse(<span style=\"color: #CC3300\">&quot;%F %T&quot;<\/span>s, timePoint);\n\n  <span style=\"color: #006699; font-weight: bold\">auto<\/span> timePointUTC <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>clock_cast<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>utc_clock<span style=\"color: #555555\">&gt;<\/span>(timePoint);\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;UTC_time:  &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:%F %X %Z}&quot;<\/span>, timePointUTC) <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> timePointSys <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>clock_cast<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>system_clock<span style=\"color: #555555\">&gt;<\/span>(timePoint);\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;sys_time:  &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:%F %X %Z}&quot;<\/span>, timePointSys) <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> timePointFile <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>clock_cast<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>file_clock<span style=\"color: #555555\">&gt;<\/span>(timePoint);\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;file_time: &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:%F %X %Z}&quot;<\/span>, timePointFile) <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> timePointGPS <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>clock_cast<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>gps_clock<span style=\"color: #555555\">&gt;<\/span>(timePoint);\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;GPS_time:  &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:%F %X %Z}&quot;<\/span>, timePointGPS) <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> timePointTAI <span style=\"color: #555555\">=<\/span> std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>clock_cast<span style=\"color: #555555\">&lt;<\/span>std<span style=\"color: #555555\">::<\/span>chrono<span style=\"color: #555555\">::<\/span>tai_clock<span style=\"color: #555555\">&gt;<\/span>(timePoint);\n  std<span style=\"color: #555555\">::<\/span>cout <span style=\"color: #555555\">&lt;&lt;<\/span> <span style=\"color: #CC3300\">&quot;TAI_time:  &quot;<\/span> <span style=\"color: #555555\">&lt;&lt;<\/span> std<span style=\"color: #555555\">::<\/span>format(<span style=\"color: #CC3300\">&quot;{:%F %X %Z}&quot;<\/span>, timePointTAI) <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>The function <code>std::chrono::parse <\/code>parses the chrono object from the stream. In the following lines, <code>std::chrono::clock_cas<\/code>t converts the <code>timePoint <\/code>into the specified clock. The following line displays the time point, specifying its date (<code>%F<\/code>), its local time representation (<code>%X<\/code>), and its time zone abbreviation (<code>%Z<\/code>). An upcoming post will provide the details about the format string.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"374\" height=\"212\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/convertClocks.png\" alt=\"\" class=\"wp-image-9253\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/convertClocks.png 374w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2024\/02\/convertClocks-300x170.png 300w\" sizes=\"auto, (max-width: 374px) 100vw, 374px\" \/><\/figure>\n\n\n\n<p>The output may surprise you. GPS time is 18 seconds ahead of the UTC time. TAI time is 37 seconds ahead of the UTC time and 19 seconds ahead of the GPS time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p>I will continue in my next post my journey through the basic types with time durations and time points.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To get the most out of the chrono extension in C++20, a basic understanding of the chrono terminology is essential. Basic Chrono Terminology Essentially, the time-zone functionality (C++20) is based on the calendar functionality (C++20) and the calendar functionality (C++20), which are based on the chrono functionality (C++11). Consequently, this basic chrono terminology starts with [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":9222,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[375],"tags":[453],"class_list":["post-9219","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\/9219","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=9219"}],"version-history":[{"count":36,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9219\/revisions"}],"predecessor-version":[{"id":9259,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/9219\/revisions\/9259"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/9222"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=9219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=9219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=9219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}