Diffstat (limited to 'microkde/kcalendarsystemgregorian.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | microkde/kcalendarsystemgregorian.cpp | 359 |
1 files changed, 359 insertions, 0 deletions
diff --git a/microkde/kcalendarsystemgregorian.cpp b/microkde/kcalendarsystemgregorian.cpp new file mode 100644 index 0000000..7c5b62a --- a/dev/null +++ b/microkde/kcalendarsystemgregorian.cpp | |||
@@ -0,0 +1,359 @@ | |||
1 | /* | ||
2 | Copyright (c) 2002 Carlos Moro <cfmoro@correo.uniovi.es> | ||
3 | Copyright (c) 2002 Hans Petter Bieker <bieker@kde.org> | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Library General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Library General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Library General Public License | ||
16 | along with this library; see the file COPYING.LIB. If not, write to | ||
17 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | // Derived gregorian kde calendar class | ||
22 | // Just a schema. | ||
23 | |||
24 | #include <qdatetime.h> | ||
25 | #include <qstring.h> | ||
26 | |||
27 | #include <klocale.h> | ||
28 | #include <kdebug.h> | ||
29 | |||
30 | #include "kcalendarsystemgregorian.h" | ||
31 | |||
32 | KCalendarSystemGregorian::KCalendarSystemGregorian(const KLocale * locale) | ||
33 | : KCalendarSystem(locale) | ||
34 | { | ||
35 | kdDebug(5400) << "Created gregorian calendar" << endl; | ||
36 | } | ||
37 | |||
38 | KCalendarSystemGregorian::~KCalendarSystemGregorian() | ||
39 | { | ||
40 | } | ||
41 | |||
42 | int KCalendarSystemGregorian::year(const QDate& date) const | ||
43 | { | ||
44 | // kdDebug(5400) << "Gregorian year..." << endl; | ||
45 | return date.year(); | ||
46 | } | ||
47 | |||
48 | int KCalendarSystemGregorian::monthsInYear( const QDate & ) const | ||
49 | { | ||
50 | // kdDebug(5400) << "Gregorian monthsInYear" << endl; | ||
51 | |||
52 | return 12; | ||
53 | } | ||
54 | |||
55 | int KCalendarSystemGregorian::weeksInYear(int year) const | ||
56 | { | ||
57 | #if QT_VERSION >= 300 | ||
58 | QDate temp; | ||
59 | temp.setYMD(year, 12, 31); | ||
60 | |||
61 | // If the last day of the year is in the first week, we have to check the | ||
62 | // week before | ||
63 | if ( temp.weekNumber() == 1 ) | ||
64 | temp.addDays(-7); | ||
65 | |||
66 | return temp.weekNumber(); | ||
67 | #else | ||
68 | return 52; | ||
69 | #endif | ||
70 | } | ||
71 | |||
72 | int KCalendarSystemGregorian::weekNumber(const QDate& date, | ||
73 | int * yearNum) const | ||
74 | { | ||
75 | #if QT_VERSION >= 300 | ||
76 | return date.weekNumber(yearNum); | ||
77 | #else | ||
78 | return 1; | ||
79 | #endif | ||
80 | } | ||
81 | |||
82 | QString KCalendarSystemGregorian::monthName(const QDate& date, | ||
83 | bool shortName) const | ||
84 | { | ||
85 | return monthName(month(date), shortName); | ||
86 | } | ||
87 | |||
88 | QString KCalendarSystemGregorian::monthNamePossessive(const QDate& date, bool shortName) const | ||
89 | { | ||
90 | return monthNamePossessive(month(date), shortName); | ||
91 | } | ||
92 | |||
93 | QString KCalendarSystemGregorian::monthName(int month, bool shortName) const | ||
94 | { | ||
95 | // kdDebug(5400) << "Gregorian getMonthName" << endl; | ||
96 | |||
97 | if ( shortName ) | ||
98 | switch ( month ) | ||
99 | { | ||
100 | case 1: | ||
101 | return locale()->translate("January", "Jan"); | ||
102 | case 2: | ||
103 | return locale()->translate("February", "Feb"); | ||
104 | case 3: | ||
105 | return locale()->translate("March", "Mar"); | ||
106 | case 4: | ||
107 | return locale()->translate("April", "Apr"); | ||
108 | case 5: | ||
109 | return locale()->translate("May short", "May"); | ||
110 | case 6: | ||
111 | return locale()->translate("June", "Jun"); | ||
112 | case 7: | ||
113 | return locale()->translate("July", "Jul"); | ||
114 | case 8: | ||
115 | return locale()->translate("August", "Aug"); | ||
116 | case 9: | ||
117 | return locale()->translate("September", "Sep"); | ||
118 | case 10: | ||
119 | return locale()->translate("October", "Oct"); | ||
120 | case 11: | ||
121 | return locale()->translate("November", "Nov"); | ||
122 | case 12: | ||
123 | return locale()->translate("December", "Dec"); | ||
124 | } | ||
125 | else | ||
126 | switch ( month ) | ||
127 | { | ||
128 | case 1: | ||
129 | return locale()->translate("January"); | ||
130 | case 2: | ||
131 | return locale()->translate("February"); | ||
132 | case 3: | ||
133 | return locale()->translate("March"); | ||
134 | case 4: | ||
135 | return locale()->translate("April"); | ||
136 | case 5: | ||
137 | return locale()->translate("May long", "May"); | ||
138 | case 6: | ||
139 | return locale()->translate("June"); | ||
140 | case 7: | ||
141 | return locale()->translate("July"); | ||
142 | case 8: | ||
143 | return locale()->translate("August"); | ||
144 | case 9: | ||
145 | return locale()->translate("September"); | ||
146 | case 10: | ||
147 | return locale()->translate("October"); | ||
148 | case 11: | ||
149 | return locale()->translate("November"); | ||
150 | case 12: | ||
151 | return locale()->translate("December"); | ||
152 | } | ||
153 | |||
154 | return QString::null; | ||
155 | } | ||
156 | |||
157 | QString KCalendarSystemGregorian::monthNamePossessive(int month, | ||
158 | bool shortName) const | ||
159 | { | ||
160 | // kdDebug(5400) << "Gregorian getMonthName" << endl; | ||
161 | |||
162 | if ( shortName ) | ||
163 | switch ( month ) | ||
164 | { | ||
165 | case 1: | ||
166 | return locale()->translate("of January", "of Jan"); | ||
167 | case 2: | ||
168 | return locale()->translate("of February", "of Feb"); | ||
169 | case 3: | ||
170 | return locale()->translate("of March", "of Mar"); | ||
171 | case 4: | ||
172 | return locale()->translate("of April", "of Apr"); | ||
173 | case 5: | ||
174 | return locale()->translate("of May short", "of May"); | ||
175 | case 6: | ||
176 | return locale()->translate("of June", "of Jun"); | ||
177 | case 7: | ||
178 | return locale()->translate("of July", "of Jul"); | ||
179 | case 8: | ||
180 | return locale()->translate("of August", "of Aug"); | ||
181 | case 9: | ||
182 | return locale()->translate("of September", "of Sep"); | ||
183 | case 10: | ||
184 | return locale()->translate("of October", "of Oct"); | ||
185 | case 11: | ||
186 | return locale()->translate("of November", "of Nov"); | ||
187 | case 12: | ||
188 | return locale()->translate("of December", "of Dec"); | ||
189 | } | ||
190 | else | ||
191 | switch ( month ) | ||
192 | { | ||
193 | case 1: | ||
194 | return locale()->translate("of January"); | ||
195 | case 2: | ||
196 | return locale()->translate("of February"); | ||
197 | case 3: | ||
198 | return locale()->translate("of March"); | ||
199 | case 4: | ||
200 | return locale()->translate("of April"); | ||
201 | case 5: | ||
202 | return locale()->translate("of May long", "of May"); | ||
203 | case 6: | ||
204 | return locale()->translate("of June"); | ||
205 | case 7: | ||
206 | return locale()->translate("of July"); | ||
207 | case 8: | ||
208 | return locale()->translate("of August"); | ||
209 | case 9: | ||
210 | return locale()->translate("of September"); | ||
211 | case 10: | ||
212 | return locale()->translate("of October"); | ||
213 | case 11: | ||
214 | return locale()->translate("of November"); | ||
215 | case 12: | ||
216 | return locale()->translate("of December"); | ||
217 | } | ||
218 | |||
219 | return QString::null; | ||
220 | } | ||
221 | |||
222 | bool KCalendarSystemGregorian::setYMD(QDate & date, int y, int m, int d) const | ||
223 | { | ||
224 | // We don't want Qt to add 1900 to them | ||
225 | if ( y >= 0 && y <= 99 ) | ||
226 | return false; | ||
227 | |||
228 | // QDate supports gregorian internally | ||
229 | return date.setYMD(y, m, d); | ||
230 | } | ||
231 | |||
232 | QDate KCalendarSystemGregorian::addYears(const QDate & date, int nyears) const | ||
233 | { | ||
234 | #if QT_VERSION >= 300 | ||
235 | return date.addYears(nyears); | ||
236 | #else | ||
237 | int year = date.year() + nyears; | ||
238 | int month = date.month(); | ||
239 | int day = date.day(); | ||
240 | QDate newDate( year, month, 1 ); | ||
241 | if ( day > newDate.daysInMonth() ) day = newDate.daysInMonth(); | ||
242 | return QDate( year, month, day ); | ||
243 | #endif | ||
244 | } | ||
245 | |||
246 | QDate KCalendarSystemGregorian::addMonths(const QDate & date, int nmonths) const | ||
247 | { | ||
248 | #if QT_VERSION >= 300 | ||
249 | return date.addMonths(nmonths); | ||
250 | #else | ||
251 | int month = date.month(); | ||
252 | int nyears; | ||
253 | if ( nmonths >= 0 ) { | ||
254 | month += nmonths; | ||
255 | nyears = ( month - 1 ) / 12; | ||
256 | month = ( ( month - 1 ) % 12 ) + 1; | ||
257 | } else { | ||
258 | nyears = nmonths / 12; | ||
259 | // nmonths += nyears * 12; | ||
260 | nmonths = nmonths % 12; | ||
261 | month += nmonths; | ||
262 | if ( month <= 0 ) { | ||
263 | month += 12; | ||
264 | --nyears; | ||
265 | } | ||
266 | } | ||
267 | int year = date.year() + nyears; | ||
268 | int day = date.day(); | ||
269 | QDate newDate( year, month, 1 ); | ||
270 | if ( day > newDate.daysInMonth() ) day = newDate.daysInMonth(); | ||
271 | return QDate( year, month, day ); | ||
272 | #endif | ||
273 | } | ||
274 | |||
275 | QDate KCalendarSystemGregorian::addDays(const QDate & date, int ndays) const | ||
276 | { | ||
277 | return date.addDays(ndays); | ||
278 | } | ||
279 | |||
280 | QString KCalendarSystemGregorian::weekDayName(int col, bool shortName) const | ||
281 | { | ||
282 | // ### Should this really be different to each calendar system? Or are we | ||
283 | // only going to support weeks with 7 days? | ||
284 | |||
285 | //kdDebug(5400) << "Gregorian wDayName" << endl; | ||
286 | return locale()->weekDayName(col, shortName); | ||
287 | } | ||
288 | |||
289 | QString KCalendarSystemGregorian::weekDayName(const QDate& date, bool shortName) const | ||
290 | { | ||
291 | return weekDayName(dayOfWeek(date), shortName); | ||
292 | } | ||
293 | |||
294 | |||
295 | int KCalendarSystemGregorian::dayOfWeek(const QDate& date) const | ||
296 | { | ||
297 | return date.dayOfWeek(); | ||
298 | } | ||
299 | |||
300 | int KCalendarSystemGregorian::dayOfYear(const QDate & date) const | ||
301 | { | ||
302 | return date.dayOfYear(); | ||
303 | } | ||
304 | |||
305 | int KCalendarSystemGregorian::daysInMonth(const QDate& date) const | ||
306 | { | ||
307 | // kdDebug(5400) << "Gregorian daysInMonth" << endl; | ||
308 | return date.daysInMonth(); | ||
309 | } | ||
310 | |||
311 | int KCalendarSystemGregorian::minValidYear() const | ||
312 | { | ||
313 | return 1753; // QDate limit | ||
314 | } | ||
315 | |||
316 | int KCalendarSystemGregorian::maxValidYear() const | ||
317 | { | ||
318 | return 8000; // QDate limit | ||
319 | } | ||
320 | |||
321 | int KCalendarSystemGregorian::day(const QDate& date) const | ||
322 | { | ||
323 | return date.day(); | ||
324 | } | ||
325 | |||
326 | int KCalendarSystemGregorian::month(const QDate& date) const | ||
327 | { | ||
328 | return date.month(); | ||
329 | } | ||
330 | |||
331 | int KCalendarSystemGregorian::daysInYear(const QDate& date) const | ||
332 | { | ||
333 | return date.daysInYear(); | ||
334 | } | ||
335 | |||
336 | int KCalendarSystemGregorian::weekDayOfPray() const | ||
337 | { | ||
338 | return 7; // sunday | ||
339 | } | ||
340 | |||
341 | QString KCalendarSystemGregorian::calendarName() const | ||
342 | { | ||
343 | return QString::fromLatin1("gregorian"); | ||
344 | } | ||
345 | |||
346 | bool KCalendarSystemGregorian::isLunar() const | ||
347 | { | ||
348 | return false; | ||
349 | } | ||
350 | |||
351 | bool KCalendarSystemGregorian::isLunisolar() const | ||
352 | { | ||
353 | return false; | ||
354 | } | ||
355 | |||
356 | bool KCalendarSystemGregorian::isSolar() const | ||
357 | { | ||
358 | return true; | ||
359 | } | ||