{"id":3435,"date":"2019-01-05T00:01:50","date_gmt":"2019-01-05T08:01:50","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3435"},"modified":"2018-12-29T09:16:16","modified_gmt":"2018-12-29T17:16:16","slug":"negative-time","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3435","title":{"rendered":"Negative Time"},"content":{"rendered":"<p>With the dawn of the new year, and my reflection upon the Clock program I <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3409\">blogged about<\/a> recently, I&#8217;ve been thinking about time. Specifically, the <em>time_t<\/em> data type used with various C library time functions. Can this value contain negative time?<br \/>\n<!--more--><br \/>\nThe <em>time_t<\/em> value represents the number of seconds ticked in the Unix epoch, since January 1, 1970. As time marches forward, this value increases in a positive direction. Can it be negative? Can you use a <em>time_t<\/em> value to calculate the date of Caesar&#8217;s assassination?<\/p>\n<p>As a review, the following code obtains the current clock-tick value from the <em>time()<\/em> function, storing it in <em>time_t<\/em> variable <code>now<\/code>. The results are displayed; the <em>ctime()<\/em> function generates a time-and-date string (with newline), which is output in the code:<\/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(\"It's now %s\",ctime(&amp;now));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s sample output, which varies depending on when you run the program:<\/p>\n<p><code>It's now Sat Jan 05 12:05:23 2019<\/code><\/p>\n<p>What about a <em>time_t<\/em> value of zero? I modified the preceding code to set zero as the <em>time_t<\/em> value:<\/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 = 0;\r\n\r\n    printf(\"The epoch is %s\",ctime(&amp;now));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s the output I saw on my computer:<\/p>\n<p><code>The epoch is Wed Dec 31 16:00:00 1969<\/code><\/p>\n<p>My time zone is -8 hours from UTC, so that explains why the date isn&#8217;t midnight, January 1, 1970.<\/p>\n<p>And for a negative value? Here&#8217;s my code:<\/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 = -63549286400;\r\n\r\n    printf(\"A bad day for Caesar: %s\",ctime(&amp;now));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>And the output:<\/p>\n<p><code>A bad day for Caesar: Thu Mar 15 12:33:42 -044<\/code><\/p>\n<p>I&#8217;m guessing about the noontime assassination hour, which probably isn&#8217;t accurate for time in Italy back in the day. Plus, I had to fiddle with a calculator to get at the exact number of negative seconds before the Unix epoch, but I proved my point: Using C library time functions, you can calculate &#8220;negative&#8221; dates &mdash; and quite far back in time as well.<\/p>\n<p>On my system, the <em>time_t<\/em> data type is 8-bytes wide, which is a size capable of holding the value 18,446,744,073,709,551,615 or 18 quintillion plus. That&#8217;s a lot of time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Time moves forward, but a <em>time_t<\/em> value can also be negative. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3435\">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-3435","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\/3435","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=3435"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3435\/revisions"}],"predecessor-version":[{"id":3441,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3435\/revisions\/3441"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}