{"id":4441,"date":"2020-11-08T00:01:44","date_gmt":"2020-11-08T08:01:44","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4441"},"modified":"2020-11-07T08:47:34","modified_gmt":"2020-11-07T16:47:34","slug":"calculating-months-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4441","title":{"rendered":"Calculating Months &#8211; Solution"},"content":{"rendered":"<p>The task for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4429\">this month&#8217;s Exercise<\/a> is to return the month number for a <em>time_t<\/em> value. Effectively you extract the year and month, do some math, presto. Could it be this easy?<br \/>\n<!--more--><br \/>\nThe key is to extract the month and year values from a <em>time_t<\/em> variable passed to the <em>monthcount()<\/em> function, as presented in the original post. And &mdash; this part is important &mdash; converting these values into their accurate representations.<\/p>\n<p>Remember, a <em>time_t<\/em> variable stores years starting at 1901 and months starting with 0 for January. Here is my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_11-Exercise.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_11-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint monthcount(time_t date)\r\n{\r\n    struct tm *dstruct;\r\n    int months;\r\n\r\n    dstruct = localtime(&amp;date);\r\n    <span class=\"comments\">\/* year is dstruct-&gt;tm_year + 1900 *\/<\/span>\r\n    <span class=\"comments\">\/* month is dstruct-&gt;tm_mon + 1 *\/<\/span>\r\n    months = (dstruct-&gt;tm_year+1900) * 12;\r\n    months += dstruct-&gt;tm_mon+1;\r\n\r\n    return(months);\r\n}\r\n\r\nint main()\r\n{\r\n    int month;\r\n    time_t now;\r\n\r\n    time(&amp;now);                    <span class=\"comments\">\/* get the current date\/time *\/<\/span>\r\n    month = monthcount(now);    <span class=\"comments\">\/* calculate total number of months *\/<\/span>\r\n    printf(\"This month is number %d\\n\",month);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>At Line 9, I use the <em>localtime()<\/em> function to fill a <em>tm<\/em> structure, <code>dstruct<\/code>, with details from the <em>time_t<\/em> value, <code>date<\/code>.<\/p>\n<p>At Line 12, variable <code>months<\/code> is calculated as the current year multiplied by 12: <code>months = (dstruct-&gt;tm_year+1900) * 12;<\/code>. This value is increased by the current month value, plus one, to obtain the month count at Line 13. The result is returned at Line 15.<\/p>\n<p>Here is a sample run (for today, 8 November 2020):<\/p>\n<p><code>This month is number 24251<\/code><\/p>\n<p>In my PHP code, I used this value to compare with the timestamp on a post. If the timestamp&#8217;s <em>monthcount()<\/em> return value was 2 less, the post isn&#8217;t listed. This technique is how I&#8217;m able to display only current posts. It&#8217;s a specific solution, but one that provided an interesting programming challenge.<\/p>\n<p>I hope your solution meets with a good result. It&#8217;s possible to extract such information directly from a <em>time_t<\/em> value, though remember <em>time_t<\/em> is a cumulative total of seconds and not a bit field.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The task for this month&#8217;s Exercise is to return the month number for a time_t value. Effectively you extract the year and month, do some math, presto. Could it be this easy?<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-4441","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4441","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=4441"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4441\/revisions"}],"predecessor-version":[{"id":4463,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4441\/revisions\/4463"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}