1 // monthviewgen.cpp - Xestia Calendar month view generation functions.
3 // (c) 2016-2017 Xestia Software Development.
5 // This file is part of Xestia Calendar.
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.
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.
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"
23 int PreviousMonthNumberofDays(int month, int year){
25 // Work out what the previous month is.
27 // Return 31 if month matches.
29 if (month == 1 || month == 2 || month == 4 ||
30 month == 6 || month == 8 || month == 9 || month == 11){
36 // Return 30 if month matches.
38 if (month == 5 || month == 7 || month == 10 || month == 12){
44 // Work out if year is a leap year and return 28 or 29
73 XCCalendarMonthViewGrid GenerateMonthGrid(int month, int year){
75 XCCalendarMonthViewGrid monthViewOutput;
77 // Work out which day of the week the first day of
82 int previousMonthNumDays = PreviousMonthNumberofDays(month, year);
83 int previousMonthDays = 0;
84 int previousMonth = 0;
85 int previousYear = year;
87 bool processGrid = true;
88 bool firstWeekProcessing = true;
92 firstDay = localtime(&emptyTime);
93 firstDay->tm_year = year - 1900;
94 firstDay->tm_mon = month - 1;
95 firstDay->tm_mday = 1;
99 if (firstDay->tm_wday == 0){
100 previousMonthDays = 6;
102 previousMonthDays = firstDay->tm_wday - 1;
112 previousMonth = month - 1;
116 // Workout the days of the previous month for the
117 // first week to be displayed.
119 XCCalendarMonthViewGridDayWeek firstWeek;
121 for (int previousMonthDaysProcessing = 0;
122 previousMonthDaysProcessing < previousMonthDays;
123 previousMonthDaysProcessing++){
125 XCCalendarMonthViewGridDayData dayItem;
127 dayItem.day = previousMonthNumDays - previousMonthDaysProcessing;
128 dayItem.month = previousMonth;
129 dayItem.year = previousYear;
130 dayItem.isInMonth = false;
132 firstWeek.dayList.insert(firstWeek.dayList.begin(), dayItem);
138 // Process the actual days of the month.
140 XCCalendarMonthViewGridDayWeek monthWeek;
143 while (processGrid == true){
145 if (dayWeekCount == 7){
147 // Add to the month view grid.
149 if (firstWeekProcessing == true){
151 firstWeekProcessing = false;
152 monthViewOutput.weekList.push_back(firstWeek);
156 monthViewOutput.weekList.push_back(monthWeek);
157 monthWeek.dayList.clear();
166 XCCalendarMonthViewGridDayData dayItem;
168 dayItem.day = processDay;
169 dayItem.month = month;
171 dayItem.isInMonth = true;
173 if (firstWeekProcessing == true){
175 firstWeek.dayList.push_back(dayItem);
179 monthWeek.dayList.push_back(dayItem);
185 if ((month == 1 || month == 3 || month == 5 ||
186 month == 7 || month == 8 || month == 10 || month == 12) &&
193 if ((month == 4 || month == 6 || month == 9 || month == 11) &&
202 if (year % 100 == 0){
204 if (year % 400 == 0){
206 if (month == 2 && processDay == 29){
214 if (month == 2 && processDay == 28){
224 if (month == 2 && processDay == 29){
230 } else if ((month == 2 && processDay == 28) && year % 4 != 0){
241 // At the end of the month add any remaining days
244 int nextMonthYear = year;
246 int nextMonthDay = 1;
255 nextMonth = month + 1;
259 for (; dayWeekCount < 7; dayWeekCount++){
261 XCCalendarMonthViewGridDayData dayItem;
263 dayItem.day = nextMonthDay;
264 dayItem.month = nextMonth;
265 dayItem.year = nextMonthYear;
266 dayItem.isInMonth = false;
268 monthWeek.dayList.push_back(dayItem);
273 if (dayWeekCount == 7){
275 monthViewOutput.weekList.push_back(monthWeek);
279 return monthViewOutput;