• Skip to content
  • Skip to link menu
KDE 4.5 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

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

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  •     Sodep
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.9-20090814
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal