{"id":3421,"date":"2018-12-29T00:01:14","date_gmt":"2018-12-29T08:01:14","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3421"},"modified":"2018-12-22T12:00:01","modified_gmt":"2018-12-22T20:00:01","slug":"tick-tock-goes-the-clock-part-iii","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3421","title":{"rendered":"Tick-Tock Goes the Clock, Part III"},"content":{"rendered":"<p>My clock program is almost complete! It generates the current time as output and it updates the current time every minute. The only step remaining is to synchronize the program&#8217;s output with the system time. I want to ensure that when the system reads :00 seconds that the program ticks over to the next minute.<br \/>\n<!--more--><br \/>\nThe modification to the code from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3411\">last week&#8217;s Lesson<\/a> is cinchy. It&#8217;s a change to Line 20 in the code, the second <em>while<\/em> loop that pauses until the next minute occurs:<\/p>\n<p><code>while(time(NULL)%60)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;;<\/code><\/p>\n<p>The <code>time(NULL)<\/code> expression returns the current clock tick value, the number of seconds elapsed since midnight January 1, 1970. The value ends in a multiple of 60 as one minute clicks to the next. So if you use the expression <code>%60<\/code> (mod sixty), the <em>while<\/em> statement returns true only when the clock ticks to the next minute. This is exactly the condition I need to keep my clock program&#8217;s output in sync with the system clock.<\/p>\n<p><a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2018\/12\/1229a.c\">Click here<\/a> to view the entire code.<\/p>\n<p>The program&#8217;s output, however, is less than desirable. The initial line is output fine, but once the top of the minute hits, the output streams for a second and makes me unhappy. That&#8217;s because the code runs so fast that the next minute holds while the clock tick value is still true for <code>time(NULL)%60<\/code>.<\/p>\n<p>To remove the streaming output for that one second, a delay must be added to the code. After the :00 minute occurs, the code must pause for over one second. To add the pause, I used the <em>sleep()<\/em> function, which is prototyped in the <code>unistd.h<\/code> header file. I inserted the following line before the inner <em>while<\/em> loop:<\/p>\n<p><code>sleep(2);<\/code><\/p>\n<p>Here&#8217;s the full code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;time.h&gt;\r\n#include &lt;unistd.h&gt;\r\n\r\nint main()\r\n{\r\n    time_t now;\r\n    struct tm *clock;\r\n\r\n    while(1)\r\n    {\r\n        <span class=\"comments\">\/* fetch the current time *\/<\/span>\r\n        time(&amp;now);\r\n        <span class=\"comments\">\/* fill the time structure *\/<\/span>\r\n        clock = localtime(&amp;now);\r\n        <span class=\"comments\">\/* display the clock *\/<\/span>\r\n        printf(\"It is now %2d:%02d\\n\",\r\n                clock-&gt;tm_hour,\r\n                clock-&gt;tm_min\r\n              );\r\n        <span class=\"comments\">\/* delay *\/<\/span>\r\n        sleep(2);\r\n        <span class=\"comments\">\/* wait until the next minute *\/<\/span>\r\n        while(time(NULL)%60)\r\n            ;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The problem with this solution is that not every compiler implements the <em>sleep()<\/em> function. If not, you can add a delay such as:<\/p>\n<p><code>while( now+2 &gt; time(NULL) )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;;<\/code><\/p>\n<p>The above loop easily replaces the <code>sleep(2)<\/code> statement, adding a 2-second pause to the program and preventing the top-of-the-minute streaming ugliness.<\/p>\n<p>With this final change, my clock program is complete: It displays the current time, updating at the top of the minute in coordination with the system clock.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Keep the clock&#8217;s time in sync with the system time. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3421\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3421","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3421"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3421\/revisions"}],"predecessor-version":[{"id":3438,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3421\/revisions\/3438"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}