{"id":4949,"date":"2016-09-17T11:25:33","date_gmt":"2016-09-17T11:25:33","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/time-point\/"},"modified":"2023-06-26T12:42:10","modified_gmt":"2023-06-26T12:42:10","slug":"time-point","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/time-point\/","title":{"rendered":"Time Point"},"content":{"rendered":"<p>The starting point (epoch) and the additional time duration define the time point. It consists of two components, clock and&nbsp;time duration.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<h2>Time Point<\/h2>\n<p>Time point is a class template.&nbsp;<span style=\"font-family: courier new,courier;\">std::chrono::time_point<\/span> requires a clock. By default, the time duration is derived from the clock.&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<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;\">Clock<\/span>,\r\n  <span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">Duration<\/span>= <span style=\"color: #0000ff;\">typename<\/span> Clock::duration\r\n&gt;\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">time_point<\/span>;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>There a four special time points depending on the clock.<\/p>\n<ul>\n<li><strong>epoch<\/strong>: The starting point of the clock.<\/li>\n<li><strong>now<\/strong>: The current time.<\/li>\n<li><strong>min<\/strong>: The minimum time point that the clock can display.<\/li>\n<li><strong>max<\/strong>: The maximum time point that the clock can display.<\/li>\n<\/ul>\n<p>The accuracy of the values depends on the used clock:&nbsp; <span style=\"font-family: courier new,courier;\">std::system::system_clock, std::chrono::steady_clock<\/span>, or <span style=\"font-family: courier new,courier;\">std::chrono::high_resolution_clock<\/span>.<\/p>\n<p>C++&nbsp;gives no guarantee about a clock&#8217;s accuracy, starting point, or valid time range. The starting point of std::chrono::system_clock is typically the 1.1.1970, the so-called <a href=\"https:\/\/en.wikipedia.org\/wiki\/Unix_time\">UNIX-epoch<\/a>. It holds further that the <span style=\"font-family: courier new,courier;\">std::chrono::high_resolution<\/span> clock has the highest accuracy.<\/p>\n<\/p>\n<h3>Displaying a time duration as a date<\/h3>\n<p>If the time point uses internally <span style=\"font-family: courier new,courier;\">std::chrono::system_clock<\/span>, you can convert the time point with the help of <span style=\"font-family: courier new,courier;\"><\/span><a href=\"http:\/\/en.cppreference.com\/w\/cpp\/chrono\/system_clock\/to_time_t\">std::chrono::system_clock::to_time_t <\/a>in an object of type <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/chrono\/c\/time_t\">std::time_t<\/a>. Thanks to further conversions with the functions <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/chrono\/c\/gmtime\"> std::gmtime<\/a> and <a href=\"http:\/\/en.cppreference.com\/w\/cpp\/chrono\/c\/asctime\">std::asctime<\/a>, you have the time points as dates in textual representation available.<\/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<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ timepoint.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;chrono&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;ctime&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;iomanip&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #0000ff;\">#include &lt;string&gt;<\/span>\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    std::chrono::time_point&lt;std::chrono::system_clock&gt; sysTimePoint;\r\n    std::<span style=\"color: #2b91af;\">time_t<\/span> tp= std::chrono::system_clock::<span style=\"color: #2b91af;\">to_time_t<\/span>(sysTimePoint);\r\n    std::string sTp= std::asctime(std::gmtime(&amp;tp));\r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"Epoch: \"<\/span> &lt;&lt; sTp &lt;&lt; std::endl;\r\n    \r\n    tp= std::chrono::system_clock::<span style=\"color: #2b91af;\">to_time_t<\/span>(sysTimePoint.min());\r\n    sTp= std::asctime(std::gmtime(&amp;tp));\r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"Time min: \"<\/span> &lt;&lt; sTp &lt;&lt; std::endl;\r\n    \r\n    tp= std::chrono::system_clock::<span style=\"color: #2b91af;\">to_time_t<\/span>(sysTimePoint.max());\r\n    sTp= std::asctime(std::gmtime(&amp;tp));\r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"Time max: \"<\/span> &lt;&lt; sTp &lt;&lt; std::endl;\r\n    \r\n    sysTimePoint= std::chrono::system_clock::now();\r\n    tp= std::chrono::system_clock::<span style=\"color: #2b91af;\">to_time_t<\/span>(sysTimePoint);\r\n    sTp= std::asctime(std::gmtime(&amp;tp));\r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"Time now: \"<\/span> &lt;&lt; sTp &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 output of the program shows the valid range of <span style=\"font-family: courier new,courier;\">std::chrono::system_clock. std::chrono::system_clock<\/span> has the UNIX-epoch as starting point and can have time points between 1677 and 2262.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4947\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/timepoint.png\" alt=\"timepoint\" style=\"margin: 15px;\" width=\"612\" height=\"277\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/timepoint.png 612w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/timepoint-300x136.png 300w\" sizes=\"auto, (max-width: 612px) 100vw, 612px\" \/><\/p>\n<p>You can add time durations to time points to get&nbsp;new time points. Now I&#8217;m curious. What will happen if I&#8217;m out of the valid range of the time points?<\/p>\n<h3>Beyond the boundaries of the valid time range<\/h3>\n<p>My experiment uses the current time and adds 1000 years or subtracts 1000 years from it. I ignore leap years and assume that the year has 365 days for simplicity.<\/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<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\">\/\/ timepointAddition.cpp\r\n\r\n<span style=\"color: #008000;\">#include &lt;chrono&gt;<\/span>\r\n<span style=\"color: #008000;\">#include &lt;ctime&gt;<\/span>\r\n<span style=\"color: #008000;\">#include &lt;iomanip&gt;<\/span>\r\n<span style=\"color: #008000;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #008000;\">#include &lt;string&gt;<\/span>\r\n\r\nusing namespace std::chrono;\r\n\r\nstd::string timePointAsString(const time_point&lt;system_clock&gt;&amp; timePoint){\r\n    std::time_t tp= system_clock::to_time_t(timePoint);\r\n    <span style=\"color: #0000ff;\">return<\/span> std::asctime(std::gmtime(&amp;tp));\r\n}\r\n\r\nint main(){  \r\n    \r\n    std::cout &lt;&lt; std::endl;\r\n    \r\n    time_point&lt;system_clock&gt; nowTimePoint= system_clock::now();\r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"Now:              \"<\/span> &lt;&lt; timePointAsString(nowTimePoint) &lt;&lt; std::endl;\r\n     \r\n    auto thousandYears=  hours(24*365*1000);\r\n    time_point&lt;system_clock&gt;  historyTimePoint= nowTimePoint - thousandYears;\r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"Now - 1000 years: \"<\/span> &lt;&lt; timePointAsString(historyTimePoint) &lt;&lt; std::endl;\r\n    \r\n    time_point&lt;system_clock&gt;  futureTimePoint= nowTimePoint + thousandYears;\r\n    std::cout &lt;&lt; <span style=\"color: #a31515;\">\"Now + 1000 years: \"<\/span> &lt;&lt; timePointAsString(futureTimePoint) &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>For the sake of readability, I introduced the namespace <span style=\"font-family: courier new,courier;\">std::chrono. <\/span>The program&#8217;s output shows that an overflow of the time points in lines 24 and 27 causes&nbsp;wrong results. Subtracting 1000 years from the current time point gives a time point in the future; adding 1000 years to the current time point gives a time point in the past, respectively.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4948\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/timepointAddition.png\" alt=\"timepointAddition\" style=\"margin: 15px;\" width=\"456\" height=\"247\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/timepointAddition.png 456w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/09\/timepointAddition-300x163.png 300w\" sizes=\"auto, (max-width: 456px) 100vw, 456px\" \/><\/p>\n<h2>What&#8217;s next? &nbsp;<\/h2>\n<p>The difference between two time points is the time duration. Time durations support the basic arithmetic and can be displayed in different time ticks. How? Wait for the <a href=\"https:\/\/www.modernescpp.com\/index.php\/time-duration\">next post<\/a>.&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The starting point (epoch) and the additional time duration define the time point. It consists of two components, clock and&nbsp;time duration.<\/p>\n","protected":false},"author":21,"featured_media":4947,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[369],"tags":[453],"class_list":["post-4949","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\/4949","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=4949"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4949\/revisions"}],"predecessor-version":[{"id":6946,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/4949\/revisions\/6946"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/4947"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=4949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=4949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=4949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}