KDECore
kcalendarsystemindiannational.cpp
Go to the documentation of this file.
00001 /* 00002 Copyright 2009, 2010 John Layt <john@layt.net> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "kcalendarsystemindiannational_p.h" 00021 #include "kcalendarsystemprivate_p.h" 00022 00023 #include "kdebug.h" 00024 #include "klocale.h" 00025 00026 #include <QtCore/QDate> 00027 #include <QtCore/QCharRef> 00028 00029 class KCalendarSystemIndianNationalPrivate : public KCalendarSystemPrivate 00030 { 00031 public: 00032 explicit KCalendarSystemIndianNationalPrivate(KCalendarSystemIndianNational *q); 00033 00034 virtual ~KCalendarSystemIndianNationalPrivate(); 00035 00036 // Virtual methods each calendar system must re-implement 00037 virtual KLocale::CalendarSystem calendarSystem() const; 00038 virtual void loadDefaultEraList(); 00039 virtual int monthsInYear(int year) const; 00040 virtual int daysInMonth(int year, int month) const; 00041 virtual int daysInYear(int year) const; 00042 virtual int daysInWeek() const; 00043 virtual bool isLeapYear(int year) const; 00044 virtual bool hasLeapMonths() const; 00045 virtual bool hasYearZero() const; 00046 virtual int maxDaysInWeek() const; 00047 virtual int maxMonthsInYear() const; 00048 virtual int earliestValidYear() const; 00049 virtual int latestValidYear() const; 00050 virtual QString monthName(int month, int year, KLocale::DateTimeComponentFormat format, bool possessive) const; 00051 virtual QString weekDayName(int weekDay, KLocale::DateTimeComponentFormat format) const; 00052 }; 00053 00054 // Shared d pointer base class definitions 00055 00056 KCalendarSystemIndianNationalPrivate::KCalendarSystemIndianNationalPrivate(KCalendarSystemIndianNational *q) 00057 : KCalendarSystemPrivate(q) 00058 { 00059 } 00060 00061 KCalendarSystemIndianNationalPrivate::~KCalendarSystemIndianNationalPrivate() 00062 { 00063 } 00064 00065 KLocale::CalendarSystem KCalendarSystemIndianNationalPrivate::calendarSystem() const 00066 { 00067 return KLocale::IndianNationalCalendar; 00068 } 00069 00070 void KCalendarSystemIndianNationalPrivate::loadDefaultEraList() 00071 { 00072 QString name, shortName, format; 00073 // Saka Era 00074 name = i18nc("Calendar Era: Indian National Saka Era, years > 0, LongFormat", "Saka Era"); 00075 shortName = i18nc("Calendar Era: Indian National Saka Era, years > 0, ShortFormat", "SE"); 00076 format = i18nc("(kdedt-format) Indian National, SE, full era year format used for %EY, e.g. 2000 SE", "%Ey %EC"); 00077 addEra('+', 1, q->epoch(), 1, q->latestValidDate(), name, shortName, format); 00078 } 00079 00080 int KCalendarSystemIndianNationalPrivate::monthsInYear(int year) const 00081 { 00082 Q_UNUSED(year) 00083 return 12; 00084 } 00085 00086 int KCalendarSystemIndianNationalPrivate::daysInMonth(int year, int month) const 00087 { 00088 if (month == 1) { 00089 if (isLeapYear(year)) { 00090 return 31; 00091 } else { 00092 return 30; 00093 } 00094 } 00095 00096 if (month >= 2 && month <= 6) { 00097 return 31; 00098 } 00099 00100 return 30; 00101 } 00102 00103 int KCalendarSystemIndianNationalPrivate::daysInYear(int year) const 00104 { 00105 if (isLeapYear(year)) { 00106 return 366; 00107 } else { 00108 return 365; 00109 } 00110 } 00111 00112 int KCalendarSystemIndianNationalPrivate::daysInWeek() const 00113 { 00114 return 7; 00115 } 00116 00117 bool KCalendarSystemIndianNationalPrivate::isLeapYear(int year) const 00118 { 00119 //Uses same rule as Gregorian, and is explicitly synchronized to Gregorian 00120 //so add 78 years to get Gregorian year and apply Gregorian calculation 00121 year = year + 78; 00122 if (!hasYearZero() && year < 1) { 00123 year = year + 1; 00124 } 00125 00126 if (year % 4 == 0) { 00127 if (year % 100 != 0) { 00128 return true; 00129 } else if (year % 400 == 0) { 00130 return true; 00131 } 00132 } 00133 00134 return false; 00135 } 00136 00137 bool KCalendarSystemIndianNationalPrivate::hasLeapMonths() const 00138 { 00139 return false; 00140 } 00141 00142 bool KCalendarSystemIndianNationalPrivate::hasYearZero() const 00143 { 00144 return true; 00145 } 00146 00147 int KCalendarSystemIndianNationalPrivate::maxDaysInWeek() const 00148 { 00149 return 7; 00150 } 00151 00152 int KCalendarSystemIndianNationalPrivate::maxMonthsInYear() const 00153 { 00154 return 12; 00155 } 00156 00157 int KCalendarSystemIndianNationalPrivate::earliestValidYear() const 00158 { 00159 return 0; 00160 } 00161 00162 int KCalendarSystemIndianNationalPrivate::latestValidYear() const 00163 { 00164 return 9999; 00165 } 00166 00167 QString KCalendarSystemIndianNationalPrivate::monthName(int month, int year, KLocale::DateTimeComponentFormat format, bool possessive) const 00168 { 00169 Q_UNUSED(year); 00170 00171 if (format == KLocale::NarrowName) { 00172 switch (month) { 00173 case 1: 00174 return ki18nc("Indian National month 1 - KLocale::NarrowName", "C").toString(locale()); 00175 case 2: 00176 return ki18nc("Indian National month 2 - KLocale::NarrowName", "V").toString(locale()); 00177 case 3: 00178 return ki18nc("Indian National month 3 - KLocale::NarrowName", "J").toString(locale()); 00179 case 4: 00180 return ki18nc("Indian National month 4 - KLocale::NarrowName", "Ā").toString(locale()); 00181 case 5: 00182 return ki18nc("Indian National month 5 - KLocale::NarrowName", "S").toString(locale()); 00183 case 6: 00184 return ki18nc("Indian National month 6 - KLocale::NarrowName", "B").toString(locale()); 00185 case 7: 00186 return ki18nc("Indian National month 7 - KLocale::NarrowName", "Ā").toString(locale()); 00187 case 8: 00188 return ki18nc("Indian National month 8 - KLocale::NarrowName", "K").toString(locale()); 00189 case 9: 00190 return ki18nc("Indian National month 9 - KLocale::NarrowName", "A").toString(locale()); 00191 case 10: 00192 return ki18nc("Indian National month 10 - KLocale::NarrowName", "P").toString(locale()); 00193 case 11: 00194 return ki18nc("Indian National month 11 - KLocale::NarrowName", "M").toString(locale()); 00195 case 12: 00196 return ki18nc("Indian National month 12 - KLocale::NarrowName", "P").toString(locale()); 00197 default: 00198 return QString(); 00199 } 00200 } 00201 00202 if (format == KLocale::ShortName && possessive) { 00203 switch (month) { 00204 case 1: 00205 return ki18nc("Indian National month 1 - KLocale::ShortName Possessive", "of Cha").toString(locale()); 00206 case 2: 00207 return ki18nc("Indian National month 2 - KLocale::ShortName Possessive", "of Vai").toString(locale()); 00208 case 3: 00209 return ki18nc("Indian National month 3 - KLocale::ShortName Possessive", "of Jya").toString(locale()); 00210 case 4: 00211 return ki18nc("Indian National month 4 - KLocale::ShortName Possessive", "of Āsh").toString(locale()); 00212 case 5: 00213 return ki18nc("Indian National month 5 - KLocale::ShortName Possessive", "of Shr").toString(locale()); 00214 case 6: 00215 return ki18nc("Indian National month 6 - KLocale::ShortName Possessive", "of Bhā").toString(locale()); 00216 case 7: 00217 return ki18nc("Indian National month 7 - KLocale::ShortName Possessive", "of Āsw").toString(locale()); 00218 case 8: 00219 return ki18nc("Indian National month 8 - KLocale::ShortName Possessive", "of Kār").toString(locale()); 00220 case 9: 00221 return ki18nc("Indian National month 9 - KLocale::ShortName Possessive", "of Agr").toString(locale()); 00222 case 10: 00223 return ki18nc("Indian National month 10 - KLocale::ShortName Possessive", "of Pau").toString(locale()); 00224 case 11: 00225 return ki18nc("Indian National month 11 - KLocale::ShortName Possessive", "of Māg").toString(locale()); 00226 case 12: 00227 return ki18nc("Indian National month 12 - KLocale::ShortName Possessive", "of Phā").toString(locale()); 00228 default: 00229 return QString(); 00230 } 00231 } 00232 00233 if (format == KLocale::ShortName && !possessive) { 00234 switch (month) { 00235 case 1: 00236 return ki18nc("Indian National month 1 - KLocale::ShortName", "Cha").toString(locale()); 00237 case 2: 00238 return ki18nc("Indian National month 2 - KLocale::ShortName", "Vai").toString(locale()); 00239 case 3: 00240 return ki18nc("Indian National month 3 - KLocale::ShortName", "Jya").toString(locale()); 00241 case 4: 00242 return ki18nc("Indian National month 4 - KLocale::ShortName", "Āsh").toString(locale()); 00243 case 5: 00244 return ki18nc("Indian National month 5 - KLocale::ShortName", "Shr").toString(locale()); 00245 case 6: 00246 return ki18nc("Indian National month 6 - KLocale::ShortName", "Bhā").toString(locale()); 00247 case 7: 00248 return ki18nc("Indian National month 7 - KLocale::ShortName", "Āsw").toString(locale()); 00249 case 8: 00250 return ki18nc("Indian National month 8 - KLocale::ShortName", "Kār").toString(locale()); 00251 case 9: 00252 return ki18nc("Indian National month 9 - KLocale::ShortName", "Agr").toString(locale()); 00253 case 10: 00254 return ki18nc("Indian National month 10 - KLocale::ShortName", "Pau").toString(locale()); 00255 case 11: 00256 return ki18nc("Indian National month 11 - KLocale::ShortName", "Māg").toString(locale()); 00257 case 12: 00258 return ki18nc("Indian National month 12 - KLocale::ShortName", "Phā").toString(locale()); 00259 default: 00260 return QString(); 00261 } 00262 } 00263 00264 if (format == KLocale::LongName && possessive) { 00265 switch (month) { 00266 case 1: 00267 return ki18nc("Indian National month 1 - KLocale::LongName Possessive", "of Chaitra").toString(locale()); 00268 case 2: 00269 return ki18nc("Indian National month 2 - KLocale::LongName Possessive", "of Vaishākh").toString(locale()); 00270 case 3: 00271 return ki18nc("Indian National month 3 - KLocale::LongName Possessive", "of Jyaishtha").toString(locale()); 00272 case 4: 00273 return ki18nc("Indian National month 4 - KLocale::LongName Possessive", "of Āshādha").toString(locale()); 00274 case 5: 00275 return ki18nc("Indian National month 5 - KLocale::LongName Possessive", "of Shrāvana").toString(locale()); 00276 case 6: 00277 return ki18nc("Indian National month 6 - KLocale::LongName Possessive", "of Bhādrapad").toString(locale()); 00278 case 7: 00279 return ki18nc("Indian National month 7 - KLocale::LongName Possessive", "of Āshwin").toString(locale()); 00280 case 8: 00281 return ki18nc("Indian National month 8 - KLocale::LongName Possessive", "of Kārtik").toString(locale()); 00282 case 9: 00283 return ki18nc("Indian National month 9 - KLocale::LongName Possessive", "of Agrahayana").toString(locale()); 00284 case 10: 00285 return ki18nc("Indian National month 10 - KLocale::LongName Possessive", "of Paush").toString(locale()); 00286 case 11: 00287 return ki18nc("Indian National month 11 - KLocale::LongName Possessive", "of Māgh").toString(locale()); 00288 case 12: 00289 return ki18nc("Indian National month 12 - KLocale::LongName Possessive", "of Phālgun").toString(locale()); 00290 default: 00291 return QString(); 00292 } 00293 } 00294 00295 // Default to LongName 00296 switch (month) { 00297 case 1: 00298 return ki18nc("Indian National month 1 - KLocale::LongName", "Chaitra").toString(locale()); 00299 case 2: 00300 return ki18nc("Indian National month 2 - KLocale::LongName", "Vaishākh").toString(locale()); 00301 case 3: 00302 return ki18nc("Indian National month 3 - KLocale::LongName", "Jyaishtha").toString(locale()); 00303 case 4: 00304 return ki18nc("Indian National month 4 - KLocale::LongName", "Āshādha").toString(locale()); 00305 case 5: 00306 return ki18nc("Indian National month 5 - KLocale::LongName", "Shrāvana").toString(locale()); 00307 case 6: 00308 return ki18nc("Indian National month 6 - KLocale::LongName", "Bhādrapad").toString(locale()); 00309 case 7: 00310 return ki18nc("Indian National month 7 - KLocale::LongName", "Āshwin").toString(locale()); 00311 case 8: 00312 return ki18nc("Indian National month 8 - KLocale::LongName", "Kārtik").toString(locale()); 00313 case 9: 00314 return ki18nc("Indian National month 9 - KLocale::LongName", "Agrahayana").toString(locale()); 00315 case 10: 00316 return ki18nc("Indian National month 10 - KLocale::LongName", "Paush").toString(locale()); 00317 case 11: 00318 return ki18nc("Indian National month 11 - KLocale::LongName", "Māgh").toString(locale()); 00319 case 12: 00320 return ki18nc("Indian National month 12 - KLocale::LongName", "Phālgun").toString(locale()); 00321 default: 00322 return QString(); 00323 } 00324 } 00325 00326 QString KCalendarSystemIndianNationalPrivate::weekDayName(int weekDay, KLocale::DateTimeComponentFormat format) const 00327 { 00328 if (format == KLocale::NarrowName) { 00329 switch (weekDay) { 00330 case 1: 00331 return ki18nc("Indian National weekday 1 - KLocale::NarrowName ", "S").toString(locale()); 00332 case 2: 00333 return ki18nc("Indian National weekday 2 - KLocale::NarrowName ", "M").toString(locale()); 00334 case 3: 00335 return ki18nc("Indian National weekday 3 - KLocale::NarrowName ", "B").toString(locale()); 00336 case 4: 00337 return ki18nc("Indian National weekday 4 - KLocale::NarrowName ", "G").toString(locale()); 00338 case 5: 00339 return ki18nc("Indian National weekday 5 - KLocale::NarrowName ", "S").toString(locale()); 00340 case 6: 00341 return ki18nc("Indian National weekday 6 - KLocale::NarrowName ", "S").toString(locale()); 00342 case 7: 00343 return ki18nc("Indian National weekday 7 - KLocale::NarrowName ", "R").toString(locale()); 00344 default: 00345 return QString(); 00346 } 00347 } 00348 00349 if (format == KLocale::ShortName || format == KLocale:: ShortNumber) { 00350 switch (weekDay) { 00351 case 1: 00352 return ki18nc("Indian National weekday 1 - KLocale::ShortName", "Som").toString(locale()); 00353 case 2: 00354 return ki18nc("Indian National weekday 2 - KLocale::ShortName", "Mañ").toString(locale()); 00355 case 3: 00356 return ki18nc("Indian National weekday 3 - KLocale::ShortName", "Bud").toString(locale()); 00357 case 4: 00358 return ki18nc("Indian National weekday 4 - KLocale::ShortName", "Gur").toString(locale()); 00359 case 5: 00360 return ki18nc("Indian National weekday 5 - KLocale::ShortName", "Suk").toString(locale()); 00361 case 6: 00362 return ki18nc("Indian National weekday 6 - KLocale::ShortName", "San").toString(locale()); 00363 case 7: 00364 return ki18nc("Indian National weekday 7 - KLocale::ShortName", "Rav").toString(locale()); 00365 default: return QString(); 00366 } 00367 } 00368 00369 switch (weekDay) { 00370 case 1: 00371 return ki18nc("Indian National weekday 1 - KLocale::LongName", "Somavãra").toString(locale()); 00372 case 2: 00373 return ki18nc("Indian National weekday 2 - KLocale::LongName", "Mañgalvã").toString(locale()); 00374 case 3: 00375 return ki18nc("Indian National weekday 3 - KLocale::LongName", "Budhavãra").toString(locale()); 00376 case 4: 00377 return ki18nc("Indian National weekday 4 - KLocale::LongName", "Guruvãra").toString(locale()); 00378 case 5: 00379 return ki18nc("Indian National weekday 5 - KLocale::LongName", "Sukravãra").toString(locale()); 00380 case 6: 00381 return ki18nc("Indian National weekday 6 - KLocale::LongName", "Sanivãra").toString(locale()); 00382 case 7: 00383 return ki18nc("Indian National weekday 7 - KLocale::LongName", "Raviãra").toString(locale()); 00384 default: 00385 return QString(); 00386 } 00387 } 00388 00389 00390 KCalendarSystemIndianNational::KCalendarSystemIndianNational(const KLocale *locale) 00391 : KCalendarSystem(*new KCalendarSystemIndianNationalPrivate(this), KSharedConfig::Ptr(), locale) 00392 { 00393 d_ptr->loadConfig(calendarType()); 00394 } 00395 00396 KCalendarSystemIndianNational::KCalendarSystemIndianNational(const KSharedConfig::Ptr config, const KLocale *locale) 00397 : KCalendarSystem(*new KCalendarSystemIndianNationalPrivate(this), config, locale) 00398 { 00399 d_ptr->loadConfig(calendarType()); 00400 } 00401 00402 KCalendarSystemIndianNational::KCalendarSystemIndianNational(KCalendarSystemIndianNationalPrivate &dd, 00403 const KSharedConfig::Ptr config, 00404 const KLocale *locale) 00405 : KCalendarSystem(dd, config, locale) 00406 { 00407 d_ptr->loadConfig(calendarType()); 00408 } 00409 00410 KCalendarSystemIndianNational::~KCalendarSystemIndianNational() 00411 { 00412 } 00413 00414 QString KCalendarSystemIndianNational::calendarType() const 00415 { 00416 return QLatin1String("indian-national"); 00417 } 00418 00419 QDate KCalendarSystemIndianNational::epoch() const 00420 { 00421 //0000-01-01, has Year 0. 00422 //0078-03-22 AD Gregorian / 0078-03-24 AD Julian 00423 return QDate::fromJulianDay(1749994); 00424 } 00425 00426 QDate KCalendarSystemIndianNational::earliestValidDate() const 00427 { 00428 //0000-01-01, has Year 0. 00429 //0078-03-22 AD Gregorian / 0078-03-24 AD Julian 00430 //Don't do proleptic yet, need to check 00431 return QDate::fromJulianDay(1749630); 00432 } 00433 00434 QDate KCalendarSystemIndianNational::latestValidDate() const 00435 { 00436 // Set to last day of year 9999 until confirm date formats & widgets support > 9999 00437 //9999-12-30 00438 //10078-03-21 AD Gregorian 00439 return QDate::fromJulianDay(5402054); 00440 } 00441 00442 bool KCalendarSystemIndianNational::isValid(int year, int month, int day) const 00443 { 00444 return KCalendarSystem::isValid(year, month, day); 00445 } 00446 00447 bool KCalendarSystemIndianNational::isValid(const QDate &date) const 00448 { 00449 return KCalendarSystem::isValid(date); 00450 } 00451 00452 bool KCalendarSystemIndianNational::isLeapYear(int year) const 00453 { 00454 return KCalendarSystem::isLeapYear(year); 00455 } 00456 00457 bool KCalendarSystemIndianNational::isLeapYear(const QDate &date) const 00458 { 00459 return KCalendarSystem::isLeapYear(date); 00460 } 00461 00462 QString KCalendarSystemIndianNational::monthName(int month, int year, MonthNameFormat format) const 00463 { 00464 return KCalendarSystem::monthName(month, year, format); 00465 } 00466 00467 QString KCalendarSystemIndianNational::monthName(const QDate &date, MonthNameFormat format) const 00468 { 00469 return KCalendarSystem::monthName(date, format); 00470 } 00471 00472 QString KCalendarSystemIndianNational::weekDayName(int weekDay, WeekDayNameFormat format) const 00473 { 00474 return KCalendarSystem::weekDayName(weekDay, format); 00475 } 00476 00477 QString KCalendarSystemIndianNational::weekDayName(const QDate &date, WeekDayNameFormat format) const 00478 { 00479 return KCalendarSystem::weekDayName(date, format); 00480 } 00481 00482 int KCalendarSystemIndianNational::weekDayOfPray() const 00483 { 00484 return 7; // JPL ??? 00485 } 00486 00487 bool KCalendarSystemIndianNational::isLunar() const 00488 { 00489 return false; 00490 } 00491 00492 bool KCalendarSystemIndianNational::isLunisolar() const 00493 { 00494 return true; 00495 } 00496 00497 bool KCalendarSystemIndianNational::isSolar() const 00498 { 00499 return false; 00500 } 00501 00502 bool KCalendarSystemIndianNational::isProleptic() const 00503 { 00504 return false; 00505 } 00506 00507 bool KCalendarSystemIndianNational::julianDayToDate(int jd, int &year, int &month, int &day) const 00508 { 00509 int L, N, I, J, D, M, Y; 00510 00511 // "Explanatory Supplement to the Astronomical Almanac" 2006 section 12.94 pp 605-606 00512 // Originally from "Report of the Calendar Reform Committee" 1955 00513 L = jd + 68518; 00514 N = (4 * L) / 146097; 00515 L = L - (146097 * N + 3) / 4; 00516 I = (4000 * (L + 1)) / 1461001; 00517 L = L - (1461 * I) / 4 + 1; 00518 J = ((L - 1) / 31) * (1 - L / 185) + (L / 185) * ((L - 156) / 30 + 5) - L / 366; 00519 D = L - 31 * J + ((J + 2) / 8) * (J - 5); 00520 L = J / 11; 00521 M = J + 2 - 12 * L; 00522 Y = 100 * (N - 49) + L + I - 78; 00523 00524 day = D; 00525 month = M; 00526 year = Y; 00527 00528 return true; 00529 } 00530 00531 bool KCalendarSystemIndianNational::dateToJulianDay(int year, int month, int day, int &jd) const 00532 { 00533 int Y = year; 00534 int M = month; 00535 int D = day; 00536 00537 // "Explanatory Supplement to the Astronomical Almanac" 2006 section 12.94 pp 605-606 00538 // Originally from "Report of the Calendar Reform Committee" 1955 00539 jd = 365 * Y 00540 + (Y + 78 - 1 / M) / 4 00541 + 31 * M 00542 - (M + 9) / 11 00543 - (M / 7) * (M - 7) 00544 - (3 * ((Y + 78 - 1 / M) / 100 + 1)) / 4 00545 + D 00546 + 1749579; 00547 00548 return true; 00549 }
KDE 4.7 API Reference