KDECore
kcalendarsystemqdate.cpp
Go to the documentation of this file.
00001 /* 00002 Copyright (c) 2002 Carlos Moro <cfmoro@correo.uniovi.es> 00003 Copyright (c) 2002-2003 Hans Petter Bieker <bieker@kde.org> 00004 Copyright 2007, 2010 John Layt <john@layt.net> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 // Derived QDate kde calendar class 00023 00024 #include "kcalendarsystemqdate_p.h" 00025 #include "kcalendarsystemprivate_p.h" 00026 #include "kcalendarera_p.h" 00027 00028 #include "kdebug.h" 00029 #include "klocale.h" 00030 #include "kglobal.h" 00031 #include "kconfiggroup.h" 00032 00033 #include <QtCore/QDate> 00034 #include <QtCore/QCharRef> 00035 00036 class KCalendarSystemQDatePrivate : public KCalendarSystemPrivate 00037 { 00038 public: 00039 explicit KCalendarSystemQDatePrivate(KCalendarSystemQDate *q); 00040 00041 virtual ~KCalendarSystemQDatePrivate(); 00042 00043 // Virtual methods each calendar system must re-implement 00044 virtual KLocale::CalendarSystem calendarSystem() const; 00045 virtual void loadDefaultEraList(); 00046 virtual int monthsInYear(int year) const; 00047 virtual int daysInMonth(int year, int month) const; 00048 virtual int daysInYear(int year) const; 00049 virtual int daysInWeek() const; 00050 virtual bool isLeapYear(int year) const; 00051 virtual bool hasLeapMonths() const; 00052 virtual bool hasYearZero() const; 00053 virtual int maxDaysInWeek() const; 00054 virtual int maxMonthsInYear() const; 00055 virtual int earliestValidYear() const; 00056 virtual int latestValidYear() const; 00057 virtual QString monthName(int month, int year, KLocale::DateTimeComponentFormat format, bool possessive) const; 00058 virtual QString weekDayName(int weekDay, KLocale::DateTimeComponentFormat format) const; 00059 00060 bool m_useCommonEra; 00061 }; 00062 00063 // Shared d pointer implementations 00064 00065 KCalendarSystemQDatePrivate::KCalendarSystemQDatePrivate(KCalendarSystemQDate *q) 00066 : KCalendarSystemPrivate(q), 00067 m_useCommonEra(false) 00068 00069 { 00070 } 00071 00072 KCalendarSystemQDatePrivate::~KCalendarSystemQDatePrivate() 00073 { 00074 } 00075 00076 KLocale::CalendarSystem KCalendarSystemQDatePrivate::calendarSystem() const 00077 { 00078 return KLocale::QDateCalendar; 00079 } 00080 00081 void KCalendarSystemQDatePrivate::loadDefaultEraList() 00082 { 00083 QString name, shortName, format; 00084 00085 KConfigGroup lcg(config(), QString::fromLatin1("Locale")); 00086 KConfigGroup cg = lcg.group(QString::fromLatin1("KCalendarSystem %1").arg(q->calendarType(q->calendarSystem()))); 00087 m_useCommonEra = cg.readEntry("UseCommonEra", false); 00088 00089 if (m_useCommonEra) { 00090 name = i18nc("Calendar Era: Gregorian Common Era, years < 0, LongFormat", "Before Common Era"); 00091 shortName = i18nc("Calendar Era: Gregorian Common Era, years < 0, ShortFormat", "BCE"); 00092 } else { 00093 name = i18nc("Calendar Era: Gregorian Christian Era, years < 0, LongFormat", "Before Christ"); 00094 shortName = i18nc("Calendar Era: Gregorian Christian Era, years < 0, ShortFormat", "BC"); 00095 } 00096 format = i18nc("(kdedt-format) Gregorian, BC, full era year format used for %EY, e.g. 2000 BC", "%Ey %EC"); 00097 addEra('-', 1, q->epoch().addDays(-1), -1, q->earliestValidDate(), name, shortName, format); 00098 00099 if (m_useCommonEra) { 00100 name = i18nc("Calendar Era: Gregorian Common Era, years > 0, LongFormat", "Common Era"); 00101 shortName = i18nc("Calendar Era: Gregorian Common Era, years > 0, ShortFormat", "CE"); 00102 } else { 00103 name = i18nc("Calendar Era: Gregorian Christian Era, years > 0, LongFormat", "Anno Domini"); 00104 shortName = i18nc("Calendar Era: Gregorian Christian Era, years > 0, ShortFormat", "AD"); 00105 } 00106 format = i18nc("(kdedt-format) Gregorian, AD, full era year format used for %EY, e.g. 2000 AD", "%Ey %EC"); 00107 addEra('+', 1, q->epoch(), 1, q->latestValidDate(), name, shortName, format); 00108 } 00109 00110 int KCalendarSystemQDatePrivate::monthsInYear(int year) const 00111 { 00112 Q_UNUSED(year) 00113 return 12; 00114 } 00115 00116 int KCalendarSystemQDatePrivate::daysInMonth(int year, int month) const 00117 { 00118 QDate tempDate(year, month, 1); 00119 return tempDate.daysInMonth(); 00120 } 00121 00122 int KCalendarSystemQDatePrivate::daysInYear(int year) const 00123 { 00124 QDate tempDate(year, 1, 1); 00125 return tempDate.daysInYear(); 00126 } 00127 00128 int KCalendarSystemQDatePrivate::daysInWeek() const 00129 { 00130 return 7; 00131 } 00132 00133 bool KCalendarSystemQDatePrivate::isLeapYear(int year) const 00134 { 00135 return QDate::isLeapYear(year); 00136 } 00137 00138 bool KCalendarSystemQDatePrivate::hasLeapMonths() const 00139 { 00140 return false; 00141 } 00142 00143 bool KCalendarSystemQDatePrivate::hasYearZero() const 00144 { 00145 return false; 00146 } 00147 00148 int KCalendarSystemQDatePrivate::maxDaysInWeek() const 00149 { 00150 return 7; 00151 } 00152 00153 int KCalendarSystemQDatePrivate::maxMonthsInYear() const 00154 { 00155 return 12; 00156 } 00157 00158 int KCalendarSystemQDatePrivate::earliestValidYear() const 00159 { 00160 return -4712; 00161 } 00162 00163 int KCalendarSystemQDatePrivate::latestValidYear() const 00164 { 00165 return 9999; 00166 } 00167 00168 QString KCalendarSystemQDatePrivate::monthName(int month, int year, KLocale::DateTimeComponentFormat format, bool possessive) const 00169 { 00170 Q_UNUSED(year); 00171 00172 if (format == KLocale::NarrowName) { 00173 switch (month) { 00174 case 1: 00175 return ki18nc("Gregorian month 1 - KLocale::NarrowName", "J").toString(locale()); 00176 case 2: 00177 return ki18nc("Gregorian month 2 - KLocale::NarrowName", "F").toString(locale()); 00178 case 3: 00179 return ki18nc("Gregorian month 3 - KLocale::NarrowName", "M").toString(locale()); 00180 case 4: 00181 return ki18nc("Gregorian month 4 - KLocale::NarrowName", "A").toString(locale()); 00182 case 5: 00183 return ki18nc("Gregorian month 5 - KLocale::NarrowName", "M").toString(locale()); 00184 case 6: 00185 return ki18nc("Gregorian month 6 - KLocale::NarrowName", "J").toString(locale()); 00186 case 7: 00187 return ki18nc("Gregorian month 7 - KLocale::NarrowName", "J").toString(locale()); 00188 case 8: 00189 return ki18nc("Gregorian month 8 - KLocale::NarrowName", "A").toString(locale()); 00190 case 9: 00191 return ki18nc("Gregorian month 9 - KLocale::NarrowName", "S").toString(locale()); 00192 case 10: 00193 return ki18nc("Gregorian month 10 - KLocale::NarrowName", "O").toString(locale()); 00194 case 11: 00195 return ki18nc("Gregorian month 11 - KLocale::NarrowName", "N").toString(locale()); 00196 case 12: 00197 return ki18nc("Gregorian month 12 - KLocale::NarrowName", "D").toString(locale()); 00198 default: 00199 return QString(); 00200 } 00201 } 00202 00203 if (format == KLocale::ShortName && possessive) { 00204 switch (month) { 00205 case 1: 00206 return ki18nc("Gregorian month 1 - KLocale::ShortName Possessive", "of Jan").toString(locale()); 00207 case 2: 00208 return ki18nc("Gregorian month 2 - KLocale::ShortName Possessive", "of Feb").toString(locale()); 00209 case 3: 00210 return ki18nc("Gregorian month 3 - KLocale::ShortName Possessive", "of Mar").toString(locale()); 00211 case 4: 00212 return ki18nc("Gregorian month 4 - KLocale::ShortName Possessive", "of Apr").toString(locale()); 00213 case 5: 00214 return ki18nc("Gregorian month 5 - KLocale::ShortName Possessive", "of May").toString(locale()); 00215 case 6: 00216 return ki18nc("Gregorian month 6 - KLocale::ShortName Possessive", "of Jun").toString(locale()); 00217 case 7: 00218 return ki18nc("Gregorian month 7 - KLocale::ShortName Possessive", "of Jul").toString(locale()); 00219 case 8: 00220 return ki18nc("Gregorian month 8 - KLocale::ShortName Possessive", "of Aug").toString(locale()); 00221 case 9: 00222 return ki18nc("Gregorian month 9 - KLocale::ShortName Possessive", "of Sep").toString(locale()); 00223 case 10: 00224 return ki18nc("Gregorian month 10 - KLocale::ShortName Possessive", "of Oct").toString(locale()); 00225 case 11: 00226 return ki18nc("Gregorian month 11 - KLocale::ShortName Possessive", "of Nov").toString(locale()); 00227 case 12: 00228 return ki18nc("Gregorian month 12 - KLocale::ShortName Possessive", "of Dec").toString(locale()); 00229 default: 00230 return QString(); 00231 } 00232 } 00233 00234 if (format == KLocale::ShortName && !possessive) { 00235 switch (month) { 00236 case 1: 00237 return ki18nc("Gregorian month 1 - KLocale::ShortName", "Jan").toString(locale()); 00238 case 2: 00239 return ki18nc("Gregorian month 2 - KLocale::ShortName", "Feb").toString(locale()); 00240 case 3: 00241 return ki18nc("Gregorian month 3 - KLocale::ShortName", "Mar").toString(locale()); 00242 case 4: 00243 return ki18nc("Gregorian month 4 - KLocale::ShortName", "Apr").toString(locale()); 00244 case 5: 00245 return ki18nc("Gregorian month 5 - KLocale::ShortName", "May").toString(locale()); 00246 case 6: 00247 return ki18nc("Gregorian month 6 - KLocale::ShortName", "Jun").toString(locale()); 00248 case 7: 00249 return ki18nc("Gregorian month 7 - KLocale::ShortName", "Jul").toString(locale()); 00250 case 8: 00251 return ki18nc("Gregorian month 8 - KLocale::ShortName", "Aug").toString(locale()); 00252 case 9: 00253 return ki18nc("Gregorian month 9 - KLocale::ShortName", "Sep").toString(locale()); 00254 case 10: 00255 return ki18nc("Gregorian month 10 - KLocale::ShortName", "Oct").toString(locale()); 00256 case 11: 00257 return ki18nc("Gregorian month 11 - KLocale::ShortName", "Nov").toString(locale()); 00258 case 12: 00259 return ki18nc("Gregorian month 12 - KLocale::ShortName", "Dec").toString(locale()); 00260 default: 00261 return QString(); 00262 } 00263 } 00264 00265 if (format == KLocale::LongName && possessive) { 00266 switch (month) { 00267 case 1: 00268 return ki18nc("Gregorian month 1 - KLocale::LongName Possessive", "of January").toString(locale()); 00269 case 2: 00270 return ki18nc("Gregorian month 2 - KLocale::LongName Possessive", "of February").toString(locale()); 00271 case 3: 00272 return ki18nc("Gregorian month 3 - KLocale::LongName Possessive", "of March").toString(locale()); 00273 case 4: 00274 return ki18nc("Gregorian month 4 - KLocale::LongName Possessive", "of April").toString(locale()); 00275 case 5: 00276 return ki18nc("Gregorian month 5 - KLocale::LongName Possessive", "of May").toString(locale()); 00277 case 6: 00278 return ki18nc("Gregorian month 6 - KLocale::LongName Possessive", "of June").toString(locale()); 00279 case 7: 00280 return ki18nc("Gregorian month 7 - KLocale::LongName Possessive", "of July").toString(locale()); 00281 case 8: 00282 return ki18nc("Gregorian month 8 - KLocale::LongName Possessive", "of August").toString(locale()); 00283 case 9: 00284 return ki18nc("Gregorian month 9 - KLocale::LongName Possessive", "of September").toString(locale()); 00285 case 10: 00286 return ki18nc("Gregorian month 10 - KLocale::LongName Possessive", "of October").toString(locale()); 00287 case 11: 00288 return ki18nc("Gregorian month 11 - KLocale::LongName Possessive", "of November").toString(locale()); 00289 case 12: 00290 return ki18nc("Gregorian month 12 - KLocale::LongName Possessive", "of December").toString(locale()); 00291 default: 00292 return QString(); 00293 } 00294 } 00295 00296 // Default to LongName 00297 switch (month) { 00298 case 1: 00299 return ki18nc("Gregorian month 1 - KLocale::LongName", "January").toString(locale()); 00300 case 2: 00301 return ki18nc("Gregorian month 2 - KLocale::LongName", "February").toString(locale()); 00302 case 3: 00303 return ki18nc("Gregorian month 3 - KLocale::LongName", "March").toString(locale()); 00304 case 4: 00305 return ki18nc("Gregorian month 4 - KLocale::LongName", "April").toString(locale()); 00306 case 5: 00307 return ki18nc("Gregorian month 5 - KLocale::LongName", "May").toString(locale()); 00308 case 6: 00309 return ki18nc("Gregorian month 6 - KLocale::LongName", "June").toString(locale()); 00310 case 7: 00311 return ki18nc("Gregorian month 7 - KLocale::LongName", "July").toString(locale()); 00312 case 8: 00313 return ki18nc("Gregorian month 8 - KLocale::LongName", "August").toString(locale()); 00314 case 9: 00315 return ki18nc("Gregorian month 9 - KLocale::LongName", "September").toString(locale()); 00316 case 10: 00317 return ki18nc("Gregorian month 10 - KLocale::LongName", "October").toString(locale()); 00318 case 11: 00319 return ki18nc("Gregorian month 11 - KLocale::LongName", "November").toString(locale()); 00320 case 12: 00321 return ki18nc("Gregorian month 12 - KLocale::LongName", "December").toString(locale()); 00322 default: 00323 return QString(); 00324 } 00325 } 00326 00327 QString KCalendarSystemQDatePrivate::weekDayName(int weekDay, KLocale::DateTimeComponentFormat format) const 00328 { 00329 if (format == KLocale::NarrowName) { 00330 switch (weekDay) { 00331 case 1: 00332 return ki18nc("Gregorian weekday 1 - KLocale::NarrowName ", "M").toString(locale()); 00333 case 2: 00334 return ki18nc("Gregorian weekday 2 - KLocale::NarrowName ", "T").toString(locale()); 00335 case 3: 00336 return ki18nc("Gregorian weekday 3 - KLocale::NarrowName ", "W").toString(locale()); 00337 case 4: 00338 return ki18nc("Gregorian weekday 4 - KLocale::NarrowName ", "T").toString(locale()); 00339 case 5: 00340 return ki18nc("Gregorian weekday 5 - KLocale::NarrowName ", "F").toString(locale()); 00341 case 6: 00342 return ki18nc("Gregorian weekday 6 - KLocale::NarrowName ", "S").toString(locale()); 00343 case 7: 00344 return ki18nc("Gregorian weekday 7 - KLocale::NarrowName ", "S").toString(locale()); 00345 default: 00346 return QString(); 00347 } 00348 } 00349 00350 if (format == KLocale::ShortName || format == KLocale:: ShortNumber) { 00351 switch (weekDay) { 00352 case 1: 00353 return ki18nc("Gregorian weekday 1 - KLocale::ShortName", "Mon").toString(locale()); 00354 case 2: 00355 return ki18nc("Gregorian weekday 2 - KLocale::ShortName", "Tue").toString(locale()); 00356 case 3: 00357 return ki18nc("Gregorian weekday 3 - KLocale::ShortName", "Wed").toString(locale()); 00358 case 4: 00359 return ki18nc("Gregorian weekday 4 - KLocale::ShortName", "Thu").toString(locale()); 00360 case 5: 00361 return ki18nc("Gregorian weekday 5 - KLocale::ShortName", "Fri").toString(locale()); 00362 case 6: 00363 return ki18nc("Gregorian weekday 6 - KLocale::ShortName", "Sat").toString(locale()); 00364 case 7: 00365 return ki18nc("Gregorian weekday 7 - KLocale::ShortName", "Sun").toString(locale()); 00366 default: return QString(); 00367 } 00368 } 00369 00370 switch (weekDay) { 00371 case 1: 00372 return ki18nc("Gregorian weekday 1 - KLocale::LongName", "Monday").toString(locale()); 00373 case 2: 00374 return ki18nc("Gregorian weekday 2 - KLocale::LongName", "Tuesday").toString(locale()); 00375 case 3: 00376 return ki18nc("Gregorian weekday 3 - KLocale::LongName", "Wednesday").toString(locale()); 00377 case 4: 00378 return ki18nc("Gregorian weekday 4 - KLocale::LongName", "Thursday").toString(locale()); 00379 case 5: 00380 return ki18nc("Gregorian weekday 5 - KLocale::LongName", "Friday").toString(locale()); 00381 case 6: 00382 return ki18nc("Gregorian weekday 6 - KLocale::LongName", "Saturday").toString(locale()); 00383 case 7: 00384 return ki18nc("Gregorian weekday 7 - KLocale::LongName", "Sunday").toString(locale()); 00385 default: 00386 return QString(); 00387 } 00388 } 00389 00390 00391 KCalendarSystemQDate::KCalendarSystemQDate(const KLocale *locale) 00392 : KCalendarSystem(*new KCalendarSystemQDatePrivate(this), KSharedConfig::Ptr(), locale) 00393 { 00394 d_ptr->loadConfig(calendarType()); 00395 } 00396 00397 KCalendarSystemQDate::KCalendarSystemQDate(const KSharedConfig::Ptr config, const KLocale *locale) 00398 : KCalendarSystem(*new KCalendarSystemQDatePrivate(this), config, locale) 00399 { 00400 d_ptr->loadConfig(calendarType()); 00401 } 00402 00403 KCalendarSystemQDate::KCalendarSystemQDate(KCalendarSystemQDatePrivate &dd, const KSharedConfig::Ptr config, const KLocale *locale) 00404 : KCalendarSystem(dd, config, locale) 00405 { 00406 d_ptr->loadConfig(calendarType()); 00407 } 00408 00409 KCalendarSystemQDate::~KCalendarSystemQDate() 00410 { 00411 } 00412 00413 QString KCalendarSystemQDate::calendarType() const 00414 { 00415 return QLatin1String("gregorian"); 00416 } 00417 00418 QDate KCalendarSystemQDate::epoch() const 00419 { 00420 // 1 Jan 1 AD in Julian 00421 return QDate::fromJulianDay(1721424); 00422 } 00423 00424 QDate KCalendarSystemQDate::earliestValidDate() const 00425 { 00426 // 1 Jan 4712 BC, no year zero, cant be 4713BC due to error in QDate that day 0 is not valid 00427 // and we really need the first in each year to be valid for the date maths 00428 return QDate::fromJulianDay(366); 00429 } 00430 00431 QDate KCalendarSystemQDate::latestValidDate() const 00432 { 00433 // Set to last day of year 9999 until confirm date formats & widets support > 9999 00434 // In Gregorian this is 9999-12-31, which is is jd 5373484 00435 // Can't call setDate( 9999, 12, 31 ) as it creates circular reference! 00436 return QDate::fromJulianDay(5373484); 00437 } 00438 00439 bool KCalendarSystemQDate::isValid(int year, int month, int day) const 00440 { 00441 // Limit to max year 9999 for now, QDate allows to be greater 00442 if (year <= 9999) { 00443 return QDate::isValid(year, month, day); 00444 } 00445 00446 return false; 00447 } 00448 00449 bool KCalendarSystemQDate::isValid(const QDate &date) const 00450 { 00451 return KCalendarSystem::isValid(date); 00452 } 00453 00454 int KCalendarSystemQDate::year(const QDate &date) const 00455 { 00456 return date.year(); 00457 } 00458 00459 int KCalendarSystemQDate::month(const QDate &date) const 00460 { 00461 return date.month(); 00462 } 00463 00464 int KCalendarSystemQDate::day(const QDate &date) const 00465 { 00466 return date.day(); 00467 } 00468 00469 int KCalendarSystemQDate::daysInYear(const QDate &date) const 00470 { 00471 return date.daysInYear(); 00472 } 00473 00474 int KCalendarSystemQDate::daysInMonth(const QDate &date) const 00475 { 00476 return date.daysInMonth(); 00477 } 00478 00479 int KCalendarSystemQDate::dayOfYear(const QDate &date) const 00480 { 00481 return date.dayOfYear(); 00482 } 00483 00484 int KCalendarSystemQDate::dayOfWeek(const QDate &date) const 00485 { 00486 return date.dayOfWeek(); 00487 } 00488 00489 bool KCalendarSystemQDate::isLeapYear(int year) const 00490 { 00491 return QDate::isLeapYear(year); 00492 } 00493 00494 bool KCalendarSystemQDate::isLeapYear(const QDate &date) const 00495 { 00496 return QDate::isLeapYear(date.year()); 00497 } 00498 00499 QString KCalendarSystemQDate::monthName(int month, int year, MonthNameFormat format) const 00500 { 00501 return KCalendarSystem::monthName(month, year, format); 00502 } 00503 00504 QString KCalendarSystemQDate::monthName(const QDate &date, MonthNameFormat format) const 00505 { 00506 return KCalendarSystem::monthName(date, format); 00507 } 00508 00509 QString KCalendarSystemQDate::weekDayName(int weekDay, WeekDayNameFormat format) const 00510 { 00511 return KCalendarSystem::weekDayName(weekDay, format); 00512 } 00513 00514 QString KCalendarSystemQDate::weekDayName(const QDate &date, WeekDayNameFormat format) const 00515 { 00516 return KCalendarSystem::weekDayName(date, format); 00517 } 00518 00519 int KCalendarSystemQDate::weekDayOfPray() const 00520 { 00521 return 7; // sunday 00522 } 00523 00524 bool KCalendarSystemQDate::isLunar() const 00525 { 00526 return false; 00527 } 00528 00529 bool KCalendarSystemQDate::isLunisolar() const 00530 { 00531 return false; 00532 } 00533 00534 bool KCalendarSystemQDate::isSolar() const 00535 { 00536 return true; 00537 } 00538 00539 bool KCalendarSystemQDate::isProleptic() const 00540 { 00541 return false; 00542 } 00543 00544 bool KCalendarSystemQDate::julianDayToDate(int jd, int &year, int &month, int &day) const 00545 { 00546 QDate date = QDate::fromJulianDay(jd); 00547 00548 date.getDate(&year, &month, &day); 00549 00550 return date.isValid(); 00551 } 00552 00553 bool KCalendarSystemQDate::dateToJulianDay(int year, int month, int day, int &jd) const 00554 { 00555 QDate date; 00556 00557 date.setDate(year, month, day); 00558 jd = date.toJulianDay(); 00559 00560 return date.isValid(); 00561 }
KDE 4.7 API Reference