{"id":5865,"date":"2023-05-01T00:01:22","date_gmt":"2023-05-01T07:01:22","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5865"},"modified":"2023-05-10T07:29:23","modified_gmt":"2023-05-10T14:29:23","slug":"yesterday","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5865","title":{"rendered":"Yesterday"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9734; &#9734;<\/h2>\n<p>The <code>time.h<\/code> header defines a handful of functions useful for discovering and manipulating today&#8217;s date. Especially the <em>localtime()<\/em> function, which translates a <em>time_t<\/em> (Unix Epoch) value into date fields for output or manipulation. It&#8217;s a wonderful tool, but what does it tell you about yesterday?<br \/>\n<!--more--><br \/>\nYour task for this month&#8217;s Exercise is to write code to output yesterday&#8217;s date. This seems like a trivial undertaking, but it might be more complex than you can imagine: Obtaining yesterday&#8217;s date isn&#8217;t as simple as subtracting &#8220;one&#8221; from today&#8217;s day-of-the-month value. For example, if today&#8217;s the first, yesterday wasn&#8217;t the zeroth.<\/p>\n<p>To get you started, I&#8217;m providing the following code skeleton. It only outputs today&#8217;s date, but hopefully it offers enough support to help you generate and output yesterday&#8217;s date as well.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_05_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2023_05_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 main()\r\n{\r\n    time_t now;\r\n    struct tm *today;\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\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s the code&#8217;s output:<\/p>\n<p><code>Today is Monday, 1 May 2023<\/code><\/p>\n<p>And here is the output from my solution:<\/p>\n<p><code>Today is Monday, 1 May 2023<br \/>\nYesterday was Sunday, 30 April 2023<\/code><\/p>\n<p>I&#8217;m eager to see how you solve this challenge. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5870\">My solution<\/a> doesn&#8217;t cheat, but it&#8217;s close!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s easy to obtain time information about right now, today. But what about yesterday? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5865\">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-5865","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\/5865","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=5865"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5865\/revisions"}],"predecessor-version":[{"id":5887,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5865\/revisions\/5887"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}