Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
frmUpdate: Implemented code from Xestia Address Book
[xestiacalendar/.git] / source / common / monthviewgen.cpp
1 // monthviewgen.cpp - Xestia Calendar month view generation functions.
2 //
3 // (c) 2016 Xestia Software Development.
4 //
5 // This file is part of Xestia Calendar.
6 //
7 // Xestia Address Book 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 Address Book 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