{"id":414,"date":"2014-01-04T00:01:35","date_gmt":"2014-01-04T08:01:35","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=414"},"modified":"2014-01-18T09:09:00","modified_gmt":"2014-01-18T17:09:00","slug":"the-month-program-phase-i","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=414","title":{"rendered":"The Month Program, Phase I"},"content":{"rendered":"<p>Unix features a handy command line tool called <em>cal<\/em>. When typed by its lonesome, <em>cal<\/em> spits up a text calendar on the terminal window. Or you can follow <em>cal<\/em> with a month and year value to see a specific month, or just the year value to see a year&#8217;s calendar. It&#8217;s nifty!<br \/>\n<!--more--><br \/>\nHere&#8217;s the output of the <em>cal<\/em> command for this month, January 2014:<\/p>\n<pre><code>    January 2014\r\nSu Mo Tu We Th Fr Sa\r\n          1  2  3  4\r\n 5  6  7  8  9 10 11\r\n12 13 14 15 16 17 18\r\n19 20 21 22 23 24 25\r\n26 27 28 29 30 31<\/code><\/pre>\n<p>I use this command in my always-open terminal window as a quick way to check dates. It&#8217;s much faster than slugging my way through a GUI calendar or scheduling app. It&#8217;s even faster than flying in the Dashboard in OS X.<\/p>\n<p>But I want more!<\/p>\n<p>Specifically, I want the monthly calendar output to highlight the current day of the month.<\/p>\n<p>Ideally, it would look like this:<\/p>\n<pre><code>    January 2014\r\nSu Mo Tu We Th Fr Sa\r\n          1  2  3 <span style=\"background-color:blue;color:white\"> 4<\/span>\r\n 5  6  7  8  9 10 11\r\n12 13 14 15 16 17 18\r\n19 20 21 22 23 24 25\r\n26 27 28 29 30 31<\/code><\/pre>\n<p>Such text coloring is possible by programming the terminal directly, using ANSI codes, or using the <a href=\"http:\/\/c-for-dummies.com\/ncurses\/\">NCurses library<\/a>. Rather than muddy the waters, I&#8217;d settle for this type of output:<\/p>\n<pre><code>    January 2014\r\nSu Mo Tu We Th Fr Sa\r\n          1  2  3[ 4]\r\n 5  6  7  8  9 10 11\r\n12 13 14 15 16 17 18\r\n19 20 21 22 23 24 25\r\n26 27 28 29 30 31<\/code><\/pre>\n<p>Above, I&#8217;m using square brackets to enclose the current day. That keeps with the text-only type of output C does, plus it&#8217;s a bit easier to code.<\/p>\n<p>Of course, it&#8217;s only easier when you can generate the current month in the first place. To do so, you have to really start by determining which day of the week is the first day of the current month. For that task, you need to use your standard C library toolbox and the <em>localtime()<\/em> function.<\/p>\n<p>The <em>localtime()<\/em> function (prototyped in the <code>time.h<\/code> header) fills a <code>tm<\/code> structure with information about a given Unix Epoch time value, also known as a <code>time_t<\/code> value. Normally the value of <code>time_t<\/code> reflects the current date and time, which is fetched by using the <em>time()<\/em> function. You can also backfill that structure to fetch information about any specific date, which was demonstrated in a <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=37\">previous Lesson<\/a>.<\/p>\n<p>As a reference, here is the information found in the typical <code>tm<\/code> structure:<\/p>\n<pre><code>int tm_sec;     \/* seconds (0 - 60) *\/\r\nint tm_min;     \/* minutes (0 - 59) *\/\r\nint tm_hour;    \/* hours (0 - 23) *\/\r\nint tm_mday;    \/* day of month (1 - 31) *\/\r\nint tm_mon;     \/* month of year (0 - 11) *\/\r\nint tm_year;    \/* year - 1900 *\/\r\nint tm_wday;    \/* day of week (Sunday = 0) *\/\r\nint tm_yday;    \/* day of year (0 - 365) *\/\r\nint tm_isdst;   \/* is summer time in effect? *\/\r\nchar *tm_zone;  \/* abbreviation of timezone name *\/\r\nlong tm_gmtoff; \/* offset from UTC in seconds *\/\r\n<\/code><\/pre>\n<p>Some library implementations add more members, but what you see above is pretty common.<\/p>\n<p>The sample code below uses the <code>tm<\/code> structure to display three relevant tidbits of information: The current day of the week, the current month, and the current day of the month.<\/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 *rightnow;\r\n    int wday,monthday,month;\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    char *weekdays[7] = {\r\n        \"Sunday\", \"Monday\", \"Tuesday\", \"Wednesday\",\r\n        \"Thursday\", \"Friday\", \"Saturday\" };\r\n\r\n    time(&tictoc);                  \/* get Unix epoch time *\/\r\n                                    \/* fill tm struct rightnow *\/\r\n    rightnow = localtime(&tictoc);\r\n                                    \/* Get info from struct *\/\r\n    wday = rightnow-&gt;tm_wday;       \/* 0=Sunday *\/\r\n    monthday = rightnow-&gt;tm_mday;   \/* 1 to 31 *\/\r\n    month = rightnow-&gt;tm_mon;       \/* 0=January *\/\r\n\r\n    printf(\"Today is %s %s %d\\n\",\r\n        weekdays[wday],\r\n        months[month],\r\n        monthday);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Sample output from this code depends on the current date. Here&#8217;s what I see:<\/p>\n<p><code>Today is Saturday January 4<\/code><\/p>\n<p>Once the program knows the current day of the month, the next step is to concoct an algorithm to determine on which day of the week the first would fall. You have all the information to do it on your own, or you can wait a week and continue with the <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=451\">next Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The process of displaying the current month starts by fetching today&#8217;s date. You need three tidbits of information: The day of the week, the month, and the day of the month. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=414\">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-414","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\/414","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=414"}],"version-history":[{"count":14,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/414\/revisions"}],"predecessor-version":[{"id":535,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/414\/revisions\/535"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}