{"id":4616,"date":"2021-02-20T00:01:49","date_gmt":"2021-02-20T08:01:49","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4616"},"modified":"2021-02-27T09:35:18","modified_gmt":"2021-02-27T17:35:18","slug":"creating-a-time_t-value","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4616","title":{"rendered":"Creating a <em>time_t<\/em> Value"},"content":{"rendered":"<p>Ho boy! This <em>time_t<\/em> project turned out to be a lot more complex than I thought it would be!<\/p>\n<p>The problem: Count the number of seconds elapsed from January 1, 1970 to a given year, month, and day. This task is easy for a computer, but not that easy to code if you want to get the value correct.<br \/>\n<!--more--><br \/>\nThe number of seconds elapsed from January 1, 1970, the Unix Epoch, is stored in a <em>time_t<\/em> data type. This <em>typedef<\/em> is created in the <code>time.h<\/code> header file and used by various C library time functions.<\/p>\n<p>Providing that you know the year, month, day, hour, minute, and second, you can perform the math necessary to calculate a <em>time_t<\/em> value. Because this value is the number of seconds elapsed from January 1, 1970, the math works this way for a given year, month, day, hour, minute, and seconds:<\/p>\n<p><code>(Year - 1970) * month * day * hour * minute + seconds<\/code><\/p>\n<p>The first issue to contend with is that zero values can mess up multiplication. For example, January is month zero. For dates in January, the calculation&#8217;s result is zero plus the seconds value, which is way off.<\/p>\n<p>The second issue, which was the biggest issue for me, is how to deal with leap years? Each day is 86,400 seconds. Missing a few February 29ths over the years adds up quickly.<\/p>\n<p>To best deal with both issues, I crafted the <em>epoch()<\/em> function. Specifically with regards to <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4590\">last week&#8217;s Lesson<\/a>, I wanted only the <em>time_t<\/em> value that gives me the current day at midnight. Therefore the hour, minute, and second values are zero; the function deals with only the year, month, and day:<\/p>\n<p><code>time_t epoch(int year, int mon, int day)<\/code><\/p>\n<p>Rather than have code multiply each year by 365, I decided to count months instead: A loop would progress from 1970 through the year before the current one, with a nested loop accumulating days of the month. The <code>days<\/code> variable tracks the days in the month for each month within each year, the <code>month[]<\/code> array looks like this:<\/p>\n<p><code>int month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };<\/code><\/p>\n<p>Here is the nested loop to count days in a year, including leap years:<\/p>\n<pre class=\"screen\">\r\n    <span class=\"comments\">\/* calculate the number of days *\/<\/span>\r\n    days = 0;\r\n    for( y=1970; y&lt;year; y++ )\r\n    {\r\n        for( m=0; m&lt;12; m++ )\r\n        {\r\n            if( m==1 )        <span class=\"comments\">\/* february *\/<\/span>\r\n                days += february(y);\r\n            else\r\n                days += month[m];\r\n        }\r\n    }<\/pre>\n<p>For the second month, <code>if( m==1 )<\/code>, the <em>february()<\/em> function is called. This function returns the values 28 or 29 based on the year. It&#8217;s inspired by an old Exercise challenge, with the solution available <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3134\">here<\/a>.<\/p>\n<p>After the years are tallied, another loop (not shown above) adds days from the months so far in the current year. Then finally, the day of the current month is added. This total, stored in <em>int<\/em> variable <code>days<\/code>, is multiplied by <code>24 * 60 * 60<\/code>, or 86,400, to obtain what I hope is the proper <em>time_t<\/em> value.<\/p>\n<p>Alas my code still had issues, which I get into in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4628\">next week&#8217;s Lesson<\/a>. Until then, you can <a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_02_27-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">click here<\/a> to preview the code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>time()<\/em> function creates a <em>time_t<\/em> value for you, though if you&#8217;re crazy enough the operation can be done manually. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4616\">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-4616","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\/4616","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=4616"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4616\/revisions"}],"predecessor-version":[{"id":4646,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4616\/revisions\/4646"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}