{"id":4953,"date":"2016-09-20T06:42:10","date_gmt":"2016-09-20T06:42:10","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/time-duration\/"},"modified":"2023-06-26T12:41:49","modified_gmt":"2023-06-26T12:41:49","slug":"time-duration","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/time-duration\/","title":{"rendered":"Time Duration"},"content":{"rendered":"<p>Time duration is the difference between two time points. It will be measured in time ticks.<\/p>\n<p><!--more--><\/p>\n<h2>Time duration<\/h2>\n<p>Time duration is a class template. <span style=\"font-family: courier new,courier;\">std::chrono::duration<\/span>&nbsp;consists of the type of the tick <span style=\"font-family: courier new,courier;\">Rep<\/span> and the length of a tick <span style=\"font-family: courier new,courier;\">Period.<\/span><\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000ff;\">template<\/span>&lt;\r\n    <span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">Rep<\/span>,\r\n    <span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">Period<\/span> = std::ratio&lt;1&gt;\r\n&gt; <span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">duration<\/span>;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The tick length is by default&nbsp;<span style=\"font-family: courier new,courier;\">std::ratio&lt;1&gt;<\/span>.&nbsp;<span style=\"font-family: courier new,courier;\"> std::ratio&lt;1&gt;<\/span> stands for a second and can also be written as <span style=\"font-family: courier new,courier;\">std::ratio&lt;1,1&gt;<\/span>. Therefore, it&#8217;s quite easy. <span style=\"font-family: courier new,courier;\">std::ratio&lt;60&gt;<\/span> is a minute and <span style=\"font-family: courier new,courier;\">std::ratio&lt;1,1000&gt;<\/span> a millisecond. When <span style=\"font-family: courier new,courier;\">Rep<\/span>&nbsp;is a floating point number, you can use it to hold fractions of time ticks.<\/p>\n<p>C++11 predefines the most important time durations:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000ff;\">typedef<\/span> duration&lt;<span style=\"color: #2b91af;\">signed<\/span> <span style=\"color: #2b91af;\">int<\/span>, nano&gt; nanoseconds;\r\n<span style=\"color: #0000ff;\">typedef<\/span> duration&lt;<span style=\"color: #2b91af;\">signed<\/span> <span style=\"color: #2b91af;\">int<\/span>, micro&gt; microseconds;\r\n<span style=\"color: #0000ff;\">typedef<\/span> duration&lt;<span style=\"color: #2b91af;\">signed<\/span> <span style=\"color: #2b91af;\">int<\/span>, milli&gt; milliseconds;\r\n<span style=\"color: #0000ff;\">typedef<\/span> duration&lt;<span style=\"color: #2b91af;\">signed<\/span> <span style=\"color: #2b91af;\">int<\/span>&gt; seconds;\r\n<span style=\"color: #0000ff;\">typedef<\/span> duration&lt;<span style=\"color: #2b91af;\">signed<\/span> <span style=\"color: #2b91af;\">int<\/span>, ratio&lt; 60&gt;&gt; minutes;\r\n<span style=\"color: #0000ff;\">typedef<\/span> duration&lt;<span style=\"color: #2b91af;\">signed<\/span> <span style=\"color: #2b91af;\">int<\/span>, ratio&lt;3600&gt;&gt; hours;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>How much time has passed since the UNIX epoch (1.1.1970)? Thanks to type aliases for the different time durations, I can answer the question quite easily. For simplicity reasons, I ignore leap years and assume that a year has 365 days.<\/p>\n<p>&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43\r\n44\r\n45\r\n46<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ timeSinceEpoch.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;chrono&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n\r\n  std::cout &lt;&lt; std::fixed &lt;&lt; std::endl;\r\n  \r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"Time since 1.1.1970:\\n\"<\/span> &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #0000ff;\">auto<\/span> timeNow= std::chrono::system_clock::now();\r\n  <span style=\"color: #0000ff;\">auto<\/span> duration= timeNow.time_since_epoch();\r\n  std::cout &lt;&lt; duration.count() &lt;&lt; <span style=\"color: #a31515;\">\" nanoseconds \"<\/span> &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #0000ff;\">typedef<\/span> std::chrono::duration&lt;<span style=\"color: #2b91af;\">long<\/span> <span style=\"color: #2b91af;\">double<\/span>,std::ratio&lt;1,1000000&gt;&gt; MyMicroSecondTick;\r\n  MyMicroSecondTick micro(duration);\r\n  std::cout &lt;&lt; micro.count() &lt;&lt; <span style=\"color: #a31515;\">\" microseconds\"<\/span> &lt;&lt; std::endl;\r\n  \r\n  <span style=\"color: #0000ff;\">typedef<\/span> std::chrono::duration&lt;<span style=\"color: #2b91af;\">long<\/span> <span style=\"color: #2b91af;\">double<\/span>,std::ratio&lt;1,1000&gt;&gt; MyMilliSecondTick;\r\n  MyMilliSecondTick milli(duration);\r\n  std::cout &lt;&lt; milli.count() &lt;&lt; <span style=\"color: #a31515;\">\" milliseconds\"<\/span> &lt;&lt; std::endl;\r\n  \r\n  <span style=\"color: #0000ff;\">typedef<\/span> std::chrono::duration&lt;<span style=\"color: #2b91af;\">long<\/span> <span style=\"color: #2b91af;\">double<\/span>&gt; MySecondTick;\r\n  MySecondTick sec(duration);\r\n  std::cout &lt;&lt; sec.count() &lt;&lt; <span style=\"color: #a31515;\">\" seconds \"<\/span> &lt;&lt; std::endl;\r\n  \r\n  <span style=\"color: #0000ff;\">typedef<\/span> std::chrono::duration&lt;<span style=\"color: #2b91af;\">double<\/span>, std::ratio&lt;60&gt;&gt; MyMinuteTick;\r\n  MyMinuteTick myMinute(duration);\r\n  std::cout &lt;&lt; myMinute.count() &lt;&lt; <span style=\"color: #a31515;\">\" minutes\"<\/span> &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #0000ff;\">typedef<\/span> std::chrono::duration&lt;<span style=\"color: #2b91af;\">double<\/span>, std::ratio&lt;60*60&gt;&gt; MyHourTick;\r\n  MyHourTick myHour(duration);\r\n  std::cout &lt;&lt; myHour.count() &lt;&lt; <span style=\"color: #a31515;\">\" hours\"<\/span> &lt;&lt; std::endl;\r\n  \r\n  <span style=\"color: #0000ff;\">typedef<\/span> std::chrono::duration&lt;<span style=\"color: #2b91af;\">double<\/span>, std::ratio&lt;60*60*24*365&gt;&gt; MyYearTick;\r\n  MyYearTick myYear(duration);\r\n  std::cout &lt;&lt; myYear.count() &lt;&lt; <span style=\"color: #a31515;\">\" years\"<\/span> &lt;&lt; std::endl;\r\n\r\n  <span style=\"color: #0000ff;\">typedef<\/span> std::chrono::duration&lt;<span style=\"color: #2b91af;\">double<\/span>, std::ratio&lt;60*45&gt;&gt; MyLessonTick;\r\n  MyLessonTick myLesson(duration);\r\n  std::cout &lt;&lt; myLesson.count() &lt;&lt; <span style=\"color: #a31515;\">\" lessons\"<\/span> &lt;&lt; std::endl;\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>In addition to the typical time duration&#8217;s microsecond (line 16), millisecond (line 20), second (line 24), minute (line 28), hour (line 32), and year (line 36), I define the German school hour (45 min) in line 45.<\/p>\n<p>&nbsp;<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4950\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/timeSinceEpoch.png\" alt=\"timeSinceEpoch\" style=\"margin: 15px;\" width=\"456\" height=\"315\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/timeSinceEpoch.png 456w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/timeSinceEpoch-300x207.png 300w\" sizes=\"auto, (max-width: 456px) 100vw, 456px\" \/><\/p>\n<p>It&#8217;s quite convenient to calculate with time durations.<\/p>\n<\/p>\n<h2>Calculations with time durations<\/h2>\n<p>The time durations support basic mathematics. In the case of multiplication or division, you can multiply or divide a time duration by a number. Of course, you can compare time durations. I explicitly want to emphasize that all these calculations and comparisons respect the units of the time durations.<\/p>\n<p>It gets even better with C++14 because C++14 has a bunch of predefined time literals.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4951\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/timeLiteralsEng.png\" alt=\"timeLiteralsEng\" width=\"500\" height=\"223\" style=\"margin: 15px;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/timeLiteralsEng.png 678w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/timeLiteralsEng-300x134.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<p>How much time does my 16 year old son need for his typical school day? I will answer the question in the example and show the result in different time durations.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ schoolDay.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;chrono&gt;<\/span>\r\n\r\n<span style=\"color: #0000ff;\">using<\/span> <span style=\"color: #0000ff;\">namespace<\/span> std::literals::chrono_literals;\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n  constexpr <span style=\"color: #0000ff;\">auto<\/span> schoolHour= 45min;\r\n\r\n  constexpr <span style=\"color: #0000ff;\">auto<\/span> shortBreak= 300s;\r\n  constexpr <span style=\"color: #0000ff;\">auto<\/span> longBreak= 0.25h;\r\n\r\n  constexpr <span style=\"color: #0000ff;\">auto<\/span> schoolWay= 15min;\r\n  constexpr <span style=\"color: #0000ff;\">auto<\/span> homework= 2h;\r\n\r\n  constexpr <span style=\"color: #0000ff;\">auto<\/span> schoolDayInSeconds= 2*schoolWay + 6 * schoolHour + 4 * shortBreak + longBreak + homework;\r\n\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"School day in seconds: \"<\/span> &lt;&lt; schoolDayInSeconds.count() &lt;&lt; std::endl;\r\n\r\n  std::chrono::duration&lt;<span style=\"color: #2b91af;\">double<\/span>,std::ratio&lt;3600&gt;&gt; schoolDayInHours = schoolDayInSeconds;\r\n  std::chrono::duration&lt;<span style=\"color: #2b91af;\">double<\/span>,std::ratio&lt;60&gt;&gt; schoolDayInMinutes = schoolDayInSeconds;\r\n  std::chrono::duration&lt;<span style=\"color: #2b91af;\">double<\/span>,std::ratio&lt;1,1000&gt;&gt; schoolDayInMilliseconds= schoolDayInSeconds;\r\n\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"School day in hours: \"<\/span> &lt;&lt; schoolDayInHours.count() &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"School day in minutes: \"<\/span> &lt;&lt; schoolDayInMinutes.count() &lt;&lt; std::endl;\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"School day in milliseconds: \"<\/span> &lt;&lt; schoolDayInMilliseconds.count() &lt;&lt; std::endl;\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The time literals are <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/language\/constexpr\">constant expressions<\/a>; therefore, they can be evaluated at compile time. I have time durations for a German school hour (line 12), for a short break (line 14), for a long break (line 15), for his way to the school (line 17), and his homework (line 18). The result of the calculation <span style=\"font-family: courier new,courier;\">schoolDaysInSeconds<\/span> (line 20) is available at compile time.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4952\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/schoolDay.png\" alt=\"schoolDay\" style=\"margin: 15px;\" width=\"456\" height=\"223\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/schoolDay.png 456w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/schoolDay-300x147.png 300w\" sizes=\"auto, (max-width: 456px) 100vw, 456px\" \/><\/p>\n<p>Impressed? I&#8217;m not. His daily duty is only about 7 1\/2 hours.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>The used clock gives the accuracy of the time tick.&nbsp;In C++, we have the clocks <span style=\"font-family: courier new,courier;\">std::chrono::system_clock,<\/span> <span style=\"font-family: courier new,courier;\">std::chrono::steady_clock,<\/span> and <span style=\"font-family: courier new,courier;\">std::chrono::high_resolution_clock. <\/span>The three clocks will be the topic of my <a href=\"https:\/\/www.modernescpp.com\/index.php\/the-three-clocks\">next post<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Time duration is the difference between two time points. It will be measured in time ticks.<\/p>\n","protected":false},"author":21,"featured_media":4950,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[369],"tags":[453],"class_list":["post-4953","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\/4953","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=4953"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4953\/revisions"}],"predecessor-version":[{"id":6945,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4953\/revisions\/6945"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/4950"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=4953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=4953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=4953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}