{"id":4429,"date":"2020-11-01T00:01:08","date_gmt":"2020-11-01T07:01:08","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4429"},"modified":"2020-11-08T08:35:00","modified_gmt":"2020-11-08T16:35:00","slug":"calculating-months","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4429","title":{"rendered":"Calculating Months"},"content":{"rendered":"<p>I recently updated my <a href=\"https:\/\/www.wambooli.com\/\" rel=\"noopener noreferrer\" target=\"_blank\">Wambooli website&#8217;s front page<\/a> to show only recent blog posts. I don&#8217;t regularly maintain the Wamblog any longer, so it was disappointing for me to see &#8220;recent posts&#8221; from ages ago. Therefore, I did a little coding to ensure that only recent posts appear or nothing.<br \/>\n<!--more--><br \/>\nWhat I wanted was to show only blog posts made within the past two months. The problem is to determine which posts are recent based on today&#8217;s date. While I wrote the code in PHP, it translates well into C: The core of the code is to get the sequential number for the current month.<\/p>\n<p>For example, this month (November) is the 24,251<sup>st<\/sup> month since year zero:<\/p>\n<p><code>2020 * 12 + 11<\/code><\/p>\n<p>I&#8217;m not considering any oddball years when months were skipped or added. This detail isn&#8217;t necessary for my calculation.<\/p>\n<p>After determining the current month number, I was able to use the same function on a blog post&#8217;s timestamp. Subtract one from the other, and if the difference is less than two months, it&#8217;s a &#8220;recent&#8221; blog post.<\/p>\n<p>Your challenge for this month&#8217;s Exercise is to reproduce my <em>monthcount()<\/em> function. Given the current date or any date stored in a <em>time_t<\/em> variable, return an integer representing the current month count. Here is a programming skeleton to get you started:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_11_01-Lesson.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_11_01-Lesson.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}\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>In my sample code, I obtain the count for the current month, though you can supply any <em>time_t<\/em> value as the function&#8217;s argument. The integer value returned is the month count. The solution involves manipulating the <em>time_t<\/em> value to extract years and months, doing a little math, then sending back the result.<\/p>\n<p>Please try this Exercise on your own before you check out <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4441\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is month number 24251. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4429\">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":[3],"tags":[],"class_list":["post-4429","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4429","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=4429"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4429\/revisions"}],"predecessor-version":[{"id":4467,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4429\/revisions\/4467"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4429"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4429"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}