1
2 /**************************************************************************
3 * Copyright (C) 2007-2015 Ruben Pollan Bella <meskio@sindominio.net> *
4 * *
5 * This file is part of TuDu. *
6 * *
7 * TuDu is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; version 3 of the License. *
10 * *
11 * TuDu 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 *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 **************************************************************************/
19
20 #include "date.h"
21
Date(int day,int month,int year)22 Date::Date(int day, int month, int year): _day(day), _month(month), _year(year) {}
23
day(int d)24 int& Date::day(int d)
25 {
26 if (d) _day = d;
27 return _day;
28 }
29
month(int m)30 int& Date::month(int m)
31 {
32 if (m) _month = m;
33 return _month;
34 }
35
year(int y)36 int& Date::year(int y)
37 {
38 if (y) _year = y;
39 return _year;
40 }
41
setToday()42 void Date::setToday()
43 {
44 time_t t = time(NULL);
45 struct tm* pt = localtime(&t);
46 _day = pt->tm_mday;
47 _month = pt->tm_mon+1;
48 _year = pt->tm_year+1900;
49 }
50
valid()51 bool Date::valid()
52 {
53 return (1900 != _year);
54 }
55
correct()56 bool Date::correct()
57 {
58 bool correct;
59
60 switch (_month)
61 {
62 case 1:
63 case 3:
64 case 5:
65 case 7:
66 case 8:
67 case 10:
68 case 12:
69 if ((_day < 1) || (_day > 31))
70 correct = false;
71 else
72 correct = true;
73 break;
74 case 4:
75 case 6:
76 case 9:
77 case 11:
78 if ((_day < 1) || (_day > 30))
79 correct = false;
80 else
81 correct = true;
82 break;
83 case 2:
84 /* if leap year */
85 if ((_year % 4 == 0) && !((_year % 100 == 0) && (_year % 400 != 0)))
86 {
87 if ((_day < 1) || (_day > 29))
88 correct = false;
89 else
90 correct = true;
91 }
92 else
93 {
94 if ((_day < 1) || (_day > 28))
95 correct = false;
96 else
97 correct = true;
98 }
99 break;
100 default:
101 correct = false;
102 break;
103 }
104 return correct;
105 }
106
107 #define SECONDS_IN_A_DAY (60*60*24)
daysLeft()108 int Date::daysLeft()
109 {
110 time_t rawtime;
111 time_t today;
112 struct tm * timeinfo;
113 int daysleft;
114
115 time(&rawtime);
116 timeinfo = localtime(&rawtime);
117 timeinfo->tm_mday = _day;
118 timeinfo->tm_mon = _month - 1;
119 timeinfo->tm_year = _year - 1900;
120 rawtime = mktime(timeinfo);
121 today = time(NULL);
122 daysleft = difftime(rawtime,today)/SECONDS_IN_A_DAY;
123 return daysleft;
124 }
125
operator -(int days)126 Date Date::operator-(int days)
127 {
128 Date d(_day, _month, _year);
129
130 d._day -= days;
131 while (d._day <= 0)
132 {
133 d._month--;
134 if (d._month == 0)
135 {
136 d._month = 12;
137 d._year--;
138 }
139
140 switch (d._month)
141 {
142 case 1:
143 case 3:
144 case 5:
145 case 7:
146 case 8:
147 case 10:
148 case 12:
149 d._day += 31;
150 break;
151 case 4:
152 case 6:
153 case 9:
154 case 11:
155 d._day += 30;
156 break;
157 case 2:
158 /* if leap year */
159 if ((d._year % 4 == 0) && !((d._year % 100 == 0) && (d._year % 400 != 0)))
160 {
161 d._day += 29;
162 }
163 else
164 {
165 d._day += 28;
166 }
167 break;
168 }
169 }
170
171 return d;
172 }
173
operator +(int days)174 Date Date::operator+(int days)
175 {
176 Date d(_day, _month, _year);
177 bool day_valid = false;
178
179 d._day += days;
180 do {
181 switch (d._month)
182 {
183 case 1:
184 case 3:
185 case 5:
186 case 7:
187 case 8:
188 case 10:
189 case 12:
190 if (d._day <= 31) day_valid = true;
191 else d._day -= 31;
192 break;
193 case 4:
194 case 6:
195 case 9:
196 case 11:
197 if (d._day <= 30) day_valid = true;
198 else d._day -= 30;
199 break;
200 case 2:
201 /* if leap year */
202 if ((d._year % 4 == 0) && !((d._year % 100 == 0) && (d._year % 400 != 0)))
203 {
204 if (d._day <= 29) day_valid = true;
205 else d._day -= 29;
206 }
207 else
208 {
209 if (d._day <= 28) day_valid = true;
210 else d._day -= 28;
211 }
212 break;
213 }
214
215 if (!day_valid)
216 {
217 d._month++;
218 if (d._month == 13)
219 {
220 d._month = 1;
221 d._year++;
222 }
223 }
224 } while (!day_valid);
225
226 return d;
227 }
228
operator <(Date d)229 bool Date::operator<(Date d)
230 {
231 if (_year < d._year)
232 return true;
233 else if (_year > d._year)
234 return false;
235 else if (_month < d._month)
236 return true;
237 else if (_month > d._month)
238 return false;
239 else if (_day < d._day)
240 return true;
241 else
242 return false;
243 }
244
operator >(Date d)245 bool Date::operator>(Date d)
246 {
247 if (_year > d._year)
248 return true;
249 else if (_year < d._year)
250 return false;
251 else if (_month > d._month)
252 return true;
253 else if (_month < d._month)
254 return false;
255 else if (_day > d._day)
256 return true;
257 else
258 return false;
259 }
260
operator !=(Date d)261 bool Date::operator!=(Date d)
262 {
263 if (d._year != _year)
264 return true;
265 else if (d._month != _month)
266 return true;
267 else if (d._day != _day)
268 return true;
269 else
270 return false;
271 }
272
operator ==(Date d)273 bool Date::operator==(Date d)
274 {
275 if (d._year != _year)
276 return false;
277 else if (d._month != _month)
278 return false;
279 else if (d._day != _day)
280 return false;
281 else
282 return true;
283 }
284