{"id":3120,"date":"2018-06-16T00:01:40","date_gmt":"2018-06-16T07:01:40","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=3120"},"modified":"2018-06-09T12:54:18","modified_gmt":"2018-06-09T19:54:18","slug":"looping-with-time_t-values","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3120","title":{"rendered":"Looping with <em>time_t<\/em> values"},"content":{"rendered":"<p>In C, the <em>time_t<\/em> variable type is associated with the Unix epoch, or the number of seconds ticked since midnight, January 1, 1970. It&#8217;s typically a <em>long int<\/em> value, though as a <em>typedef<\/em> declaration, its specific data type could change in the future. Still, as a <em>long int<\/em>, you can play with it like other integers in your code.<br \/>\n<!--more--><br \/>\nFor example:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\n#define POSTDATE 1528527660\r\n\r\nint main()\r\n{\r\n    time_t t;\r\n\r\n    for(t=POSTDATE;t&lt;POSTDATE+600;t+=60)\r\n        printf(\"%s\",ctime(&amp;t));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <code>POSTDATE<\/code> constant expression is set at Line 4, equal to June 16, 2018 midnight. The <em>for<\/em> loop at Line 10 uses <em>time_t<\/em> variable <code>t<\/code> to loop through that value to a <em>time_t<\/em> value 600 seconds in the future, stepping 60 seconds at a time. Yes, this is a valid loop in the C language; <em>time_t<\/em> is a data type just like <em>char<\/em> or <em>int<\/em>.<\/p>\n<p>Here&#8217;s the sample run:<\/p>\n<pre><code>Sat Jun 16 00:01:00 2018\r\nSat Jun 16 00:02:00 2018\r\nSat Jun 16 00:03:00 2018\r\nSat Jun 16 00:04:00 2018\r\nSat Jun 16 00:05:00 2018\r\nSat Jun 16 00:06:00 2018\r\nSat Jun 16 00:07:00 2018\r\nSat Jun 16 00:08:00 2018\r\nSat Jun 16 00:09:00 2018\r\nSat Jun 16 00:10:00 2018<\/code><\/pre>\n<p>I used this type of loop in code I wrote recently. The <em>time_t<\/em> value was part of a data structure, so the loop would recreate data inputs given the time of day and display the results.<\/p>\n<p>You can modify the loop to display intervals greater than 60 seconds. The following code does this task with only a few modifications:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\n#define POSTDATE 1529132400\r\n#define DAY 86400\r\n\r\nint main()\r\n{\r\n    time_t t;\r\n\r\n    for(t=POSTDATE;t&lt;POSTDATE+(DAY*10);t+=DAY)\r\n        printf(\"%s\",ctime(&amp;t));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>In this code, I&#8217;ve changed the <code>POSTDATE<\/code> constant expression to represent midnight, which involves subtracting 60 from the value in the original code. The <code>DAY<\/code> constant expression is set to 86400, the number of seconds in a day.<\/p>\n<pre><code>Sat Jun 16 00:00:00 2018\r\nSun Jun 17 00:00:00 2018\r\nMon Jun 18 00:00:00 2018\r\nTue Jun 19 00:00:00 2018\r\nWed Jun 20 00:00:00 2018\r\nThu Jun 21 00:00:00 2018\r\nFri Jun 22 00:00:00 2018\r\nSat Jun 23 00:00:00 2018\r\nSun Jun 24 00:00:00 2018\r\nMon Jun 25 00:00:00 2018<\/code><\/pre>\n<p>Remember, these <em>time_t<\/em> values are interpreted in the code as epoch values. You can also display the values directly with only one addition to the code:<\/p>\n<p><code>printf(\"%ld: %s\",t,ctime(&amp;t));<\/code><\/p>\n<p>And you get this output:<\/p>\n<pre><code>1529132400: Sat Jun 16 00:00:00 2018\r\n1529218800: Sun Jun 17 00:00:00 2018\r\n1529305200: Mon Jun 18 00:00:00 2018\r\n1529391600: Tue Jun 19 00:00:00 2018\r\n1529478000: Wed Jun 20 00:00:00 2018\r\n1529564400: Thu Jun 21 00:00:00 2018\r\n1529650800: Fri Jun 22 00:00:00 2018\r\n1529737200: Sat Jun 23 00:00:00 2018\r\n1529823600: Sun Jun 24 00:00:00 2018\r\n1529910000: Mon Jun 25 00:00:00 2018<\/code><\/pre>\n<p>The bottom line, and what I took advantage of in my code, is to understand that a <em>time_t<\/em> value is still a data type, and you can use it beneficially as either an epoch value or straight-up integer, depending on the needs of your code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>time_t<\/em> variable type is a number so, yes, I suppose you can stick it into a loop. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3120\">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-3120","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\/3120","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=3120"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3120\/revisions"}],"predecessor-version":[{"id":3158,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3120\/revisions\/3158"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}