KDECore
kcalendarsystemgregorian.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 gregorian kde calendar class 00023 00024 #include "kcalendarsystemgregorian_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 KCalendarSystemGregorianPrivate : public KCalendarSystemPrivate 00037 { 00038 public: 00039 explicit KCalendarSystemGregorianPrivate( KCalendarSystemGregorian *q ); 00040 00041 virtual ~KCalendarSystemGregorianPrivate(); 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 00058 bool m_useCommonEra; 00059 }; 00060 00061 // Shared d pointer implementations 00062 00063 KCalendarSystemGregorianPrivate::KCalendarSystemGregorianPrivate( KCalendarSystemGregorian *q ) 00064 :KCalendarSystemPrivate( q ), 00065 m_useCommonEra( false ) 00066 00067 { 00068 } 00069 00070 KCalendarSystemGregorianPrivate::~KCalendarSystemGregorianPrivate() 00071 { 00072 } 00073 00074 KLocale::CalendarSystem KCalendarSystemGregorianPrivate::calendarSystem() const 00075 { 00076 return KLocale::QDateCalendar; 00077 } 00078 00079 void KCalendarSystemGregorianPrivate::loadDefaultEraList() 00080 { 00081 QString name, shortName, format; 00082 00083 KConfigGroup lcg( config(), QString::fromLatin1( "Locale" ) ); 00084 KConfigGroup cg = lcg.group( QString::fromLatin1( "KCalendarSystem %1" ).arg( q->calendarType() ) ); 00085 m_useCommonEra = cg.readEntry( "UseCommonEra", false ); 00086 00087 if ( m_useCommonEra ) { 00088 name = i18nc( "Calendar Era: Gregorian Common Era, years < 0, LongFormat", "Before Common Era" ); 00089 shortName = i18nc( "Calendar Era: Gregorian Common Era, years < 0, ShortFormat", "BCE" ); 00090 } else { 00091 name = i18nc( "Calendar Era: Gregorian Christian Era, years < 0, LongFormat", "Before Christ" ); 00092 shortName = i18nc( "Calendar Era: Gregorian Christian Era, years < 0, ShortFormat", "BC" ); 00093 } 00094 format = i18nc( "(kdedt-format) Gregorian, BC, full era year format used for %EY, e.g. 2000 BC", "%Ey %EC" ); 00095 addEra( '-', 1, q->epoch().addDays( -1 ), -1, q->earliestValidDate(), name, shortName, format ); 00096 00097 if ( m_useCommonEra ) { 00098 name = i18nc( "Calendar Era: Gregorian Common Era, years > 0, LongFormat", "Common Era" ); 00099 shortName = i18nc( "Calendar Era: Gregorian Common Era, years > 0, ShortFormat", "CE" ); 00100 } else { 00101 name = i18nc( "Calendar Era: Gregorian Christian Era, years > 0, LongFormat", "Anno Domini" ); 00102 shortName = i18nc( "Calendar Era: Gregorian Christian Era, years > 0, ShortFormat", "AD" ); 00103 } 00104 format = i18nc( "(kdedt-format) Gregorian, AD, full era year format used for %EY, e.g. 2000 AD", "%Ey %EC" ); 00105 addEra( '+', 1, q->epoch(), 1, q->latestValidDate(), name, shortName, format ); 00106 } 00107 00108 int KCalendarSystemGregorianPrivate::monthsInYear( int year ) const 00109 { 00110 Q_UNUSED( year ) 00111 return 12; 00112 } 00113 00114 int KCalendarSystemGregorianPrivate::daysInMonth( int year, int month ) const 00115 { 00116 QDate tempDate( year, month, 1 ); 00117 return tempDate.daysInMonth(); 00118 } 00119 00120 int KCalendarSystemGregorianPrivate::daysInYear( int year ) const 00121 { 00122 QDate tempDate( year, 1, 1 ); 00123 return tempDate.daysInYear(); 00124 } 00125 00126 int KCalendarSystemGregorianPrivate::daysInWeek() const 00127 { 00128 return 7; 00129 } 00130 00131 bool KCalendarSystemGregorianPrivate::isLeapYear( int year ) const 00132 { 00133 return QDate::isLeapYear( year ); 00134 } 00135 00136 bool KCalendarSystemGregorianPrivate::hasLeapMonths() const 00137 { 00138 return false; 00139 } 00140 00141 bool KCalendarSystemGregorianPrivate::hasYearZero() const 00142 { 00143 return false; 00144 } 00145 00146 int KCalendarSystemGregorianPrivate::maxDaysInWeek() const 00147 { 00148 return 7; 00149 } 00150 00151 int KCalendarSystemGregorianPrivate::maxMonthsInYear() const 00152 { 00153 return 12; 00154 } 00155 00156 int KCalendarSystemGregorianPrivate::earliestValidYear() const 00157 { 00158 return -4712; 00159 } 00160 00161 int KCalendarSystemGregorianPrivate::latestValidYear() const 00162 { 00163 return 9999; 00164 } 00165 00166 00167 KCalendarSystemGregorian::KCalendarSystemGregorian( const KLocale *locale ) 00168 : KCalendarSystem( *new KCalendarSystemGregorianPrivate( this ), KSharedConfig::Ptr(), locale ), 00169 dont_use( 0 ) 00170 { 00171 d_ptr->loadConfig( calendarType() ); 00172 } 00173 00174 KCalendarSystemGregorian::KCalendarSystemGregorian( const KSharedConfig::Ptr config, const KLocale *locale ) 00175 : KCalendarSystem( *new KCalendarSystemGregorianPrivate( this ), config, locale ), 00176 dont_use( 0 ) 00177 { 00178 d_ptr->loadConfig( calendarType() ); 00179 } 00180 00181 KCalendarSystemGregorian::KCalendarSystemGregorian( KCalendarSystemGregorianPrivate &dd, const KSharedConfig::Ptr config, const KLocale *locale ) 00182 : KCalendarSystem( dd, config, locale ), 00183 dont_use( 0 ) 00184 { 00185 d_ptr->loadConfig( calendarType() ); 00186 } 00187 00188 KCalendarSystemGregorian::~KCalendarSystemGregorian() 00189 { 00190 delete dont_use; 00191 } 00192 00193 QString KCalendarSystemGregorian::calendarType() const 00194 { 00195 return QLatin1String( "gregorian" ); 00196 } 00197 00198 QDate KCalendarSystemGregorian::epoch() const 00199 { 00200 // 1 Jan 1 AD in Julian 00201 return QDate::fromJulianDay( 1721424 ); 00202 } 00203 00204 QDate KCalendarSystemGregorian::earliestValidDate() const 00205 { 00206 // 1 Jan 4712 BC, no year zero, cant be 4713BC due to error in QDate that day 0 is not valid 00207 // and we really need the first in each year to be valid for the date maths 00208 return QDate::fromJulianDay( 366 ); 00209 } 00210 00211 QDate KCalendarSystemGregorian::latestValidDate() const 00212 { 00213 // Set to last day of year 9999 until confirm date formats & widets support > 9999 00214 // In Gregorian this is 9999-12-31, which is is jd 5373484 00215 // Can't call setDate( 9999, 12, 31 ) as it creates circular reference! 00216 return QDate::fromJulianDay( 5373484 ); 00217 } 00218 00219 bool KCalendarSystemGregorian::isValid( int year, int month, int day ) const 00220 { 00221 // Limit to max year 9999 for now, QDate allows to be greater 00222 if ( year <= 9999 ) { 00223 return QDate::isValid( year, month, day ); 00224 } 00225 00226 return false; 00227 } 00228 00229 bool KCalendarSystemGregorian::isValid( const QDate &date ) const 00230 { 00231 return KCalendarSystem::isValid( date ); 00232 } 00233 00234 bool KCalendarSystemGregorian::setDate( QDate &date, int year, int month, int day ) const 00235 { 00236 return KCalendarSystem::setDate( date, year, month, day ); 00237 } 00238 00239 // Deprecated 00240 bool KCalendarSystemGregorian::setYMD( QDate &date, int y, int m, int d ) const 00241 { 00242 return KCalendarSystem::setDate( date, y, m, d ); 00243 } 00244 00245 int KCalendarSystemGregorian::year( const QDate &date ) const 00246 { 00247 return date.year(); 00248 } 00249 00250 int KCalendarSystemGregorian::month( const QDate &date ) const 00251 { 00252 return date.month(); 00253 } 00254 00255 int KCalendarSystemGregorian::day( const QDate &date ) const 00256 { 00257 return date.day(); 00258 } 00259 00260 QDate KCalendarSystemGregorian::addYears( const QDate &date, int nyears ) const 00261 { 00262 return KCalendarSystem::addYears( date, nyears ); 00263 } 00264 00265 QDate KCalendarSystemGregorian::addMonths( const QDate &date, int nmonths ) const 00266 { 00267 return KCalendarSystem::addMonths( date, nmonths ); 00268 } 00269 00270 QDate KCalendarSystemGregorian::addDays( const QDate &date, int ndays ) const 00271 { 00272 return KCalendarSystem::addDays( date, ndays ); 00273 } 00274 00275 int KCalendarSystemGregorian::monthsInYear( const QDate &date ) const 00276 { 00277 return KCalendarSystem::monthsInYear( date ); 00278 } 00279 00280 int KCalendarSystemGregorian::weeksInYear( const QDate &date ) const 00281 { 00282 return KCalendarSystem::weeksInYear( date ); 00283 } 00284 00285 int KCalendarSystemGregorian::weeksInYear( int year ) const 00286 { 00287 return KCalendarSystem::weeksInYear( year ); 00288 } 00289 00290 int KCalendarSystemGregorian::daysInYear( const QDate &date ) const 00291 { 00292 return date.daysInYear(); 00293 } 00294 00295 int KCalendarSystemGregorian::daysInMonth( const QDate &date ) const 00296 { 00297 return date.daysInMonth(); 00298 } 00299 00300 int KCalendarSystemGregorian::daysInWeek( const QDate &date ) const 00301 { 00302 return KCalendarSystem::daysInWeek( date ); 00303 } 00304 00305 int KCalendarSystemGregorian::dayOfYear( const QDate &date ) const 00306 { 00307 return date.dayOfYear(); 00308 } 00309 00310 int KCalendarSystemGregorian::dayOfWeek( const QDate &date ) const 00311 { 00312 return date.dayOfWeek(); 00313 } 00314 00315 int KCalendarSystemGregorian::weekNumber( const QDate &date, int * yearNum ) const 00316 { 00317 return KCalendarSystem::weekNumber( date, yearNum ); 00318 } 00319 00320 bool KCalendarSystemGregorian::isLeapYear( int year ) const 00321 { 00322 return QDate::isLeapYear( year ); 00323 } 00324 00325 bool KCalendarSystemGregorian::isLeapYear( const QDate &date ) const 00326 { 00327 return QDate::isLeapYear( date.year() ); 00328 } 00329 00330 QString KCalendarSystemGregorian::monthName( int month, int year, MonthNameFormat format ) const 00331 { 00332 Q_UNUSED( year ); 00333 00334 if ( format == ShortNamePossessive ) { 00335 switch ( month ) { 00336 case 1: 00337 return ki18nc( "of January", "of Jan" ).toString( locale() ); 00338 case 2: 00339 return ki18nc( "of February", "of Feb" ).toString( locale() ); 00340 case 3: 00341 return ki18nc( "of March", "of Mar" ).toString( locale() ); 00342 case 4: 00343 return ki18nc( "of April", "of Apr" ).toString( locale() ); 00344 case 5: 00345 return ki18nc( "of May short", "of May" ).toString( locale() ); 00346 case 6: 00347 return ki18nc( "of June", "of Jun" ).toString( locale() ); 00348 case 7: 00349 return ki18nc( "of July", "of Jul" ).toString( locale() ); 00350 case 8: 00351 return ki18nc( "of August", "of Aug" ).toString( locale() ); 00352 case 9: 00353 return ki18nc( "of September", "of Sep" ).toString( locale() ); 00354 case 10: 00355 return ki18nc( "of October", "of Oct" ).toString( locale() ); 00356 case 11: 00357 return ki18nc( "of November", "of Nov" ).toString( locale() ); 00358 case 12: 00359 return ki18nc( "of December", "of Dec" ).toString( locale() ); 00360 default: 00361 return QString(); 00362 } 00363 } 00364 00365 if ( format == LongNamePossessive ) { 00366 switch ( month ) { 00367 case 1: 00368 return ki18n( "of January" ).toString( locale() ); 00369 case 2: 00370 return ki18n( "of February" ).toString( locale() ); 00371 case 3: 00372 return ki18n( "of March" ).toString( locale() ); 00373 case 4: 00374 return ki18n( "of April" ).toString( locale() ); 00375 case 5: 00376 return ki18nc( "of May long", "of May" ).toString( locale() ); 00377 case 6: 00378 return ki18n( "of June" ).toString( locale() ); 00379 case 7: 00380 return ki18n( "of July" ).toString( locale() ); 00381 case 8: 00382 return ki18n( "of August" ).toString( locale() ); 00383 case 9: 00384 return ki18n( "of September" ).toString( locale() ); 00385 case 10: 00386 return ki18n( "of October" ).toString( locale() ); 00387 case 11: 00388 return ki18n( "of November" ).toString( locale() ); 00389 case 12: 00390 return ki18n( "of December" ).toString( locale() ); 00391 default: 00392 return QString(); 00393 } 00394 } 00395 00396 if ( format == ShortName ) { 00397 switch ( month ) { 00398 case 1: 00399 return ki18nc( "January", "Jan" ).toString( locale() ); 00400 case 2: 00401 return ki18nc( "February", "Feb" ).toString( locale() ); 00402 case 3: 00403 return ki18nc( "March", "Mar" ).toString( locale() ); 00404 case 4: 00405 return ki18nc( "April", "Apr" ).toString( locale() ); 00406 case 5: 00407 return ki18nc( "May short", "May" ).toString( locale() ); 00408 case 6: 00409 return ki18nc( "June", "Jun" ).toString( locale() ); 00410 case 7: 00411 return ki18nc( "July", "Jul" ).toString( locale() ); 00412 case 8: 00413 return ki18nc( "August", "Aug" ).toString( locale() ); 00414 case 9: 00415 return ki18nc( "September", "Sep" ).toString( locale() ); 00416 case 10: 00417 return ki18nc( "October", "Oct" ).toString( locale() ); 00418 case 11: 00419 return ki18nc( "November", "Nov" ).toString( locale() ); 00420 case 12: 00421 return ki18nc( "December", "Dec" ).toString( locale() ); 00422 default: 00423 return QString(); 00424 } 00425 } 00426 00427 // Default to LongName 00428 switch ( month ) { 00429 case 1: 00430 return ki18n( "January" ).toString( locale() ); 00431 case 2: 00432 return ki18n( "February" ).toString( locale() ); 00433 case 3: 00434 return ki18nc( "March long", "March" ).toString( locale() ); 00435 case 4: 00436 return ki18n( "April" ).toString( locale() ); 00437 case 5: 00438 return ki18nc( "May long", "May" ).toString( locale() ); 00439 case 6: 00440 return ki18n( "June" ).toString( locale() ); 00441 case 7: 00442 return ki18n( "July" ).toString( locale() ); 00443 case 8: 00444 return ki18nc( "August long", "August" ).toString( locale() ); 00445 case 9: 00446 return ki18n( "September" ).toString( locale() ); 00447 case 10: 00448 return ki18n( "October" ).toString( locale() ); 00449 case 11: 00450 return ki18n( "November" ).toString( locale() ); 00451 case 12: 00452 return ki18n( "December" ).toString( locale() ); 00453 default: 00454 return QString(); 00455 } 00456 } 00457 00458 QString KCalendarSystemGregorian::monthName( const QDate &date, MonthNameFormat format ) const 00459 { 00460 return KCalendarSystem::monthName( date, format ); 00461 } 00462 00463 00464 QString KCalendarSystemGregorian::weekDayName( int weekDay, WeekDayNameFormat format ) const 00465 { 00466 if ( format == ShortDayName ) { 00467 switch ( weekDay ) { 00468 case 1: return ki18nc( "Monday", "Mon" ).toString( locale() ); 00469 case 2: return ki18nc( "Tuesday", "Tue" ).toString( locale() ); 00470 case 3: return ki18nc( "Wednesday", "Wed" ).toString( locale() ); 00471 case 4: return ki18nc( "Thursday", "Thu" ).toString( locale() ); 00472 case 5: return ki18nc( "Friday", "Fri" ).toString( locale() ); 00473 case 6: return ki18nc( "Saturday", "Sat" ).toString( locale() ); 00474 case 7: return ki18nc( "Sunday", "Sun" ).toString( locale() ); 00475 default: return QString(); 00476 } 00477 } 00478 00479 switch ( weekDay ) { 00480 case 1: return ki18n( "Monday" ).toString( locale() ); 00481 case 2: return ki18n( "Tuesday" ).toString( locale() ); 00482 case 3: return ki18n( "Wednesday" ).toString( locale() ); 00483 case 4: return ki18n( "Thursday" ).toString( locale() ); 00484 case 5: return ki18n( "Friday" ).toString( locale() ); 00485 case 6: return ki18n( "Saturday" ).toString( locale() ); 00486 case 7: return ki18n( "Sunday" ).toString( locale() ); 00487 default: return QString(); 00488 } 00489 } 00490 00491 QString KCalendarSystemGregorian::weekDayName( const QDate &date, WeekDayNameFormat format ) const 00492 { 00493 return KCalendarSystem::weekDayName( date, format ); 00494 } 00495 00496 QString KCalendarSystemGregorian::yearString( const QDate &pDate, StringFormat format ) const 00497 { 00498 return KCalendarSystem::yearString( pDate, format ); 00499 } 00500 00501 QString KCalendarSystemGregorian::monthString( const QDate &pDate, StringFormat format ) const 00502 { 00503 return KCalendarSystem::monthString( pDate, format ); 00504 } 00505 00506 QString KCalendarSystemGregorian::dayString( const QDate &pDate, StringFormat format ) const 00507 { 00508 return KCalendarSystem::dayString( pDate, format ); 00509 } 00510 00511 int KCalendarSystemGregorian::yearStringToInteger( const QString &sNum, int &iLength ) const 00512 { 00513 return KCalendarSystem::yearStringToInteger( sNum, iLength ); 00514 } 00515 00516 int KCalendarSystemGregorian::monthStringToInteger( const QString &sNum, int &iLength ) const 00517 { 00518 return KCalendarSystem::monthStringToInteger( sNum, iLength ); 00519 } 00520 00521 int KCalendarSystemGregorian::dayStringToInteger( const QString &sNum, int &iLength ) const 00522 { 00523 return KCalendarSystem::dayStringToInteger( sNum, iLength ); 00524 } 00525 00526 QString KCalendarSystemGregorian::formatDate( const QDate &date, KLocale::DateFormat format ) const 00527 { 00528 return KCalendarSystem::formatDate( date, format ); 00529 } 00530 00531 QDate KCalendarSystemGregorian::readDate( const QString &str, bool *ok ) const 00532 { 00533 return KCalendarSystem::readDate( str, ok ); 00534 } 00535 00536 QDate KCalendarSystemGregorian::readDate( const QString &intstr, const QString &fmt, bool *ok ) const 00537 { 00538 return KCalendarSystem::readDate( intstr, fmt, ok ); 00539 } 00540 00541 QDate KCalendarSystemGregorian::readDate( const QString &str, KLocale::ReadDateFlags flags, bool *ok ) const 00542 { 00543 return KCalendarSystem::readDate( str, flags, ok ); 00544 } 00545 00546 int KCalendarSystemGregorian::weekStartDay() const 00547 { 00548 return KCalendarSystem::weekStartDay(); 00549 } 00550 00551 int KCalendarSystemGregorian::weekDayOfPray() const 00552 { 00553 return 7; // sunday 00554 } 00555 00556 bool KCalendarSystemGregorian::isLunar() const 00557 { 00558 return false; 00559 } 00560 00561 bool KCalendarSystemGregorian::isLunisolar() const 00562 { 00563 return false; 00564 } 00565 00566 bool KCalendarSystemGregorian::isSolar() const 00567 { 00568 return true; 00569 } 00570 00571 bool KCalendarSystemGregorian::isProleptic() const 00572 { 00573 return false; 00574 } 00575 00576 bool KCalendarSystemGregorian::julianDayToDate( int jd, int &year, int &month, int &day ) const 00577 { 00578 QDate date = QDate::fromJulianDay( jd ); 00579 00580 date.getDate( &year, &month, &day ); 00581 00582 return date.isValid(); 00583 } 00584 00585 bool KCalendarSystemGregorian::dateToJulianDay( int year, int month, int day, int &jd ) const 00586 { 00587 QDate date; 00588 00589 date.setDate( year, month, day ); 00590 jd = date.toJulianDay(); 00591 00592 return date.isValid(); 00593 }
KDE 4.6 API Reference