{"id":3409,"date":"2018-12-15T00:01:12","date_gmt":"2018-12-15T08:01:12","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3409"},"modified":"2019-11-23T21:56:33","modified_gmt":"2019-11-24T05:56:33","slug":"tic-tock-goes-the-clock-part-i","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3409","title":{"rendered":"Tick-Tock Goes the Clock, Part I"},"content":{"rendered":"<p>The other day I had this strong urge to write myself a text mode clock program, one that runs at the command prompt. This desire harkens back to my days programming text mode computers: I don&#8217;t need a clock, I just wanted to see if I could code one.<br \/>\n<!--more--><br \/>\nThe clock must display the current time of day, which can easily be obtained from the <em>ctime()<\/em> function, defined in the <code>time.h <\/code>header. This function requires an argument representing a <em>time_t<\/em> value, the number of seconds ticked since the dawn of the Unix epoch, January 1, 1970:<\/p>\n<p><code>time(&now);<\/code><\/p>\n<p>Variable <code>now<\/code> is a <em>time_t<\/em> type. Its address is passed to the <em>time()<\/em> function, which sets the value of variable <code>now<\/code> to the current epoch value. The <code>now<\/code> variable can then be used in the <em>ctime()<\/em> function to return a time string, but for my clock I just want the current hours and minutes. So instead of <em>ctime()<\/em>, I use the <em>localtime()<\/em> function, which fills a <em>tm<\/em> structure with individual time tidbits. Two members of the structure are <em>tm_hour<\/em> and <em>tm_minute<\/em>, which is what I want.<\/p>\n<p><em>Whew<\/em>!<\/p>\n<p>Full details of all this timely nonsense can be found in my books, which I urge you to read.<\/p>\n<p>Below is the source code required to read the system&#8217;s time, extract the current hour and minute, and then display those values.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint main()\r\n{\r\n    time_t now;\r\n    struct tm *clock;\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\r\n    return(0);\r\n}<\/pre>\n<p>And here&#8217;s the output:<\/p>\n<p><code>It is now 11:41<\/code><\/p>\n<p>Yes, the clock program works, but I want more! I want the output to update every minute. Like a digital clock, I want my program to output <code>11:42<\/code> to reflect the current time &mdash; and then <code>11:43<\/code> and so on. To accomplish this task requires additional programming, which I cover in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3411\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Writing a clock program isn&#8217;t as easy as it seems. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3409\">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-3409","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\/3409","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=3409"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3409\/revisions"}],"predecessor-version":[{"id":3880,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3409\/revisions\/3880"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}