{"id":3139,"date":"2018-06-09T00:01:07","date_gmt":"2018-06-09T07:01:07","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=3139"},"modified":"2018-06-02T12:37:21","modified_gmt":"2018-06-02T19:37:21","slug":"time-after-time","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3139","title":{"rendered":"Time After Time"},"content":{"rendered":"<p>While writing some code recently, it became necessary to translate timestamp strings into <em>time_t<\/em> or epoch time values. This value is the number of seconds that have ticked since midnight, January 1, 1970. <em>time_t<\/em> values are stored long integers (generally), but I needed a utility to translate them into time strings. It turned out, such a tool wasn&#8217;t that difficult to create.<br \/>\n<!--more--><br \/>\nHere is the code I wrote for a utility I call <em>epoch<\/em>. It takes a command line <em>time_t<\/em> value or, if not present, it prompts for one. The value is output as a time string.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    time_t now;\r\n\r\n    <span class=\"comments\">\/* gather input *\/<\/span>\r\n    if(argc &gt; 1)\r\n    {\r\n        now = atoi(argv[1]);\r\n    }\r\n    else\r\n    {\r\n        printf(\"Unix epoch time: \");\r\n        scanf(\"%ld\",&amp;now);\r\n    }\r\n\r\n    printf(\"%s\",ctime(&amp;now));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The code turned out to be much simpler than I thought: <em>time_t<\/em> variable <code>now<\/code> is declared at Line 7. If <code>argv[1]<\/code> is present, it&#8217;s translated from a string into an <em>int<\/em> value by the <em>atoi()<\/em> function at Line 12. Otherwise, input is fetched at Line 17.<\/p>\n<p>Yes, I used <em>scanf()<\/em> because it&#8217;s easy. And though a bogus value could be read, no potential for damage is made to the system by entering a bogus <em>time_t<\/em> value; you just get bad output.<\/p>\n<p>The workhorse of the utility is the <em>ctime()<\/em> function in the <em>printf()<\/em> statement at Line 20, which outputs a time string.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<pre><code>.\/epoch 1528527600\r\nSat Jun  9 00:00:00 2018<\/code><\/pre>\n<p>The problem with the <em>epoch <\/em>utility is that it doesn&#8217;t do you any good unless you know a <em>time_t<\/em> value. You can guess at values, which is an interesting diversion, but instead of guessing, I wrote the <em>now<\/em> utility. It generates the <em>time_t<\/em> value for &#8220;right now.&#8221;<\/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\r\n        time(&amp;now);\r\n        printf(\"%ld\\n\",now);\r\n\r\n        return(0);\r\n}<\/pre>\n<p>This code declares the <em>time_t<\/em> variable <code>now<\/code> at Line 6, then fills that value by using the <em>time()<\/em> function at Line 8. The value, a <em>long int<\/em>, is printed at Line 9. Simple enough, as is the output:<\/p>\n<pre><code>.\/now\r\n1528527600<\/code><\/pre>\n<p>And, if you&#8217;re using a Unix shell, you can combine the two commands to output the current time:<\/p>\n<pre><code>.\/epoch `.\/now`\r\nSat Jun  9 00:00:00 2018<\/code><\/pre>\n<p>Indeed, this code is silly stuff. Many utilities are silly, but their purpose greatly makes sense when you need them. In my case, I was working with <em>time_t<\/em> values and needed a quick way to translate them. These two utilities, <em>epoch<\/em> and <em>now<\/em>, proved most helpful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here are some handy utilities that deal with Unix epoch time. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3139\">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-3139","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\/3139","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=3139"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3139\/revisions"}],"predecessor-version":[{"id":3152,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3139\/revisions\/3152"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}