]> git.piffa.net Git - arduino/blob - books/beginning_c_code/Chapter12/Dates/Dates.cpp
first commit
[arduino] / books / beginning_c_code / Chapter12 / Dates / Dates.cpp
1 #include "Arduino.h"\r
2 #include "Dates.h"\r
3 \r
4 /*****\r
5   Purpose: Determine if a given year is a leap year. Algorithm \r
6            taken from C Programmer's Toolkit, Jack Purdum, Que \r
7            Corp., 1993, p.258.\r
8 \r
9   Parameters:\r
10     int year            The year to test \r
11 \r
12   Return value:\r
13     int                 1 if the year is a leap year, 0 otherwise\r
14 *****/\r
15 int Dates::IsLeapYear(int year)\r
16 {\r
17   if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {\r
18     return 1;   // It is a leap year\r
19   } else {\r
20     return 0;   // not a leap year\r
21   }\r
22 }\r
23 \r
24 /*****\r
25   Purpose: Determine the date for Easter for a given year. \r
26            Algorithm taken from Beginning Object Oriented               \r
27            Programming with C#, Jack Purdum, Wrox, 2012. \r
28 \r
29   Parameters:\r
30     struct easter *myData    Pointer to an easter structure\r
31 \r
32   Return value:\r
33     void                \r
34     \r
35   CAUTION: This function assumes that the year member of the structure holds the\r
36            year being tested upon entry.        \r
37 *****/\r
38 \r
39 void Dates::GetEaster(Dates *myData){ // This is line 44\r
40   int offset;\r
41   int leap;\r
42   int day;\r
43   int temp1, temp2, total;\r
44 \r
45   myData->myEaster.easterStr[0] = '0';    // Always a '0'\r
46   myData->myEaster.easterStr[2] = '/';    // Always a '/'\r
47   myData->myEaster.easterStr[3] = '0';    // Assume day is less than 10\r
48   myData->myEaster.easterStr[10] = '\0';  // null char for End of string\r
49   \r
50   offset = myData->myEaster.year % 19;\r
51   leap = myData->myEaster.year % 4;\r
52   day = myData->myEaster.year % 7;\r
53   temp1 = (19 * offset + 24) % 30;\r
54   temp2 = (2 * leap + 4 * day + 6 * temp1 + 5) % 7;\r
55   total = (22 + temp1 + temp2);\r
56   if (total > 31) {\r
57     myData->myEaster.easterStr[1] = '4';    // Must be in April\r
58     myData->myEaster.month = 4;             // Save the month\r
59     day = total - 31;\r
60   } else {\r
61     myData->myEaster.easterStr[1] = '3';    // Must be in March\r
62     myData->myEaster.month = 3;             // Save the month\r
63     day = total;\r
64   }\r
65   myData->myEaster.day = day;              // Save the day\r
66   \r
67   if (day < 10) {                 // One day char or two?\r
68     myData->myEaster.easterStr[4] = (char) (day + ASCIIZERO);\r
69   } else {\r
70     itoa(day, myData->myEaster.easterStr + 3, 10);  // Convert day to ASCII and...\r
71   }\r
72   myData->myEaster.easterStr[5] = '/';     // Always a '/' and overwrites null from itoa()\r
73   itoa(myData->myEaster.year, myData->myEaster.easterStr + 6, 10);    // Convert year to ASCII...\r
74 }\r