{"id":4946,"date":"2016-09-14T20:29:18","date_gmt":"2016-09-14T20:29:18","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/the-time-library-2\/"},"modified":"2023-08-11T15:57:41","modified_gmt":"2023-08-11T15:57:41","slug":"the-time-library-2","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/the-time-library-2\/","title":{"rendered":"The Time Library"},"content":{"rendered":"<p>A blog dealing with multithreading in modern C++ but not writing about the new time library is incomplete. Primarily because I often used the time library in my posts to measure the performance of shortcode snippets. Therefore, I give in this post an overview of the components of the time library: time point, time duration, and clock. I will write additional posts about each of these three components.<\/p>\n<p><!--more--><\/p>\n<p>As ever before, the practice is the theory.<\/p>\n<h2>Time Point, Time Duration, and Clocks<\/h2>\n<p>The time library consists of three parts that are dependent on each other.<\/p>\n<ul>\n<li><strong>Time point<\/strong>: The time point is given by its starting point &#8211; the so-called epoch &#8211; and the time duration, which refers to the epoch.<\/li>\n<li><strong>Time duration: <\/strong>The time duration is the difference between two-time points. It&#8217;s measured in the number of time ticks.<\/li>\n<li><strong>Clock<\/strong>: The clock consists of a starting point and a time tick. This information enables you to calculate the current time.<\/li>\n<\/ul>\n<p>You can compare time points. You get a new time point when you add a time duration to a time point. The time tick is the clock&#8217;s accuracy in which you measure the time duration. The birth of Christ in my culture is the starting time point and a year, a typical time tick.<\/p>\n<p>I will use the three concepts to present the lifetime of the 2011 dead father of the programming language C:&nbsp; <a href=\"https:\/\/de.wikipedia.org\/wiki\/Dennis_Ritchie\">Dennis Ritchie<\/a>. For simplicity reasons, I&#8217;m only interested in the years.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4945\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/ZeitPunktDauerGeberEng.png\" alt=\"ZeitPunktDauerGeberEng\" width=\"700\" height=\"213\" style=\"margin: 15px;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/ZeitPunktDauerGeberEng.png 951w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/ZeitPunktDauerGeberEng-300x91.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/ZeitPunktDauerGeberEng-768x233.png 768w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>The time points 1941 and 2011 are defined by the birth of Christ and the time duration. Therefore, the birth of Christ is our epoch. Of course, the epoch is also a time point. When I subtract 1941 from 2011, I get the time duration. This dime duration is measured with the accuracy year. Dennis Ritchie became 70 years old.&nbsp;<\/p>\n<p>Before I dive deeper into the time library in an upcoming post, a few remarks, a time point consists of a clock and a time duration. C++ has the clocks <span style=\"font-family: courier new,courier;\">std::chrono::system_clock, std::chrono::steady_clock<\/span>, and <span style=\"font-family: courier new,courier;\">std::chrono::high_resolution_clock<\/span>. The time duration can be positive and negative. The known time durations as <span style=\"font-family: courier new,courier;\">std::chrono::seconds, std::chrono::minutes<\/span>, or <span style=\"font-family: courier new,courier;\">std::chrono::hours<\/span> are predefined in C++. Additionally, C++14 offers time literals: <span style=\"font-family: courier new,courier;\">1s<\/span>, <span style=\"font-family: courier new,courier;\">1min<\/span>, or <span style=\"font-family: courier new,courier;\">1h<\/span>. The clocks differ in their accuracy (time tick) and epoch. Although it&#8217;s not defined, std::chrono::system_clock usually starts at 1.1.1970. Each clock has a method <span style=\"font-family: courier new;\">now <\/span>for returning the current time (time point).<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>In the <a href=\"https:\/\/www.modernescpp.com\/index.php\/time-point\">next post<\/a>, I will write about the concept of time points. Therefore, I will calculate in different time ticks how much time is passed since 1.1.1970.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A blog dealing with multithreading in modern C++ but not writing about the new time library is incomplete. Primarily because I often used the time library in my posts to measure the performance of shortcode snippets. Therefore, I give in this post an overview of the components of the time library: time point, time duration, [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":4945,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[369],"tags":[453],"class_list":["post-4946","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-multithreading-application","tag-time"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4946","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=4946"}],"version-history":[{"count":2,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4946\/revisions"}],"predecessor-version":[{"id":8041,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4946\/revisions\/8041"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/4945"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=4946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=4946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=4946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}