-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalender.c
173 lines (163 loc) · 5.45 KB
/
Calender.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Calender.c
*
* Created on: 25 Nov 2016
* Author: David
*/
#include "Calender.h"
/*FUNCTION**********************************************************************
*
* Function Name : cal_HandleCalender
* Description : This function manages and creates a calender display on the
* console. The days of the week are synced to their correct date for any given
* year.
*
*END**************************************************************************/
void cal_HandleCalender(char *inputChar, TimeHolder *myTime,
TimeHolder *alarmTime) {
TimeHolder calenderTime;
char monthNames[13][20] = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December" };
th_SaveTime(myTime, &calenderTime);
while (*inputChar != '0') { // Control for moving forward in time, month by month
if (*inputChar == '2') {
calenderTime.t.month++;
if (calenderTime.t.month > 12) {
calenderTime.t.month = 1;
calenderTime.t.year++;
if (calenderTime.t.year > 2099) {
calenderTime.t.year = 2099;
}
}
} else if (*inputChar == '1') { // Control for moving back in time, month by month
calenderTime.t.month--;
if (calenderTime.t.month < 1) {
calenderTime.t.month = 12;
calenderTime.t.year--;
if (calenderTime.t.year < 1970) {
calenderTime.t.year = 1970;
}
}
}
th_GetWeekday(&calenderTime); // Get the current weekday
VT100_terminalClearFormat(0);
// Display formated calender design on the terminal.
PRINTF("\n\n\r%i\n\r%s\n\n\rM\tT\tW\tT\tF\tS\tS \n\n\r",
calenderTime.t.year, monthNames[calenderTime.t.month - 1]);
for (int i = 0; i < calenderTime.weekday; i++) {
PRINTF("\t");
}
for (int i = 1; i < 32; i++) {
calenderTime.t.day = i;
// Check for the correct time format
if (RTC_HAL_IsDatetimeCorrectFormat(&(calenderTime.t))) {
// Highlight days off such as weekends, bank holidays, special holidays.
if ((calenderTime.weekday + i) % 7 == 6
|| (calenderTime.weekday + i) % 7 == 0) {
VT100_DisplayFRed();
} else if (cal_CheckFixedHoliday(&calenderTime)) {
VT100_DisplayFRed();
} else if (cal_CheckUnfixedHoliday(&calenderTime,
((calenderTime.weekday + i) % 7))) {
VT100_DisplayFRed();
} else {
VT100_DisplayFWhite();
}
if (cal_CheckCurrentDay(&calenderTime, myTime)) {
VT100_DisplayBGreen();
VT100_DisplayBWhite();
} else if (cal_CheckAlarmDay(&calenderTime, alarmTime)) {
VT100_DisplayBYellow();
VT100_DisplayFBlack();
}
PRINTF("%02d\t", calenderTime.t.day);
VT100_DisplayBBlack();
if ((calenderTime.weekday + i) % 7 == 0) {
PRINTF("\n\r");
}
}
}
// Display controls.
VT100_DisplayFGreen();
PRINTF("\n\n\r <---(1\t 0 to Exit \t\t2)--->");
*inputChar = GETCHAR();
}
}
/*FUNCTION**********************************************************************
*
* Function Name : cal_CheckAlarmDay
* Description : This function highlights all the alarm days on the calender.
*
*END**************************************************************************/
bool cal_CheckAlarmDay(TimeHolder *calenderTime, TimeHolder *alarmTime) {
bool returnValue = 0;
if ((calenderTime->t.year == alarmTime->t.year)
&& (calenderTime->t.month == alarmTime->t.month)
&& (calenderTime->t.day == alarmTime->t.day)) {
returnValue = 1;
}
return returnValue;
}
/*FUNCTION**********************************************************************
*
* Function Name : cal_CheckCurrentDay
* Description : This function highlights the current day on the calender.
*
*END**************************************************************************/
bool cal_CheckCurrentDay(TimeHolder *calenderTime, TimeHolder *currentTime) {
bool returnValue = 0;
if ((calenderTime->t.year == currentTime->t.year)
&& (calenderTime->t.month == currentTime->t.month)
&& (calenderTime->t.day == currentTime->t.day)) {
returnValue = 1;
}
return returnValue;
}
/*FUNCTION**********************************************************************
*
* Function Name : cal_CheckUnfixedHoliday
* Description : This function highlights all the unfixed, floating holidays
* days on the calender such as bank holidays.
*
*END**************************************************************************/
bool cal_CheckUnfixedHoliday(TimeHolder *calenderTime, int weekday) {
const int UNFIXED_HOLIDAY_MONTH[] = { 5, 6, 8, 11 };
bool returnValue = 0;
if (calenderTime->t.month == 10) {
if (calenderTime->t.day == 31) {
if (weekday == 1) {
returnValue = 1;
}
}
}
for (int i = 0; i < 4; i++) {
if (calenderTime->t.month == UNFIXED_HOLIDAY_MONTH[i]) {
if (calenderTime->t.day <= 7) {
if (weekday == 1) {
returnValue = 1;
}
}
}
}
return returnValue;
}
/*FUNCTION**********************************************************************
*
* Function Name : cal_CheckFixedHoliday
* Description : This function highlights all the fixed, static holidays
* days on the calender such as christmas Day and St.Patricks Day..
*
*END**************************************************************************/
bool cal_CheckFixedHoliday(TimeHolder *calenderTime) {
const int FIXED_HOLIDAY_MONTH[] = { 1, 3, 12, 12 };
const int FIXED_HOLIDAY_DAY[] = { 1, 17, 25, 26 };
bool returnValue = 0;
for (int i = 0; i < 4; i++) {
if (calenderTime->t.month == FIXED_HOLIDAY_MONTH[i]) {
if (calenderTime->t.day == FIXED_HOLIDAY_DAY[i])
returnValue = 1;
}
}
return returnValue;
}