Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
camelCase: Converted remaining code that was missed
[xestiacalendar/.git] / source / common / monthviewgen.cpp
1 // monthviewgen.cpp - Xestia Calendar month view generation functions.
2 //
3 // (c) 2016-2017 Xestia Software Development.
4 //
5 // This file is part of Xestia Calendar.
6 //
7 // Xestia Calendar is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
10 //
11 // Xestia Calendar is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Calendar. If not, see <http://www.gnu.org/licenses/>
19 #include "monthviewgen.h"
21 using namespace std;
23 int PreviousMonthNumberofDays(int month, int year){
24         
25         // Work out what the previous month is.
26         
27         // Return 31 if month matches.
28         
29         if (month == 1 || month == 2 || month == 4 || 
30                 month == 6 || month == 8 || month == 9 || month == 11){
31         
32                 return 31;
33                         
34         }
35         
36         // Return 30 if month matches.
37         
38         if (month == 5 || month == 7 || month == 10 || month == 12){
39                 
40                 return 30;
41                 
42         }
43         
44         // Work out if year is a leap year and return 28 or 29
45         // for february.
46         
47         if (year % 4 == 0){
48                 
49                 if (year % 100 == 0){
50                         
51                         if (year % 400 == 0){
52                         
53                                 return 29;
54                                 
55                         } else {
56                                 
57                                 return 28;
58                                 
59                         }
60                         
61                 }
62                 
63                 return 29;
64                 
65         } else {
66                 
67                 return 28;
68                 
69         }
70         
71 }
73 XCCalendarMonthViewGrid GenerateMonthGrid(int month, int year){
74         
75         XCCalendarMonthViewGrid monthViewOutput;
76         
77         // Work out which day of the week the first day of 
78         // the month is.
79                 
80         time_t emptyTime;
81         struct tm * firstDay;
82         int previousMonthNumDays = PreviousMonthNumberofDays(month, year);
83         int previousMonthDays = 0;
84         int previousMonth = 0;
85         int previousYear = year;
86         int dayWeekCount = 0;
87         bool processGrid = true;
88         bool firstWeekProcessing = true;
89         
90         time(&emptyTime);
91         
92         firstDay = localtime(&emptyTime);
93         firstDay->tm_year = year - 1900;
94         firstDay->tm_mon = month - 1;
95         firstDay->tm_mday = 1;
96         
97         mktime(firstDay);
98         
99         if (firstDay->tm_wday == 0){
100                 previousMonthDays = 6;
101         } else {
102                 previousMonthDays = firstDay->tm_wday - 1;
103         }
104         
105         if (month == 1){
106                 
107                 previousMonth = 12;
108                 previousYear--;
109                 
110         } else {
112                 previousMonth = month - 1;
113                 
114         }
115         
116         // Workout the days of the previous month for the
117         // first week to be displayed.
118         
119         XCCalendarMonthViewGridDayWeek firstWeek;
120         
121         for (int previousMonthDaysProcessing = 0; 
122                 previousMonthDaysProcessing < previousMonthDays; 
123                 previousMonthDaysProcessing++){
124                 
125                 XCCalendarMonthViewGridDayData dayItem;
126                         
127                 dayItem.day = previousMonthNumDays - previousMonthDaysProcessing;
128                 dayItem.month = previousMonth;
129                 dayItem.year = previousYear;
130                 dayItem.isInMonth = false;
131                         
132                 firstWeek.dayList.insert(firstWeek.dayList.begin(), dayItem);
133                         
134                 dayWeekCount++;
135                         
136         }
137         
138         // Process the actual days of the month.
140         XCCalendarMonthViewGridDayWeek monthWeek;
141         int processDay = 1;
142         
143         while (processGrid == true){
144                 
145                 if (dayWeekCount == 7){
146                         
147                         // Add to the month view grid.
148                         
149                         if (firstWeekProcessing == true){
150                                 
151                                 firstWeekProcessing = false;
152                                 monthViewOutput.weekList.push_back(firstWeek);
153                                 
154                         } else {
155                         
156                                 monthViewOutput.weekList.push_back(monthWeek);
157                                 monthWeek.dayList.clear();
158                                 
159                         }
160                         
161                         dayWeekCount = 0;
162                         continue;
163                         
164                 }
166                 XCCalendarMonthViewGridDayData dayItem;
167                 
168                 dayItem.day = processDay;
169                 dayItem.month = month;
170                 dayItem.year = year;
171                 dayItem.isInMonth = true;
172                 
173                 if (firstWeekProcessing == true){
174                         
175                         firstWeek.dayList.push_back(dayItem);
176                         
177                 } else {
178                         
179                         monthWeek.dayList.push_back(dayItem);
180                         
181                 }
183                 dayWeekCount++;
184                 
185                 if ((month == 1 || month == 3 || month == 5 || 
186                         month == 7 || month == 8 || month == 10 || month == 12) && 
187                         processDay == 31){
188         
189                         break;
190                         
191                 }
192         
193                 if ((month == 4 || month == 6 || month == 9 || month == 11) &&
194                         processDay == 30){
195                 
196                         break;
197                 
198                 }
199                 
200                 if (year % 4 == 0){
201                 
202                         if (year % 100 == 0){
203                         
204                                 if (year % 400 == 0){
205                         
206                                         if (month == 2 && processDay == 29){
207                                         
208                                                 break;
209                                                 
210                                         }
211                                 
212                                 } else {
213                                 
214                                         if (month == 2 && processDay == 28){
215                                                 
216                                                 break;
217                                                 
218                                         }
219                                 
220                                 }
221                         
222                         }
223                 
224                         if (month == 2 && processDay == 29){
226                                 break;
227                                 
228                         }
229                 
230                 } else if ((month == 2 && processDay == 28) && year % 4 != 0){
231                 
232                         break;
233                 
234                 }
235                 
236                 processDay++;
237                 
238                 
239         }
240         
241         // At the end of the month add any remaining days
242         // (if required).
243         
244         int nextMonthYear = year;
245         int nextMonth = 0;
246         int nextMonthDay = 1;
247         
248         if (month == 12){
249                 
250                 nextMonth = 1;
251                 nextMonthYear++;
252                 
253         } else {
254                 
255                 nextMonth = month + 1;
256                 
257         }
258         
259         for (; dayWeekCount < 7; dayWeekCount++){
260         
261                 XCCalendarMonthViewGridDayData dayItem;
262                 
263                 dayItem.day = nextMonthDay;
264                 dayItem.month = nextMonth;
265                 dayItem.year = nextMonthYear;
266                 dayItem.isInMonth = false;
268                 monthWeek.dayList.push_back(dayItem);
269                 nextMonthDay++;
270                 
271         }
272         
273         if (dayWeekCount == 7){
275                 monthViewOutput.weekList.push_back(monthWeek);
276                 
277         }
278         
279         return monthViewOutput;
280         
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy