Create data structures for a calendar page view.

Dependents:   TimeInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CalendarPage.cpp Source File

CalendarPage.cpp

00001 
00002 
00003 #include "CalendarPage.h"
00004 
00005 
00006 CalendarPage::CalendarPage(uint8_t Month, uint16_t Year) {
00007     month = Month;
00008     year = Year;
00009     verbose = false;
00010     Compute(month, year);
00011 }
00012 
00013 
00014 // 37 Elements with Array index 0 to 36,
00015 //    populated with 0 or the day number 
00016 //
00017 //  0  1  2  3  4  5  6     |                       1
00018 //  7  8  9 10 11 12 13     |     2  3  4  5  6  7  8
00019 // 14 15 16 17 18 19 20     |     9 10 11 12 13 14 15
00020 // 21 22 23 24 25 26 27     |    16 17 18 19 20 21 22
00021 // 28 29 30 31 32 33 34     |    23 24 25 26 27 28 29
00022 // 35 36                    |    30 31
00023 //
00024 void CalendarPage::Compute(uint8_t Month, uint16_t Year) {
00025     const int DaysInMonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
00026     const char * MonthName[] = { "", "January", "February", "March", "April", "May", "June",
00027         "July", "August", "September", "October", "November", "December" };
00028     const char * DayName[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
00029     int NumberOfDaysInMonth;
00030     int FirstDayOfMonth = 0;
00031     int DayOfWeekCounter = 0;
00032     int DateCounter = 1;
00033     int index = 0;
00034     int day = 1;
00035 
00036     month = Month;
00037     year = Year;
00038     int y = year - (14 - month) / 12;
00039     int m = month + 12 * ((14 - month) / 12) - 2;
00040 
00041     firstday = (day + y + y / 4 - y / 100 + y / 400 + (31 * m / 12)) % 7;
00042     if ( (month == 2) && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) ) {
00043         NumberOfDaysInMonth = 29;
00044     } else {
00045         NumberOfDaysInMonth = DaysInMonth[month];
00046     }
00047 
00048     if (verbose) {
00049         printf("%20s %d\r\n", MonthName[month], year);
00050         for (int d = 0; d < 7; d++) {
00051             printf("%4s", DayName[d]);
00052         }
00053         printf("\r\n");
00054     }
00055 
00056     memset(DayMap, 0, sizeof(DayMap));
00057     for (FirstDayOfMonth = 0; FirstDayOfMonth < firstday; ++FirstDayOfMonth) {
00058         if (verbose)
00059             printf("%4s", "");
00060         DayMap[index++] = 0;
00061     }
00062 
00063     int tempfirstday = firstday;
00064     DateCounter = 1;
00065     DayOfWeekCounter = tempfirstday;
00066     //This loop represents the date display and will continue to run until
00067     //the number of days in that month have been reached
00068     for (DateCounter = 1; DateCounter <= NumberOfDaysInMonth; ++DateCounter) {
00069         DayMap[index++] = DateCounter;
00070         if (verbose)
00071             printf("%4d", DateCounter);
00072         ++DayOfWeekCounter;
00073         if (DayOfWeekCounter > 6 && DateCounter != NumberOfDaysInMonth) {
00074             if (verbose) 
00075                 printf("\r\n");
00076             DayOfWeekCounter = 0;
00077         }
00078     }
00079     if (verbose)
00080         printf("\r\n");
00081     tempfirstday = DayOfWeekCounter + 1;
00082 }