{"id":5870,"date":"2023-05-08T00:01:50","date_gmt":"2023-05-08T07:01:50","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5870"},"modified":"2023-05-06T10:25:20","modified_gmt":"2023-05-06T17:25:20","slug":"yesterday-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5870","title":{"rendered":"Yesterday &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5865\">this month&#8217;s Exercise<\/a> is to output yesterday&#8217;s date: the day of the week, day of the month, month, and year. It&#8217;s not as complex as it could be, though keep in mind that any code that outputs the proper result is valid.<br \/>\n<!--more--><br \/>\nMy initial approach to solving this challenge was to use the <em>localtime()<\/em> function to obtain today&#8217;s data, then work backwards to retrieve yesterday&#8217;s data. You know: Subtract one from the day of the month, but then test to see if the result is zero and then adjust the other values accordingly. Such a solution is valid, but it&#8217;s a lot of work.<\/p>\n<p>Instead, I considered the sole argument to the <em>localtime()<\/em> function: a <em>time_t<\/em> value. This value represents the number of seconds elapsed since the Unix Epoch of January 1, 1970. After obtaining today&#8217;s <em>time_t<\/em> value in the code, the sneaky way to obtain yesterday&#8217;s data is to subtract one day&#8217;s worth of seconds from the result, then call the <em>localtime()<\/em> function again with the updated <em>time_t<\/em> value.<\/p>\n<p>One day&#8217;s worth of seconds is 60 * 60 * 24, or seconds_in_a_minute &times; minutes_in_an_hour &times; hours_in_a_day.<\/p>\n<p>Here is my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_05-Exercise.c\" rel=\"noopener\" target=\"_blank\">2023_05-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 main()\r\n{\r\n    time_t now;\r\n    struct tm *today,*yesterday;\r\n    const char *day[] = { \"Sunday\", \"Monday\",\r\n        \"Tuesday\", \"Wednesday\", \"Thursday\",\r\n        \"Friday\", \"Saturday\" };\r\n    const char *month[] = { \"January\", \"February\",\r\n        \"March\", \"April\", \"May\", \"June\", \"July\",\r\n        \"August\", \"September\", \"October\",\r\n        \"November\", \"December\" };\r\n\r\n    <span class=\"comments\">\/* get today's date *\/<\/span>\r\n    time(&amp;now);\r\n    today = localtime(&amp;now);\r\n    printf(\"Today is %s, %d %s %d\\n\",\r\n            day[today-&gt;tm_wday],\r\n            today-&gt;tm_mday,\r\n            month[today-&gt;tm_mon],\r\n            today-&gt;tm_year+1900\r\n          );\r\n\r\n    <span class=\"comments\">\/* output yesterday's date *\/<\/span>\r\n    now -= 60*60*24;\r\n    yesterday = localtime(&amp;now);\r\n    printf(\"Yesterday was %s, %d %s %d\\n\",\r\n            day[yesterday-&gt;tm_wday],\r\n            yesterday-&gt;tm_mday,\r\n            month[yesterday-&gt;tm_mon],\r\n            yesterday-&gt;tm_year+1900\r\n          );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>After today&#8217;s information is obtained, I subtract one day&#8217;s worth of seconds from variable <code>now<\/code> (today&#8217;s date and time). The <em>localtime()<\/em> function is called again with this updated <em>time_t<\/em> value. The results are saved in the new <em>tm<\/em> structure <code>yesterday<\/code>. The structure&#8217;s data is then output:<\/p>\n<p><code>Today is Monday, 8 May 2023<br \/>\nYesterday was Sunday, 7 May 2023<\/code><\/p>\n<p>The more complex approach of manipulating various <em>tm<\/em> structure members is also valid, though not as brief as the solution above. As long as the output is accurate, consider that you passed the challenge.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to output yesterday&#8217;s date: the day of the week, day of the month, month, and year. It&#8217;s not as complex as it could be, though keep in mind that any code that outputs &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5870\">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":[5],"tags":[],"class_list":["post-5870","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\/5870","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=5870"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5870\/revisions"}],"predecessor-version":[{"id":5880,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5870\/revisions\/5880"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}