{"id":217,"date":"2013-08-31T00:01:46","date_gmt":"2013-08-31T08:01:46","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=217"},"modified":"2013-08-24T08:42:26","modified_gmt":"2013-08-24T16:42:26","slug":"the-month-array","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=217","title":{"rendered":"The Month Array"},"content":{"rendered":"<p>Why present the current date as 8\/31\/2013 when you can express it as August 31, 2013? Because some things are easy to program and some are easy for the program&#8217;s user to read.<br \/>\n<!--more--><br \/>\nTake the current date. It&#8217;s a rather simple process to obtain the current date (and time) in C. Well, it may not be simple: I&#8217;m guessing that most programmers just copy the same code over and over, so that process is simple enough.<\/p>\n<p>The following listing shows a sample &#8220;show me the date&#8221; type of program. It first uses the <em>time()<\/em> function to fetch the current Unix Epoch tick count. That value, <code>tictoc<\/code> in the code, is then fed to the <em>localtime()<\/em> function, which fills a <code>tm<\/code> structure with details about the current tick count, i.e., the right now time.<\/p>\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 tictoc;\r\n    struct tm *today;\r\n\r\n    time(&tictoc);          \/* Get the current time *\/\r\n    today = localtime(&tictoc);\r\n                            \/* Read today structure *\/\r\n    printf(\"Today is %d\/%d\/%d\\n\",\r\n        today-&gt;tm_mon+1,\r\n        today-&gt;tm_mday,\r\n        today-&gt;tm_year+1900);\r\n    return(0);\r\n}<\/pre>\n<p>The three key members in the <code>tm<\/code> structure, referenced by the variable <code>today<\/code> in the code above, are <code>tm_mon<\/code>, <code>tm_mday<\/code>, and <code>tm_year<\/code>.<\/p>\n<p>The <code>tm_mon<\/code> member represents the current month. Its values range from 0 through 11, not the 1 through 12 you would expect. Therefore, the <em>printf()<\/em> statement adds one to the value at Line 13.<\/p>\n<p>The <code>tm_mday<\/code> member represents the current day of the month, ranging from 1 through 31. Of course, the top value is limited based on the current month.<\/p>\n<p>The <code>tm_year<\/code> member represents the current year, minus 1900. That 1900 is added to the value at Line 15. (If you wanted to display the year as a two-digit value, you&#8217;d subtract 100. I suppose the original purpose of the <code>tm_year<\/code> member was to display a two-digit year, which works fine for the 20th Century, but not for the 21st.)<\/p>\n<p>Here&#8217;s the sample output:<\/p>\n<p><code>Today is 8\/31\/2013<\/code><\/p>\n<p>And, of course, if you&#8217;re in a locale where the day comes before the month, just swap the variables at Lines 13 and 14 in the code:<\/p>\n<p><code>Today is 31\/8\/2013<\/code><\/p>\n<p>To display the month as a text string, such as &#8220;August&#8221; instead of 8, you need to add an array of month strings. Such an addition is shown in the the following modification to the original code:<\/p>\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 tictoc;\r\n    struct tm *today;\r\n    char *months[12] = {\r\n        \"January\",\r\n        \"February\",\r\n        \"March\",\r\n        \"April\",\r\n        \"May\",\r\n        \"June\",\r\n        \"July\",\r\n        \"August\",\r\n        \"September\",\r\n        \"October\",\r\n        \"November\",\r\n        \"December\" };\r\n\r\n    time(&tictoc);          \/* Get the current time *\/\r\n    today = localtime(&tictoc);\r\n                            \/* Read today structure *\/\r\n    printf(\"Today is %s %d, %d\\n\",\r\n        months[today-&gt;tm_mon],\r\n        today-&gt;tm_mday,\r\n        today-&gt;tm_year+1900);\r\n    return(0);\r\n}<\/pre>\n<p>I&#8217;ve changed only two items between the first and second code listings.<\/p>\n<p>First is the addition of the string array at Line 8. The <code>char *months[12]<\/code> declaration tells the compiler that 12 strings are being created. Each string is then specified on its own line. The compiler calculates the string&#8217;s lengths and all that. This type of declaration is simpler than doing <code>char months[12][10]<\/code>, which would be another way to express such an allocation.<\/p>\n<p>Second, the <em>printf()<\/em> statement has been modified. The <code>%s<\/code> conversion character is used to display the month string. The <code>tm_mon<\/code> member is used, but it doesn&#8217;t need to be incremented by 1; in the <code>months<\/code> array, element 0 is &#8220;January,&#8221; so everything works out fine. I&#8217;ve also changed the format so that the output looks like this:<\/p>\n<p><code>Today is August 31, 2013.<\/code><\/p>\n<p>The point to all this is that it&#8217;s not really that difficult to switch from an all digit representation of the current day to a more readable, human representation. All you need to do is add an array of strings representing the months, then use the fact that the <code>tm<\/code> structure references months starting at 0 to help the code fetch the proper month string from that array.<\/p>\n<p>If you&#8217;re willing to play with this concept further, know that the <code>tm_wday<\/code> member of the <code>tm<\/code> structure contains the current day of the week, where 0 represents Sunday.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An array of month strings can help your code better display the current date. Not only does it make the date more readable, it&#8217;s not a bear to program. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=217\">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-217","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\/217","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=217"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/217\/revisions"}],"predecessor-version":[{"id":263,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/217\/revisions\/263"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}