{"id":529,"date":"2014-02-01T00:01:58","date_gmt":"2014-02-01T08:01:58","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=529"},"modified":"2014-08-30T17:56:41","modified_gmt":"2014-08-31T00:56:41","slug":"the-month-program-phase-v","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=529","title":{"rendered":"The Month Program, Phase V"},"content":{"rendered":"<p>It&#8217;s all come to this: Gathering up the individual functions necessary to code a program that outputs the current month, formatted, with the proper number of days for February. Coincidentally, this post is published in February.<br \/>\n<!--more--><br \/>\nHere was my original request, from the January 4, 2014 lesson:<\/p>\n<blockquote><p>I want the monthly calendar output to highlight the current day of the month.<\/p><\/blockquote>\n<p>The process I&#8217;ve shown over the past five weeks illustrates how a programmer would tackle such a problem:<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=414\">Phase I:<\/a> Figure out today&#8217;s date. You need that information primarily to obtain the current year and month, but also to help calculate the first day of the month.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=451\">Phase II:<\/a> Determine on which day of the week is the first of the month. This calculation requires knowing the current day (Phase I), and it&#8217;s necessary when you want to format a calendar&#8217;s output.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=456\">Phase III:<\/a> Once you have the first day of the month, the process of creating a calendar involves creating formatted output. The big issue then is knowing on which day of the month you stop displaying the calendar.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=522\">Phase IV:<\/a> Next you have to deal with the February problem. Twenty five percent of the time February could be 29 days long instead of 28. That step involves creating a special days-of-the-month function.<\/p>\n<p>Phase V (this Lesson) brings it all together: The calendar output now contains a function to calculate the number of days in the month, including February. I also added one more element, which is to highlight the current day. That code is rather easy to implement: In the example below, an <em>if<\/em> statement at Line 47 determines whether <code>day<\/code> (the day being output for the calendar) is equal to <code>mday<\/code> (the current day of the month). If so, the <em>printf()<\/em> function displays the date with square brackets around the number (Line 48).<\/p>\n<p>Here&#8217;s the final code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint thefirst(int weekday, int monthday);\r\nint daysInMonth(int month,int year);\r\n\r\nint main()\r\n{\r\n    time_t tictoc;\r\n    struct tm *rightnow;\r\n    int wday,mday,month,year,firstdom,dim,day,d;\r\n    char *months[12] = {\r\n        \"January\", \"February\", \"March\", \"April\",\r\n        \"May\", \"June\", \"July\", \"August\",\r\n        \"September\", \"October\", \"November\", \"December\" };\r\n\r\n    time(&amp;tictoc);                  \/* get Unix epoch time *\/\r\n                                    \/* fill tm struct rightnow *\/\r\n    rightnow = localtime(&amp;tictoc);\r\n                                    \/* Get info from struct *\/\r\n    wday = rightnow-&gt;tm_wday;        \/* 0=Sunday *\/\r\n    mday = rightnow-&gt;tm_mday;        \/* 1 to 31 *\/\r\n    month = rightnow-&gt;tm_mon;        \/* 0=January *\/\r\n    year = rightnow-&gt;tm_year+1900;   \/* current year - 1900 *\/\r\n\r\n\/* Display Calendar Header *\/\r\n    printf(\"%s %d\\n\",months[month],year);\r\n    printf(\"Sun Mon Tue Wed Thu Fri Sat\\n\");\r\n\r\n\/* Prep starting variables *\/\r\n    firstdom = thefirst(wday,mday);\r\n    dim = daysInMonth(month,year);\r\n    day = 1;                            \/* Day of the month *\/\r\n\/* Display the days of the month *\/\r\n    while( day &lt;= dim )\r\n    {\r\n        for( d = 0; d &lt; 7; d++)\r\n        {\r\n            \/* Test for the first week of the month *\/\r\n            if( d &lt; firstdom && day == 1 )\r\n            {\r\n                printf(\"    \");         \/* blank date, 4 spaces *\/\r\n            }\r\n            else\r\n            {\r\n                                        \/* highlight current day *\/\r\n                if( day == mday )\r\n                    printf(\"[%2d]\",day);\r\n                else\r\n                    printf(\" %2d \",day);\r\n                                        \/* check the next day *\/\r\n                day++;\r\n                if( day &gt; dim )\r\n                    break;\r\n            }\r\n        }\r\n        putchar('\\n');\r\n    }\r\n\r\n    return(0);\r\n}\r\n\r\n\/* Calculate First Day of the Month. *\/\r\nint thefirst(int weekday, int monthday)\r\n{\r\n    int first;\r\n\r\n    first = weekday - (monthday % 7) + 1;\r\n    first %= 7;\r\n    if( first &lt; 0 )\r\n        first+=7;\r\n\r\n    return(first);\r\n}\r\n\r\n\/* Determine the number of days in the month *\/\r\nint daysInMonth(int month,int year)\r\n{\r\n    int month_length[] = {\r\n        31, 28, 31, 30, 31, 30,\r\n        31, 31, 30, 31, 30, 31 };\r\n\r\n    if(month != 1)\r\n        return(month_length[month]);\r\n\r\n    if( year % 4 )\r\n        return(28);\r\n    if( !(year % 100))\r\n    {\r\n        if( year % 400 )\r\n            return(28);\r\n        else\r\n            return(29);\r\n    }\r\n    return(29);\r\n}<\/pre>\n<p>Here&#8217;s the sample output (for today, February 1, 2014):<\/p>\n<pre><code>February 2014\r\nSun Mon Tue Wed Thu Fri Sat\r\n                        [ 1]\r\n  2   3   4   5   6   7   8 \r\n  9  10  11  12  13  14  15 \r\n 16  17  18  19  20  21  22 \r\n 23  24  25  26  27  28 <\/code><\/pre>\n<p>One more addition you could make would be to center the month and year at the top of the calendar output. That involves a few tricks, but mostly pulling in your solution to <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=431\">January&#8217;s Exercise<\/a> on centering text.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The final in the Month Program series, here is the end result, a program that displays the current month and highlights the current day. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=529\">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-529","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\/529","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=529"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/529\/revisions"}],"predecessor-version":[{"id":961,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/529\/revisions\/961"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}