diff --git a/kdecore/date/kcalendarsystemhebrew.cpp b/kdecore/date/kcalendarsystemhebrew.cpp index 8e5f25a30a..fbe30d7920 100644 --- a/kdecore/date/kcalendarsystemhebrew.cpp +++ b/kdecore/date/kcalendarsystemhebrew.cpp @@ -1,1021 +1,962 @@ /* Copyright (c) 2003 Hans Petter Bieker Copyright (c) 2007 John Layt Calendar conversion routines based on Hdate v6, by Amos Shapir 1978 (rev. 1985, 1992) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ // Derived hebrew kde calendar class #include "kcalendarsystemhebrew_p.h" #include "kdebug.h" #include "klocale.h" #include #include -//=========================================================================== -// This section holds the old Hebrew <=> jd <=> Hebrew conversion code -// This is here to allow comparison testing to confirm old and new match -//=========================================================================== - static int hebrewDaysElapsed( int y ); static QString num2heb( int num, bool includeMillenium ); class h_date { public: int hd_day; int hd_mon; int hd_year; int hd_dw; int hd_flg; }; /* * compute general date structure from hebrew date */ static class h_date * hebrewToGregorian( int y, int m, int d ) { static class h_date h; int s; y -= 3744; s = hebrewDaysElapsed( y ); d += s; s = hebrewDaysElapsed( y + 1 ) - s; /* length of year */ if ( s > 365 && m > 6 ) { --m; d += 30; } d += ( 59 * ( m - 1 ) + 1 ) / 2; /* regular months */ /* special cases */ if ( s % 10 > 4 && m > 2 ) { /* long Heshvan */ d++; } if ( s % 10 < 4 && m > 3 ) { /* short Kislev */ d--; } // ### HPB: Broken in leap years //if (s > 365 && m > 6) /* leap year */ // d += 30; d -= 6002; y = ( d + 36525 ) * 4 / 146097 - 1; d -= y / 4 * 146097 + ( y % 4 ) * 36524; y *= 100; /* compute year */ s = ( d + 366 )*4 / 1461 - 1; d -= s / 4*1461 + ( s % 4 )*365; y += s; /* compute month */ m = ( d + 245 )*12 / 367 - 7; d -= m*367 / 12 - 30; if ( ++m >= 12 ) { m -= 12; y++; } h.hd_day = d; h.hd_mon = m; h.hd_year = y; return( &h ); } /* * compute date structure from no. of days since 1 Tishrei 3744 */ static class h_date * gregorianToHebrew( int y, int m, int d ) { static class h_date h; int s; if ( ( m -= 2 ) <= 0 ) { m += 12; y--; } /* no. of days, Julian calendar */ d += 365*y + y / 4 + 367*m / 12 + 5968; /* Gregorian calendar */ d -= y / 100 - y / 400 - 2; h.hd_dw = ( d + 1 ) % 7; /* compute the year */ y += 16; s = hebrewDaysElapsed( y ); m = hebrewDaysElapsed( y + 1 ); while( d >= m ) { /* computed year was underestimated */ s = m; y++; m = hebrewDaysElapsed( y + 1 ); } d -= s; s = m - s; /* size of current year */ y += 3744; h.hd_flg = s % 10 - 4; /* compute day and month */ if ( d >= s - 236 ) { /* last 8 months are regular */ d -= s - 236; m = d * 2 / 59; d -= ( m * 59 + 1 ) / 2; m += 4; if ( s > 365 && m <= 5 ) { /* Adar of Meuberet */ m += 8; } } else { /* first 4 months have 117-119 days */ s = 114 + s % 10; m = d * 4 / s; d -= ( m * s + 3 ) / 4; } h.hd_day = d; h.hd_mon = m; h.hd_year = y; return( &h ); } static QString num2heb( int num, bool includeMillenium ) { const QChar decade[] = { 0x05D8, 0x05D9, 0x05DB, 0x05DC, 0x05DE, 0x05E0, 0x05E1, 0x05E2, 0x05E4, 0x05E6 }; QString result; if ( num < 1 || num > 9999 ) { return QString::number( num ); } if ( num >= 1000 ) { if ( includeMillenium || num % 1000 == 0 ) result += QChar( 0x05D0 - 1 + num / 1000 ); num %= 1000; } if ( num >= 100 ) { while ( num >= 500 ) { result += QChar( 0x05EA ); num -= 400; } result += QChar( 0x05E7 - 1 + num / 100 ); num %= 100; } if ( num >= 10 ) { if ( num == 15 || num == 16 ) num -= 9; result += decade[num / 10]; num %= 10; } if ( num > 0 ) { result += QChar( 0x05D0 - 1 + num ); } if ( result.length() == 1 ) { result += '\''; } else { result.insert( result.length() - 1, '\"' ); } return result; } static int heb2num( const QString &str, int &iLength ) { QChar c; QString s = str; int result = 0; iLength = 0; int decadeValues[14] = {10, 20, 20, 30, 40, 40, 50, 50, 60, 70, 80, 80, 90, 90}; int pos; for ( pos = 0 ; pos < s.length() ; pos++ ) { c = s[pos]; if ( s.length() > pos && ( s[pos + 1] == QChar( '\'' ) || s[pos + 1] == QChar( '\"' ) ) ) { iLength++; s.remove( pos + 1, 1 ); } if ( c >= QChar( 0x05D0 ) && c <= QChar( 0x05D7 ) ) { if ( s.length() > pos && s[pos + 1] >= QChar( 0x05D0 ) && s[pos + 1] <= QChar( 0x05EA ) ) { result += ( c.unicode() - 0x05D0 + 1 ) * 1000; } else { result += c.unicode() - 0x05D0 + 1; } } else if ( c == QChar( 0x05D8 ) ) { if ( s.length() > pos && s[pos + 1] >= QChar( 0x05D0 ) && s[pos + 1] <= QChar( 0x05EA ) && s[pos + 1] != QChar( 0x05D5 ) && s[pos + 1] != QChar( 0x05D6 ) ) { result += 9000; } else { result += 9; } } else if ( c >= QChar( 0x05D9 ) && c <= QChar( 0x05E6 ) ) { if ( s.length() > pos && s[pos + 1] >= QChar( 0x05D9 ) ) { return -1; } else { result += decadeValues[c.unicode() - 0x05D9]; } } else if ( c >= QChar( 0x05E7 ) && c <= QChar( 0x05EA ) ) { result += ( c.unicode() - 0x05E7 + 1 ) * 100; } else { break; } } iLength += pos; return result; } /* constants, in 1/18th of minute */ static const int HOUR = 1080; static const int DAY = 24*HOUR; static const int WEEK = 7*DAY; #define M(h,p) ((h)*HOUR+p) #define MONTH (DAY+M(12,793)) /** * @internal * no. of days in y years */ static int hebrewDaysElapsed( int y ) { int m, nm, dw, s, l; l = y * 7 + 1; // no. of leap months m = y * 12 + l / 19; // total no. of months l %= 19; nm = m * MONTH + M( 1 + 6, 779 ); // molad new year 3744 (16BC) + 6 hours s = m * 28 + nm / DAY - 2; nm %= WEEK; dw = nm / DAY; nm %= DAY; // special cases of Molad Zaken if ( (l < 12 && dw == 3 && nm >= M( 9 + 6, 204 )) || (l < 7 && dw == 2 && nm >= M( 15 + 6, 589 )) ) { s++, dw++; } /* ADU */ if ( dw == 1 || dw == 4 || dw == 6 ) { s++; } return s; } /** * @internal * true if long Cheshvan */ static int long_cheshvan( int year ) { QDate first, last; class h_date *gd; gd = hebrewToGregorian( year, 1, 1 ); first.setYMD( gd->hd_year, gd->hd_mon + 1, gd->hd_day + 1 ); gd = hebrewToGregorian( year + 1, 1, 1 ); last.setYMD( gd->hd_year, gd->hd_mon + 1, gd->hd_day + 1 ); return ( first.daysTo( last ) % 10 == 5 ); } /** * @internal * true if short Kislev */ static int short_kislev( int year ) { QDate first, last; class h_date * gd; gd = hebrewToGregorian( year, 1, 1 ); first.setYMD( gd->hd_year, gd->hd_mon + 1, gd->hd_day + 1 ); gd = hebrewToGregorian( year + 1, 1, 1 ); last.setYMD( gd->hd_year, gd->hd_mon + 1, gd->hd_day + 1 ); return ( first.daysTo( last ) % 10 == 3 ); } -static bool is_leap_year( int year ) -{ - return ( ( ( ( 7 * year ) + 1 ) % 19 ) < 7 ); -} - // Ok static class h_date *toHebrew( const QDate &date ) { class h_date *sd; sd = gregorianToHebrew( date.year(), date.month(), date.day() ); ++sd->hd_mon; ++sd->hd_day; return sd; } -//=========================================================================== -// End of old code -//=========================================================================== - class KCalendarSystemHebrewPrivate { public: - int characterOfYear( int year ) const; - int julianDayOfTishri1( int year ) const; - int yearOfJulianDay( int jd ) const; - int daysPreceedingMonth( int character, int month ) const; - - /** - * Gets the number of days in a month for a given date - * - * @param year given year - * @param mon month number - * @return number of days in month - */ - int hndays( int year, int mon ) const; -}; - -int KCalendarSystemHebrewPrivate::daysPreceedingMonth( int character, int month ) const -{ - // FIXME move to Private class header as static when figure out how to do it without compiler errors :-) - int daysPreceedingMonthTable[6][13] = - { - {0, 30, 59, 88, 117, 147, 176, 206, 235, 265, 294, 324, }, - {0, 30, 59, 89, 118, 148, 177, 207, 236, 266, 295, 325, }, - {0, 30, 60, 90, 119, 149, 178, 208, 237, 267, 296, 326, }, - {0, 30, 59, 88, 117, 147, 177, 206, 236, 265, 295, 324, 354}, - {0, 30, 59, 89, 118, 148, 178, 207, 237, 266, 296, 325, 355}, - {0, 30, 60, 90, 119, 149, 179, 209, 238, 267, 297, 326, 356}, - }; + KCalendarSystemHebrewPrivate( KCalendarSystemHebrew *q ): q( q ) + { + } - if ( character >= 1 && character <= 6 && month >= 1 && month <= 13 ) { - return daysPreceedingMonthTable[character-1][month-1]; + ~KCalendarSystemHebrewPrivate() + { } - return -911; -} -int KCalendarSystemHebrewPrivate::characterOfYear( int year ) const + KCalendarSystem *q; + + bool isLeapYear( int year ) const; + + int monthNumberToMonthIndex( int year, int month ) const; + + int monthsInYear( int year ) const; + + int daysInMonth( int year, int month ) const; +}; + +bool KCalendarSystemHebrewPrivate::isLeapYear( int year ) const { - int jdTishri1ThisYear = julianDayOfTishri1( year ); - int jdTishri1NextYear = julianDayOfTishri1( year + 1 ); - int K = jdTishri1NextYear - jdTishri1ThisYear - 352 - - 27 * ( ( 7 * year + 13 ) % 19 ) / 12; - return K; + return ( ( ( ( 7 * year ) + 1 ) % 19 ) < 7 ); } -// Hatcher formula G -int KCalendarSystemHebrewPrivate::julianDayOfTishri1( int year ) const +int KCalendarSystemHebrewPrivate::monthNumberToMonthIndex( int year, int month ) const { - // if calc of t overflows, have alternative formula for step - long t = 31524 + 765433 * ( ( 235 * year - 234 ) / 19 ); - int d = t / 25920; - int t1 = t % 25920; - int w = 1 + ( d % 7 ); - int E = ( ( 7 * year + 13 ) % 19 ) / 12; - int E1 = ( ( 7 * year + 6 ) % 19 ) / 12; - if ( t1 >= 19940 || - ( t1 >= 9924 && w == 3 && E == 0 ) || - ( t1 >= 16788 && w == 2 && E == 0 && E1 == 1 ) ) { - d = d + 1; + if ( q->isLeapYear( year ) ) { + if ( month == 6 ) { + return 13; // Adar I + } else if ( month == 7 ) { + return 14; // Adar II + } else if ( month > 7 ) { + return month - 1; // Because of Adar II + } } - int jd = d + ( ( ( d + 5 ) % 7 ) % 2 ) + 347997; - return jd; + + return month; } -// Hatcher formula H -int KCalendarSystemHebrewPrivate::yearOfJulianDay( int jd ) const +int KCalendarSystemHebrewPrivate::monthsInYear( int year ) const { - long M = ( 25920 * ( jd - 347996 ) ) / 765433 + 1; - int year = 19 * ( M / 235 ) + ( 19 * ( M % 2356 ) - 2 ) / 235 + 1; - if ( julianDayOfTishri1( year ) > jd ) { - year = year - 1; + if ( isLeapYear( year ) ) { + return 13; + } else { + return 12; } - return year; } -int KCalendarSystemHebrewPrivate::hndays( int mon, int year ) const +int KCalendarSystemHebrewPrivate::daysInMonth( int year, int month ) const { - if ( mon == 6 && is_leap_year( year ) ) { - mon = 13; /*Adar I*/ - } else if ( mon == 7 && is_leap_year( year ) ) { - mon = 14; /*Adar II*/ - } else if ( mon > 7 && is_leap_year( year ) ) { - mon--; //Because of Adar II + int mi = monthNumberToMonthIndex( year, month ); + + if ( mi == 2 && long_cheshvan( year ) ) { + return 30; } - if( mon == 8 /*IYYAR*/ || mon == 10 /*TAMUZ*/ || - mon == 12 /*ELUL*/ || mon == 4 /*TEVET*/ || - mon == 14 /*ADAR 2*/ || - ( mon == 6 /*ADAR*/ && !is_leap_year( year ) ) || - ( mon == 2 /*CHESHVAN*/ && !long_cheshvan( year ) ) || - ( mon == 3 /*KISLEV*/ && short_kislev( year ) ) ) { + if ( mi == 3 && short_kislev( year ) ) { + return 29; + } + + if ( mi % 2 == 0 ) { return 29; } else { return 30; } } -// Ok KCalendarSystemHebrew::KCalendarSystemHebrew( const KLocale * locale ) - : KCalendarSystem( locale ), d( new KCalendarSystemHebrewPrivate ) + : KCalendarSystem( locale ), + d( new KCalendarSystemHebrewPrivate( this ) ) { setMaxMonthsInYear(13); } -// Ok KCalendarSystemHebrew::~KCalendarSystemHebrew() { delete d; } QString KCalendarSystemHebrew::calendarType() const { return QLatin1String( "hebrew" ); } QDate KCalendarSystemHebrew::epoch() const { + // Hebrew 0001-01-01 (Gregorian -3760-09-07, Julian -3761-10-07) return QDate::fromJulianDay( 347998 ); } QDate KCalendarSystemHebrew::earliestValidDate() const { - return KCalendarSystem::earliestValidDate(); + // Current formulas using direct Gregorian <-> Hebrew conversion using Qt + // will return invalid results prior to the Gregorian switchover in 1582 + // Next valid Hebrew year starts 5344-01-01 (Gregorian 1583-09-17) + return QDate::fromJulianDay( 2299498 ); } QDate KCalendarSystemHebrew::latestValidDate() const { - // Set to last day of year 9999 until confirm date formats & widets support > 9999 - // Last day of Hebrew year 9999 is 9999-12-29 - // Which in Gregorian is 6239-09-25 - // Which is jd xxxx FIXME Find out jd and use that instead - // Can't call setDate( 9999, 12, 29 ) as it creates circular reference! - return QDate( 6239, 9, 25 ); + // Testing shows current formulas only work up to 8119-13-29 (Gregorian 4359-10-07) + return QDate::fromJulianDay( 3313431 ); } -bool KCalendarSystemHebrew::isValid( int y, int month, int day ) const +bool KCalendarSystemHebrew::isValid( int year, int month, int day ) const { - // taken from setYMD below, adapted to use new methods - if ( y < year( earliestValidDate() ) || y > year( latestValidDate() ) ) { + if ( year < 5344 || year > 8119 ) { return false; } - if ( month < 1 || month > ( isLeapYear( y ) ? 13 : 12 ) ) { + + if ( month < 1 || month > d->monthsInYear( year ) ) { return false; } - if ( day < 1 || day > d->hndays( month, y ) ) { + + if ( day < 1 || day > d->daysInMonth( year, month ) ) { return false; } + return true; } bool KCalendarSystemHebrew::isValid( const QDate &date ) const { return KCalendarSystem::isValid( date ); } bool KCalendarSystemHebrew::setDate( QDate &date, int year, int month, int day ) const { - return KCalendarSystem::setDate( date, year, month, day ); + return setYMD( date, year, month, day ); } // Deprecated -bool KCalendarSystemHebrew::setYMD( QDate & date, int y, int m, int day ) const -{ - // range checks - // Removed deleted minValidYear and maxValidYear methods - // Still use minimum of 1753 for now due to QDate using Julian calendar before then - // Later change to following once new methods validated - // if ( y < year( earliestValidDate() ) || y > year( latestValidDate() ) ) - if ( y < year( QDate( 1753, 1, 1 ) ) || y > 9999 ) { - return false; - } - - if( m < 1 || m > ( is_leap_year( y ) ? 13 : 12 ) ) { - return false; - } - - if( day < 1 || day > d->hndays( m, y ) ) { +bool KCalendarSystemHebrew::setYMD( QDate &date, int year, int month, int day ) const +{ + if ( !isValid( year, month, day) ) { return false; } - class h_date * gd = hebrewToGregorian( y, m, day ); + class h_date * gd = hebrewToGregorian( year, month, day ); return date.setYMD( gd->hd_year, gd->hd_mon + 1, gd->hd_day + 1 ); } int KCalendarSystemHebrew::year( const QDate &date ) const { + if ( !isValid( date ) ) { + return -1; + } + class h_date * sd = toHebrew( date ); return sd->hd_year; } int KCalendarSystemHebrew::month( const QDate &date ) const { + if ( !isValid( date ) ) { + return -1; + } + class h_date * sd = toHebrew( date ); int month = sd->hd_mon; - if ( is_leap_year( sd->hd_year ) ) { + if ( isLeapYear( sd->hd_year ) ) { if( month == 13 /*AdarI*/ ) { month = 6; } else if( month == 14 /*AdarII*/ ) { month = 7; } else if ( month > 6 && month < 13 ) { ++month; } } return month; } int KCalendarSystemHebrew::day( const QDate &date ) const { + if ( !isValid( date ) ) { + return -1; + } + class h_date * sd = toHebrew( date ); return sd->hd_day; } QDate KCalendarSystemHebrew::addYears( const QDate &date, int nyears ) const { + if ( !isValid( date ) ) { + return QDate(); + } + QDate result = date; int y = year( date ) + nyears; setYMD( result, y, month( date ), day( date ) ); return result; } QDate KCalendarSystemHebrew::addMonths( const QDate &date, int nmonths ) const { + if ( !isValid( date ) ) { + return QDate(); + } + QDate result = date; while ( nmonths > 0 ) { result = addDays( result, daysInMonth( result ) ); --nmonths; } while ( nmonths < 0 ) { // get the number of days in the previous month to be consistent with // addMonths where nmonths > 0 int nDaysInMonth = daysInMonth( addDays( result, -day( result ) ) ); result = addDays( result, -nDaysInMonth ); ++nmonths; } return result; } QDate KCalendarSystemHebrew::addDays( const QDate &date, int ndays ) const { + if ( !isValid( date ) ) { + return QDate(); + } + return date.addDays( ndays ); } int KCalendarSystemHebrew::monthsInYear( const QDate &date ) const { - if ( is_leap_year( year( date ) ) ) { - return 13; - } else { - return 12; + if ( !isValid( date ) ) { + return -1; } + + return d->monthsInYear( year( date ) ); } int KCalendarSystemHebrew::weeksInYear( const QDate &date ) const { return KCalendarSystem::weeksInYear( date ); } -// Ok int KCalendarSystemHebrew::weeksInYear( int year ) const { - QDate temp; - setYMD( temp, year, 1, 1 ); // don't pass an uninitialized QDate to - // monthsInYear in the next call - setYMD( temp, year, monthsInYear( temp ), d->hndays( monthsInYear( temp ), year ) ); - - int nWeekNumber = weekNumber( temp ); - // last week belongs to next year - if( nWeekNumber == 1 ) { - temp = temp.addDays( -7 ); - nWeekNumber = weekNumber( temp ); - } - - return nWeekNumber; + return KCalendarSystem::weeksInYear( year ); } int KCalendarSystemHebrew::daysInYear( const QDate &date ) const { - QDate first, last; + if ( !isValid( date ) ) { + return -1; + } + + int y = year( date ); + + // Last valid year, as setYmd() won't allow calculating following year + if ( y == 8119 ) { + return 385; + } - setYMD( first, year( date ), 1, 1 ); // 1 Tishrey - setYMD( last, year( date ) + 1, 1, 1 ); // 1 Tishrey the year later + QDate first, last; + setYMD( first, y, 1, 1 ); // 1 Tishrey + setYMD( last, y + 1, 1, 1 ); // 1 Tishrey the year later return first.daysTo( last ); } int KCalendarSystemHebrew::daysInMonth( const QDate &date ) const { - return d->hndays( month( date ), year( date ) ); + if ( !isValid( date ) ) { + return -1; + } + + return d->daysInMonth( year( date ), month( date ) ); } int KCalendarSystemHebrew::daysInWeek( const QDate &date ) const { - return KCalendarSystem::daysInWeek( date ); + Q_UNUSED( date ); + + return 7; } int KCalendarSystemHebrew::dayOfYear( const QDate &date ) const { + if ( !isValid( date ) ) { + return -1; + } + QDate first; setYMD( first, year( date ), 1, 1 ); return first.daysTo( date ) + 1; } int KCalendarSystemHebrew::dayOfWeek( const QDate &date ) const { class h_date * sd = toHebrew( date ); if ( sd->hd_dw == 0 ) { return 7; } else { return ( sd->hd_dw ); } } int KCalendarSystemHebrew::weekNumber( const QDate &date, int *yearNum ) const { QDate firstDayWeek1, lastDayOfYear; int y = year( date ); int week; int weekDay1, dayOfWeek1InYear; // let's guess 1st day of 1st week setYMD( firstDayWeek1, y, 1, 1 ); weekDay1 = dayOfWeek( firstDayWeek1 ); // iso 8601: week 1 is the first containing thursday and week starts on // monday if ( weekDay1 > 4 /*Thursday*/ ) { firstDayWeek1 = addDays( firstDayWeek1 , 7 - weekDay1 + 1 ); // next monday } dayOfWeek1InYear = dayOfYear( firstDayWeek1 ); // if our date in prev year's week if ( dayOfYear( date ) < dayOfWeek1InYear ) { if ( yearNum ) * yearNum = y - 1; return weeksInYear( y - 1 ); } // let's check if its last week belongs to next year setYMD( lastDayOfYear, y + 1, 1, 1 ); lastDayOfYear = addDays( lastDayOfYear, -1 ); // if our date is in last week && 1st week in next year has thursday if ( ( dayOfYear( date ) >= daysInYear( date ) - dayOfWeek( lastDayOfYear ) + 1 ) && dayOfWeek( lastDayOfYear ) < 4 ) { if ( yearNum ) { * yearNum = y + 1; } week = 1; } else { // To calculate properly the number of weeks from day a to x let's make a day 1 of week if( weekDay1 < 5 ) { firstDayWeek1 = addDays( firstDayWeek1, -( weekDay1 - 1 ) ); } week = firstDayWeek1.daysTo( date ) / 7 + 1; } return week; } bool KCalendarSystemHebrew::isLeapYear( int year ) const { - // from is_leap_year above - return ( ( ( ( 7 * year ) + 1 ) % 19 ) < 7 ); + return d->isLeapYear( year ); } bool KCalendarSystemHebrew::isLeapYear( const QDate &date ) const { - return QDate::isLeapYear( year( date ) ); + return KCalendarSystem::isLeapYear( date ); } // ### Fixme // JPL Fix what? // Ask translators for short fomats of month names! QString KCalendarSystemHebrew::monthName( int month, int year, MonthNameFormat format ) const { - if ( month < 1 ) { + if ( month < 1 || month > d->monthsInYear( year ) ) { return QString(); } - if ( is_leap_year( year ) && month > 13 ) { - return QString(); - } else if ( month > 12 ) { - return QString(); - } - - // We must map conversion algorithm month index to real index - if( month == 6 && is_leap_year( year ) ) { - month = 13; /*Adar I*/ - } else if ( month == 7 && is_leap_year( year ) ) { - month = 14; /*Adar II*/ - } else if ( month > 7 && is_leap_year( year ) ) { - month--; //Because of Adar II - } + // We must map month number to month index + int monthIndex = d->monthNumberToMonthIndex( year, month ); if ( format == ShortNamePossessive || format == LongNamePossessive ) { - switch( month ) { + switch( monthIndex ) { case 1: return ki18n( "of Tishrey" ).toString( locale() ); case 2: return ki18n( "of Heshvan" ).toString( locale() ); case 3: return ki18n( "of Kislev" ).toString( locale() ); case 4: return ki18n( "of Tevet" ).toString( locale() ); case 5: return ki18n( "of Shvat" ).toString( locale() ); case 6: return ki18n( "of Adar" ).toString( locale() ); case 7: return ki18n( "of Nisan" ).toString( locale() ); case 8: return ki18n( "of Iyar" ).toString( locale() ); case 9: return ki18n( "of Sivan" ).toString( locale() ); case 10: return ki18n( "of Tamuz" ).toString( locale() ); case 11: return ki18n( "of Av" ).toString( locale() ); case 12: return ki18n( "of Elul" ).toString( locale() ); case 13: return ki18n( "of Adar I" ).toString( locale() ); case 14: return ki18n( "of Adar II" ).toString( locale() ); default: return QString(); } } - switch( month ) { + switch( monthIndex ) { case 1: return ki18n( "Tishrey" ).toString( locale() ); case 2: return ki18n( "Heshvan" ).toString( locale() ); case 3: return ki18n( "Kislev" ).toString( locale() ); case 4: return ki18n( "Tevet" ).toString( locale() ); case 5: return ki18n( "Shvat" ).toString( locale() ); case 6: return ki18n( "Adar" ).toString( locale() ); case 7: return ki18n( "Nisan" ).toString( locale() ); case 8: return ki18n( "Iyar" ).toString( locale() ); case 9: return ki18n( "Sivan" ).toString( locale() ); case 10: return ki18n( "Tamuz" ).toString( locale() ); case 11: return ki18n( "Av" ).toString( locale() ); case 12: return ki18n( "Elul" ).toString( locale() ); case 13: return ki18n( "Adar I" ).toString( locale() ); case 14: return ki18n( "Adar II" ).toString( locale() ); default: return QString(); } } QString KCalendarSystemHebrew::monthName( const QDate& date, MonthNameFormat format ) const { return monthName( month( date ), year( date ), format ); } QString KCalendarSystemHebrew::weekDayName( int weekDay, WeekDayNameFormat format ) const { // Use Western day names for now as that's what the old version did, // but wouldn't it be better to use the right Hebrew names like Shabbat? // Could make it switchable by adding new enums to WeekDayFormat, e.g. ShortNameWestern? if ( format == ShortDayName ) { switch ( weekDay ) { case 1: return ki18nc( "Monday", "Mon" ).toString( locale() ); case 2: return ki18nc( "Tuesday", "Tue" ).toString( locale() ); case 3: return ki18nc( "Wednesday", "Wed" ).toString( locale() ); case 4: return ki18nc( "Thursday", "Thu" ).toString( locale() ); case 5: return ki18nc( "Friday", "Fri" ).toString( locale() ); case 6: return ki18nc( "Saturday", "Sat" ).toString( locale() ); case 7: return ki18nc( "Sunday", "Sun" ).toString( locale() ); default: return QString(); } } switch ( weekDay ) { case 1: return ki18n( "Monday" ).toString( locale() ); case 2: return ki18n( "Tuesday" ).toString( locale() ); case 3: return ki18n( "Wednesday" ).toString( locale() ); case 4: return ki18n( "Thursday" ).toString( locale() ); case 5: return ki18n( "Friday" ).toString( locale() ); case 6: return ki18n( "Saturday" ).toString( locale() ); case 7: return ki18n( "Sunday" ).toString( locale() ); default: return QString(); } } QString KCalendarSystemHebrew::weekDayName( const QDate &date, WeekDayNameFormat format ) const { return weekDayName( dayOfWeek( date ), format ); } QString KCalendarSystemHebrew::yearString( const QDate &pDate, StringFormat format ) const { QString sResult; // Only use hebrew numbers if the hebrew setting is selected if ( locale()->language() == QLatin1String( "he" ) ) { if ( format == ShortFormat ) { sResult = num2heb( year( pDate ), false ); } } else { sResult = KCalendarSystem::yearString( pDate, format ); } return sResult; } QString KCalendarSystemHebrew::monthString( const QDate &pDate, StringFormat format ) const { return KCalendarSystem::monthString( pDate, format ); } QString KCalendarSystemHebrew::dayString( const QDate &pDate, StringFormat format ) const { QString sResult; // Only use hebrew numbers if the hebrew setting is selected if ( locale()->language() == QLatin1String( "he" ) ) { sResult = num2heb( day( pDate ), false ); } else { sResult = KCalendarSystem::dayString( pDate, format ); } return sResult; } int KCalendarSystemHebrew::yearStringToInteger( const QString &sNum, int &iLength ) const { int iResult; if ( locale()->language() == "he" ) { iResult = heb2num( sNum, iLength ); } else { iResult = KCalendarSystem::yearStringToInteger( sNum, iLength ); } if ( iResult < 1000 ) { iResult += 5000; // assume we're in the 6th millenium (y6k bug) } return iResult; } int KCalendarSystemHebrew::monthStringToInteger( const QString &sNum, int &iLength ) const { return KCalendarSystem::monthStringToInteger( sNum, iLength ); } int KCalendarSystemHebrew::dayStringToInteger( const QString &sNum, int &iLength ) const { int iResult; if ( locale()->language() == "he" ) { iResult = heb2num( sNum, iLength ); } else { iResult = KCalendarSystem::yearStringToInteger( sNum, iLength ); } return iResult; } QString KCalendarSystemHebrew::formatDate( const QDate &date, KLocale::DateFormat format ) const { return KCalendarSystem::formatDate( date, format ); } QDate KCalendarSystemHebrew::readDate( const QString &str, bool *ok ) const { return KCalendarSystem::readDate( str, ok ); } QDate KCalendarSystemHebrew::readDate( const QString &intstr, const QString &fmt, bool *ok ) const { return KCalendarSystem::readDate( intstr, fmt, ok ); } QDate KCalendarSystemHebrew::readDate( const QString &str, KLocale::ReadDateFlags flags, bool *ok ) const { return KCalendarSystem::readDate( str, flags, ok ); } int KCalendarSystemHebrew::weekDayOfPray() const { - return 6; // saturday + return 6; // Saturday } int KCalendarSystemHebrew::weekStartDay() const { return KCalendarSystem::weekStartDay(); } bool KCalendarSystemHebrew::isLunar() const { return false; } bool KCalendarSystemHebrew::isLunisolar() const { return true; } bool KCalendarSystemHebrew::isSolar() const { return false; } bool KCalendarSystemHebrew::isProleptic() const { return false; } bool KCalendarSystemHebrew::julianDayToDate( int jd, int &year, int &month, int &day ) const { - if ( jd >= earliestValidDate().toJulianDay() && jd <= latestValidDate().toJulianDay() ) { - // Hatcher formula I. Fix me! - year = d->yearOfJulianDay( jd ); - int jdTishri1ThisYear = d->julianDayOfTishri1( year ); - int dayInYear = jd - jdTishri1ThisYear + 1; - int K = d->characterOfYear( year ); - int n = dayInYear / 30; - if ( dayInYear > d->daysPreceedingMonth( K, n + 1 ) ) { - month = n + 1; - } else { - if ( dayInYear > d->daysPreceedingMonth( K, n + 1 ) ) { - month = n; - } else { - month = n - 1; - } - } - day = dayInYear - d->daysPreceedingMonth( K, month ); - return true; - } + year = -1; + month = -1; + day = -1; return false; } bool KCalendarSystemHebrew::dateToJulianDay( int year, int month, int day, int &jd ) const { - // From Hatcher formula J. Fix me! - if ( isValid( year, month, day ) ) { - int jdTishri1ThisYear = d->julianDayOfTishri1( year ); - jd = jdTishri1ThisYear - + d->daysPreceedingMonth( d->characterOfYear( year ), month ) - + day - 1; - return true; - } + jd = -1; return false; } diff --git a/kdecore/tests/kcalendartest.cpp b/kdecore/tests/kcalendartest.cpp index bd69aaef5f..c16df9d864 100644 --- a/kdecore/tests/kcalendartest.cpp +++ b/kdecore/tests/kcalendartest.cpp @@ -1,1529 +1,1571 @@ #include "qtest_kde.h" #include "kcalendarsystem.h" #include "kglobal.h" #include "kdebug.h" #include "kcalendartest.h" #include "kcalendartest.moc" #include "klocale.h" QTEST_KDEMAIN_CORE(KCalendarTest) void KCalendarTest::testTypes() { const KCalendarSystem *calendar; calendar = KCalendarSystem::create( QString("gregorian") ); QCOMPARE( calendar->calendarType(), QString("gregorian") ); delete calendar; calendar = KCalendarSystem::create( QString("gregorian-proleptic") ); QCOMPARE( calendar->calendarType(), QString("gregorian-proleptic") ); delete calendar; calendar = KCalendarSystem::create( QString("hebrew") ); QCOMPARE( calendar->calendarType(), QString("hebrew") ); delete calendar; calendar = KCalendarSystem::create( QString("hijri") ); QCOMPARE( calendar->calendarType(), QString("hijri") ); delete calendar; calendar = KCalendarSystem::create( QString("indian-national") ); QCOMPARE( calendar->calendarType(), QString("indian-national") ); delete calendar; calendar = KCalendarSystem::create( QString("jalali") ); QCOMPARE( calendar->calendarType(), QString("jalali") ); delete calendar; calendar = KCalendarSystem::create( QString("julian") ); QCOMPARE( calendar->calendarType(), QString("julian") ); delete calendar; calendar = KCalendarSystem::create( QString("invalid") ); QCOMPARE( calendar->calendarType(), QString("gregorian") ); delete calendar; calendar = KCalendarSystem::create( QString() ); QCOMPARE( calendar->calendarType(), QString("gregorian") ); delete calendar; QStringList lst = KCalendarSystem::calendarSystems(); QVERIFY( lst.contains("gregorian") ); QVERIFY( lst.contains("hebrew") ); QVERIFY( lst.contains("hijri") ); QVERIFY( lst.contains("indian-national") ); QVERIFY( lst.contains("jalali") ); QVERIFY( lst.contains("julian") ); QCOMPARE( KCalendarSystem::calendarLabel("gregorian"), QString("Gregorian") ); QCOMPARE( KCalendarSystem::calendarLabel("gregorian-proleptic"), QString("Gregorian (Proleptic)") ); QCOMPARE( KCalendarSystem::calendarLabel("hebrew"), QString("Hebrew") ); QCOMPARE( KCalendarSystem::calendarLabel("hijri"), QString("Hijri") ); QCOMPARE( KCalendarSystem::calendarLabel("indian-national"), QString("Indian National") ); QCOMPARE( KCalendarSystem::calendarLabel("jalali"), QString("Jalali") ); QCOMPARE( KCalendarSystem::calendarLabel("julian"), QString("Julian") ); } void KCalendarTest::testLocale() { KGlobal::locale()->setCalendar("gregorian"); const KCalendarSystem *calendar = KGlobal::locale()->calendar(); QCOMPARE( calendar->calendarType(), QString("gregorian") ); KGlobal::locale()->setCalendar("gregorian-proleptic"); calendar = KGlobal::locale()->calendar(); QCOMPARE( calendar->calendarType(), QString("gregorian-proleptic") ); KGlobal::locale()->setCalendar("hebrew"); calendar = KGlobal::locale()->calendar(); QCOMPARE( calendar->calendarType(), QString("hebrew") ); KGlobal::locale()->setCalendar("hijri"); calendar = KGlobal::locale()->calendar(); QCOMPARE( calendar->calendarType(), QString("hijri") ); KGlobal::locale()->setCalendar("indian-national"); calendar = KGlobal::locale()->calendar(); QCOMPARE( calendar->calendarType(), QString("indian-national") ); KGlobal::locale()->setCalendar("jalali"); calendar = KGlobal::locale()->calendar(); QCOMPARE( calendar->calendarType(), QString("jalali") ); KGlobal::locale()->setCalendar("julian"); calendar = KGlobal::locale()->calendar(); QCOMPARE( calendar->calendarType(), QString("julian") ); } void KCalendarTest::testFormatDate() { KLocale *locale = new KLocale( *KGlobal::locale() ); locale->setDateFormatShort( "%y-%m-%d" ); const KCalendarSystem *calendar = KCalendarSystem::create( QString( "gregorian" ), locale ); //Test default settings QDate testDate( 2005, 10, 20 ); QCOMPARE( calendar->formatDate( testDate, "%Y" ), QString( "2005" ) ); QCOMPARE( calendar->formatDate( testDate, "%C" ), QString( "20" ) ); QCOMPARE( calendar->formatDate( testDate, "%y" ), QString( "05" ) ); QCOMPARE( calendar->formatDate( testDate, "%m" ), QString( "10" ) ); QCOMPARE( calendar->formatDate( testDate, "%n" ), QString( "10" ) ); QCOMPARE( calendar->formatDate( testDate, "%d" ), QString( "20" ) ); QCOMPARE( calendar->formatDate( testDate, "%e" ), QString( "20" ) ); QCOMPARE( calendar->formatDate( testDate, "%B" ), QString( "October" ) ); QCOMPARE( calendar->formatDate( testDate, "%b" ), QString( "Oct" ) ); QCOMPARE( calendar->formatDate( testDate, "%h" ), QString( "Oct" ) ); QCOMPARE( calendar->formatDate( testDate, "%A" ), QString( "Thursday" ) ); QCOMPARE( calendar->formatDate( testDate, "%a" ), QString( "Thu" ) ); QCOMPARE( calendar->formatDate( testDate, "%j" ), QString( "293" ) ); QCOMPARE( calendar->formatDate( testDate, "%V" ), QString( "42" ) ); QCOMPARE( calendar->formatDate( testDate, "%G" ), QString( "2005" ) ); QCOMPARE( calendar->formatDate( testDate, "%g" ), QString( "05" ) ); QCOMPARE( calendar->formatDate( testDate, "%u" ), QString( "4" ) ); QCOMPARE( calendar->formatDate( testDate, "%D" ), QString( "10/20/05" ) ); QCOMPARE( calendar->formatDate( testDate, "%F" ), QString( "2005-10-20" ) ); QCOMPARE( calendar->formatDate( testDate, "%x" ), QString( "05-10-20" ) ); QCOMPARE( calendar->formatDate( testDate, "%%" ), QString( "%" ) ); QCOMPARE( calendar->formatDate( testDate, "%t" ), QString( "\t" ) ); QCOMPARE( calendar->formatDate( testDate, "-%Y-%C-%y-%m-%n-%d-%e-%B-%b-%h-%A-%a-%j-%V-%G-%g-%u:%D:%F:%x:%%-%t-" ), QString( "-2005-20-05-10-10-20-20-October-Oct-Oct-Thursday-Thu-293-42-2005-05-4:10/20/05:2005-10-20:05-10-20:%-\t-" ) ); //Test the minimum widths are respected QCOMPARE( calendar->formatDate( testDate, "%1Y" ), QString( "2005" ) ); QCOMPARE( calendar->formatDate( testDate, "%1C" ), QString( "20" ) ); QCOMPARE( calendar->formatDate( testDate, "%1y" ), QString( "05" ) ); QCOMPARE( calendar->formatDate( testDate, "%1m" ), QString( "10" ) ); QCOMPARE( calendar->formatDate( testDate, "%1n" ), QString( "10" ) ); QCOMPARE( calendar->formatDate( testDate, "%1d" ), QString( "20" ) ); QCOMPARE( calendar->formatDate( testDate, "%1e" ), QString( "20" ) ); QCOMPARE( calendar->formatDate( testDate, "%01B" ), QString( "October" ) ); QCOMPARE( calendar->formatDate( testDate, "%01b" ), QString( "Oct" ) ); QCOMPARE( calendar->formatDate( testDate, "%01h" ), QString( "Oct" ) ); QCOMPARE( calendar->formatDate( testDate, "%01A" ), QString( "Thursday" ) ); QCOMPARE( calendar->formatDate( testDate, "%01a" ), QString( "Thu" ) ); QCOMPARE( calendar->formatDate( testDate, "%1j" ), QString( "293" ) ); QCOMPARE( calendar->formatDate( testDate, "%1V" ), QString( "42" ) ); QCOMPARE( calendar->formatDate( testDate, "%1G" ), QString( "2005" ) ); QCOMPARE( calendar->formatDate( testDate, "%1g" ), QString( "05" ) ); QCOMPARE( calendar->formatDate( testDate, "%1u" ), QString( "4" ) ); QCOMPARE( calendar->formatDate( testDate, "%1D" ), QString( "10/20/05" ) ); QCOMPARE( calendar->formatDate( testDate, "%1F" ), QString( "2005-10-20" ) ); QCOMPARE( calendar->formatDate( testDate, "%1x" ), QString( "05-10-20" ) ); QCOMPARE( calendar->formatDate( testDate, "%1%" ), QString( "%" ) ); QCOMPARE( calendar->formatDate( testDate, "%1t" ), QString( "\t" ) ); testDate.setDate( 2005, 1, 2 ); //Test various padding options with minimum width QCOMPARE( calendar->formatDate( testDate, "%" ), QString( "" ) ); QCOMPARE( calendar->formatDate( testDate, "%m" ), QString( "01" ) ); QCOMPARE( calendar->formatDate( testDate, "%n" ), QString( "1" ) ); QCOMPARE( calendar->formatDate( testDate, "%d" ), QString( "02" ) ); QCOMPARE( calendar->formatDate( testDate, "%e" ), QString( "2" ) ); QCOMPARE( calendar->formatDate( testDate, "%j" ), QString( "002" ) ); QCOMPARE( calendar->formatDate( testDate, "%_m" ), QString( " 1" ) ); QCOMPARE( calendar->formatDate( testDate, "%_n" ), QString( "1" ) ); QCOMPARE( calendar->formatDate( testDate, "%_d" ), QString( " 2" ) ); QCOMPARE( calendar->formatDate( testDate, "%_e" ), QString( "2" ) ); QCOMPARE( calendar->formatDate( testDate, "%_j" ), QString( " 2" ) ); QCOMPARE( calendar->formatDate( testDate, "%-m" ), QString( "1" ) ); QCOMPARE( calendar->formatDate( testDate, "%-n" ), QString( "1" ) ); QCOMPARE( calendar->formatDate( testDate, "%-d" ), QString( "2" ) ); QCOMPARE( calendar->formatDate( testDate, "%-e" ), QString( "2" ) ); QCOMPARE( calendar->formatDate( testDate, "%-j" ), QString( "2" ) ); QCOMPARE( calendar->formatDate( testDate, "%0m" ), QString( "01" ) ); QCOMPARE( calendar->formatDate( testDate, "%0n" ), QString( "1" ) ); QCOMPARE( calendar->formatDate( testDate, "%0d" ), QString( "02" ) ); QCOMPARE( calendar->formatDate( testDate, "%0e" ), QString( "2" ) ); QCOMPARE( calendar->formatDate( testDate, "%0j" ), QString( "002" ) ); QCOMPARE( calendar->formatDate( testDate, "%-_j" ), QString( " 2" ) ); QCOMPARE( calendar->formatDate( testDate, "%_-j" ), QString( "2" ) ); QCOMPARE( calendar->formatDate( testDate, "%-_0j" ), QString( "002" ) ); //Test various padding options with width override QCOMPARE( calendar->formatDate( testDate, "%1m" ), QString( "01" ) ); QCOMPARE( calendar->formatDate( testDate, "%-05m" ), QString( "00001" ) ); QCOMPARE( calendar->formatDate( testDate, "%10m" ), QString( "0000000001" ) ); QCOMPARE( calendar->formatDate( testDate, "%15m" ), QString( "000000000000001" ) ); QCOMPARE( calendar->formatDate( testDate, "%-5m" ), QString( "1" ) ); QCOMPARE( calendar->formatDate( testDate, "%_5m" ), QString( " 1" ) ); QCOMPARE( calendar->formatDate( testDate, "%_15m" ), QString( " 1" ) ); QCOMPARE( calendar->formatDate( testDate, "%1B" ), QString( "January" ) ); QCOMPARE( calendar->formatDate( testDate, "%15B" ), QString( " January" ) ); QCOMPARE( calendar->formatDate( testDate, "%-15B" ), QString( "January" ) ); QCOMPARE( calendar->formatDate( testDate, "%_15B" ), QString( " January" ) ); QCOMPARE( calendar->formatDate( testDate, "%015B" ), QString( "00000000January" ) ); //Test week number in previous year QCOMPARE( calendar->formatDate( testDate, "%V" ), QString( "53" ) ); QCOMPARE( calendar->formatDate( testDate, "%G" ), QString( "2004" ) ); QCOMPARE( calendar->formatDate( testDate, "%g" ), QString( "04" ) ); //test case overrides QCOMPARE( calendar->formatDate( testDate, "%B" ), QString( "January" ) ); QCOMPARE( calendar->formatDate( testDate, "%^B" ), QString( "JANUARY" ) ); QCOMPARE( calendar->formatDate( testDate, "%#B" ), QString( "JANUARY" ) ); QCOMPARE( calendar->formatDate( testDate, "%m" ), QString( "01" ) ); QCOMPARE( calendar->formatDate( testDate, "%^m" ), QString( "01" ) ); QCOMPARE( calendar->formatDate( testDate, "%#m" ), QString( "01" ) ); //Test various year options testDate.setDate( 789, 1, 1 ); QCOMPARE( calendar->formatDate( testDate, "%Y" ), QString( "0789" ) ); QCOMPARE( calendar->formatDate( testDate, "%C" ), QString( "07" ) ); QCOMPARE( calendar->formatDate( testDate, "%y" ), QString( "89" ) ); QCOMPARE( calendar->formatDate( testDate, "%-Y" ), QString( "789" ) ); QCOMPARE( calendar->formatDate( testDate, "%-C" ), QString( "7" ) ); QCOMPARE( calendar->formatDate( testDate, "%-y" ), QString( "89" ) ); QCOMPARE( calendar->formatDate( testDate, "%_Y" ), QString( " 789" ) ); QCOMPARE( calendar->formatDate( testDate, "%_C" ), QString( " 7" ) ); QCOMPARE( calendar->formatDate( testDate, "%_y" ), QString( "89" ) ); QCOMPARE( calendar->formatDate( testDate, "%0Y" ), QString( "0789" ) ); QCOMPARE( calendar->formatDate( testDate, "%0C" ), QString( "07" ) ); QCOMPARE( calendar->formatDate( testDate, "%0y" ), QString( "89" ) ); testDate.setDate( 709, 1, 1 ); QCOMPARE( calendar->formatDate( testDate, "%Y" ), QString( "0709" ) ); QCOMPARE( calendar->formatDate( testDate, "%C" ), QString( "07" ) ); QCOMPARE( calendar->formatDate( testDate, "%y" ), QString( "09" ) ); QCOMPARE( calendar->formatDate( testDate, "%-Y" ), QString( "709" ) ); QCOMPARE( calendar->formatDate( testDate, "%-C" ), QString( "7" ) ); QCOMPARE( calendar->formatDate( testDate, "%-y" ), QString( "9" ) ); QCOMPARE( calendar->formatDate( testDate, "%_Y" ), QString( " 709" ) ); QCOMPARE( calendar->formatDate( testDate, "%_C" ), QString( " 7" ) ); QCOMPARE( calendar->formatDate( testDate, "%_y" ), QString( " 9" ) ); QCOMPARE( calendar->formatDate( testDate, "%0Y" ), QString( "0709" ) ); QCOMPARE( calendar->formatDate( testDate, "%0C" ), QString( "07" ) ); QCOMPARE( calendar->formatDate( testDate, "%0y" ), QString( "09" ) ); testDate.setDate( 89, 1, 1 ); QCOMPARE( calendar->formatDate( testDate, "%Y" ), QString( "0089" ) ); QCOMPARE( calendar->formatDate( testDate, "%C" ), QString( "00" ) ); QCOMPARE( calendar->formatDate( testDate, "%y" ), QString( "89" ) ); QCOMPARE( calendar->formatDate( testDate, "%-Y" ), QString( "89" ) ); QCOMPARE( calendar->formatDate( testDate, "%-C" ), QString( "0" ) ); QCOMPARE( calendar->formatDate( testDate, "%-y" ), QString( "89" ) ); QCOMPARE( calendar->formatDate( testDate, "%_Y" ), QString( " 89" ) ); QCOMPARE( calendar->formatDate( testDate, "%_C" ), QString( " 0" ) ); QCOMPARE( calendar->formatDate( testDate, "%_y" ), QString( "89" ) ); QCOMPARE( calendar->formatDate( testDate, "%0Y" ), QString( "0089" ) ); QCOMPARE( calendar->formatDate( testDate, "%0C" ), QString( "00" ) ); QCOMPARE( calendar->formatDate( testDate, "%0y" ), QString( "89" ) ); testDate.setDate( 9, 1, 1 ); QCOMPARE( calendar->formatDate( testDate, "%Y" ), QString( "0009" ) ); QCOMPARE( calendar->formatDate( testDate, "%C" ), QString( "00" ) ); QCOMPARE( calendar->formatDate( testDate, "%y" ), QString( "09" ) ); QCOMPARE( calendar->formatDate( testDate, "%-Y" ), QString( "9" ) ); QCOMPARE( calendar->formatDate( testDate, "%-C" ), QString( "0" ) ); QCOMPARE( calendar->formatDate( testDate, "%-y" ), QString( "9" ) ); QCOMPARE( calendar->formatDate( testDate, "%_Y" ), QString( " 9" ) ); QCOMPARE( calendar->formatDate( testDate, "%_C" ), QString( " 0" ) ); QCOMPARE( calendar->formatDate( testDate, "%_y" ), QString( " 9" ) ); QCOMPARE( calendar->formatDate( testDate, "%0Y" ), QString( "0009" ) ); QCOMPARE( calendar->formatDate( testDate, "%0C" ), QString( "00" ) ); QCOMPARE( calendar->formatDate( testDate, "%0y" ), QString( "09" ) ); testDate.setDate( -9, 1, 1 ); QCOMPARE( calendar->formatDate( testDate, "%Y" ), QString( "-0009" ) ); QCOMPARE( calendar->formatDate( testDate, "%C" ), QString( "-00" ) ); QCOMPARE( calendar->formatDate( testDate, "%y" ), QString( "-09" ) ); QCOMPARE( calendar->formatDate( testDate, "%-Y" ), QString( "-9" ) ); QCOMPARE( calendar->formatDate( testDate, "%-C" ), QString( "-0" ) ); QCOMPARE( calendar->formatDate( testDate, "%-y" ), QString( "-9" ) ); QCOMPARE( calendar->formatDate( testDate, "%_Y" ), QString( "- 9" ) ); QCOMPARE( calendar->formatDate( testDate, "%_C" ), QString( "- 0" ) ); QCOMPARE( calendar->formatDate( testDate, "%_y" ), QString( "- 9" ) ); QCOMPARE( calendar->formatDate( testDate, "%0Y" ), QString( "-0009" ) ); QCOMPARE( calendar->formatDate( testDate, "%0C" ), QString( "-00" ) ); QCOMPARE( calendar->formatDate( testDate, "%0y" ), QString( "-09" ) ); testDate.setDate( -89, 1, 1 ); QCOMPARE( calendar->formatDate( testDate, "%Y" ), QString( "-0089" ) ); QCOMPARE( calendar->formatDate( testDate, "%C" ), QString( "-00" ) ); QCOMPARE( calendar->formatDate( testDate, "%y" ), QString( "-89" ) ); QCOMPARE( calendar->formatDate( testDate, "%-Y" ), QString( "-89" ) ); QCOMPARE( calendar->formatDate( testDate, "%-C" ), QString( "-0" ) ); QCOMPARE( calendar->formatDate( testDate, "%-y" ), QString( "-89" ) ); QCOMPARE( calendar->formatDate( testDate, "%_Y" ), QString( "- 89" ) ); QCOMPARE( calendar->formatDate( testDate, "%_C" ), QString( "- 0" ) ); QCOMPARE( calendar->formatDate( testDate, "%_y" ), QString( "-89" ) ); QCOMPARE( calendar->formatDate( testDate, "%0Y" ), QString( "-0089" ) ); QCOMPARE( calendar->formatDate( testDate, "%0C" ), QString( "-00" ) ); QCOMPARE( calendar->formatDate( testDate, "%0y" ), QString( "-89" ) ); testDate.setDate( -789, 1, 1 ); QCOMPARE( calendar->formatDate( testDate, "%Y" ), QString( "-0789" ) ); QCOMPARE( calendar->formatDate( testDate, "%C" ), QString( "-07" ) ); QCOMPARE( calendar->formatDate( testDate, "%y" ), QString( "-89" ) ); QCOMPARE( calendar->formatDate( testDate, "%-Y" ), QString( "-789" ) ); QCOMPARE( calendar->formatDate( testDate, "%-C" ), QString( "-7" ) ); QCOMPARE( calendar->formatDate( testDate, "%-y" ), QString( "-89" ) ); QCOMPARE( calendar->formatDate( testDate, "%_Y" ), QString( "- 789" ) ); QCOMPARE( calendar->formatDate( testDate, "%_C" ), QString( "- 7" ) ); QCOMPARE( calendar->formatDate( testDate, "%_y" ), QString( "-89" ) ); QCOMPARE( calendar->formatDate( testDate, "%0Y" ), QString( "-0789" ) ); QCOMPARE( calendar->formatDate( testDate, "%0C" ), QString( "-07" ) ); QCOMPARE( calendar->formatDate( testDate, "%0y" ), QString( "-89" ) ); testDate.setDate( -709, 1, 1 ); QCOMPARE( calendar->formatDate( testDate, "%Y" ), QString( "-0709" ) ); QCOMPARE( calendar->formatDate( testDate, "%C" ), QString( "-07" ) ); QCOMPARE( calendar->formatDate( testDate, "%y" ), QString( "-09" ) ); QCOMPARE( calendar->formatDate( testDate, "%-Y" ), QString( "-709" ) ); QCOMPARE( calendar->formatDate( testDate, "%-C" ), QString( "-7" ) ); QCOMPARE( calendar->formatDate( testDate, "%-y" ), QString( "-9" ) ); QCOMPARE( calendar->formatDate( testDate, "%_Y" ), QString( "- 709" ) ); QCOMPARE( calendar->formatDate( testDate, "%_C" ), QString( "- 7" ) ); QCOMPARE( calendar->formatDate( testDate, "%_y" ), QString( "- 9" ) ); QCOMPARE( calendar->formatDate( testDate, "%0Y" ), QString( "-0709" ) ); QCOMPARE( calendar->formatDate( testDate, "%0C" ), QString( "-07" ) ); QCOMPARE( calendar->formatDate( testDate, "%0y" ), QString( "-09" ) ); testDate.setDate( -1789, 1, 1 ); QCOMPARE( calendar->formatDate( testDate, "%Y" ), QString( "-1789" ) ); QCOMPARE( calendar->formatDate( testDate, "%C" ), QString( "-17" ) ); QCOMPARE( calendar->formatDate( testDate, "%y" ), QString( "-89" ) ); QCOMPARE( calendar->formatDate( testDate, "%-Y" ), QString( "-1789" ) ); QCOMPARE( calendar->formatDate( testDate, "%-C" ), QString( "-17" ) ); QCOMPARE( calendar->formatDate( testDate, "%-y" ), QString( "-89" ) ); QCOMPARE( calendar->formatDate( testDate, "%_Y" ), QString( "-1789" ) ); QCOMPARE( calendar->formatDate( testDate, "%_C" ), QString( "-17" ) ); QCOMPARE( calendar->formatDate( testDate, "%_y" ), QString( "-89" ) ); QCOMPARE( calendar->formatDate( testDate, "%0Y" ), QString( "-1789" ) ); QCOMPARE( calendar->formatDate( testDate, "%0C" ), QString( "-17" ) ); QCOMPARE( calendar->formatDate( testDate, "%0y" ), QString( "-89" ) ); testDate.setDate( -1709, 1, 1 ); QCOMPARE( calendar->formatDate( testDate, "%Y" ), QString( "-1709" ) ); QCOMPARE( calendar->formatDate( testDate, "%C" ), QString( "-17" ) ); QCOMPARE( calendar->formatDate( testDate, "%y" ), QString( "-09" ) ); QCOMPARE( calendar->formatDate( testDate, "%-Y" ), QString( "-1709" ) ); QCOMPARE( calendar->formatDate( testDate, "%-C" ), QString( "-17" ) ); QCOMPARE( calendar->formatDate( testDate, "%-y" ), QString( "-9" ) ); QCOMPARE( calendar->formatDate( testDate, "%_Y" ), QString( "-1709" ) ); QCOMPARE( calendar->formatDate( testDate, "%_C" ), QString( "-17" ) ); QCOMPARE( calendar->formatDate( testDate, "%_y" ), QString( "- 9" ) ); QCOMPARE( calendar->formatDate( testDate, "%0Y" ), QString( "-1709" ) ); QCOMPARE( calendar->formatDate( testDate, "%0C" ), QString( "-17" ) ); QCOMPARE( calendar->formatDate( testDate, "%0y" ), QString( "-09" ) ); } void KCalendarTest::testReadDate() { KLocale *locale = new KLocale( *KGlobal::locale() ); locale->setDateFormatShort( "%y-%m-%d" ); const KCalendarSystem *calendar = KCalendarSystem::create( QString( "gregorian" ), locale ); int currentYear = QDate::currentDate().year(); //Test default settings QDate testDate( 2005, 10, 20 ); //Test Month and Day QCOMPARE( calendar->readDate( "2005-01-01", "%Y-%m-%d" ), QDate( 2005, 1, 1 ) ); QCOMPARE( calendar->readDate( "05-01-01", "%y-%m-%d" ), QDate( 2005, 1, 1 ) ); QCOMPARE( calendar->readDate( "01-01", "%m-%d" ), QDate( currentYear, 1, 1 ) ); //Test Day-of-Year QCOMPARE( calendar->readDate( "2005-001", "%Y-%j" ), QDate( 2005, 1, 1 ) ); QCOMPARE( calendar->readDate( "2005-1", "%Y-%j" ), QDate( 2005, 1, 1 ) ); QCOMPARE( calendar->readDate( "001", "%j" ), QDate( currentYear, 1, 1 ) ); QCOMPARE( calendar->readDate( "2005-365", "%Y-%j" ), QDate( 2005, 12, 31 ) ); QCOMPARE( calendar->readDate( "2000-366", "%Y-%j" ), QDate( 2000, 12, 31 ) ); QCOMPARE( calendar->readDate( "2005-000", "%Y-%j" ), QDate() ); QCOMPARE( calendar->readDate( "2005-366", "%Y-%j" ), QDate() ); QCOMPARE( calendar->readDate( "2000-367", "%Y-%j" ), QDate() ); //Test Weekday and Month names QCOMPARE( calendar->readDate( "Monday 2004-01-05", "%A %Y-%m-%d" ), QDate( 2004, 1, 5 ) ); QCOMPARE( calendar->readDate( "Mon 2004-01-05", "%A %Y-%m-%d" ), QDate( 2004, 1, 5 ) ); QCOMPARE( calendar->readDate( "Monday 2004-01-05", "%a %Y-%m-%d" ), QDate( 2004, 1, 5 ) ); QCOMPARE( calendar->readDate( "Mon 2004-01-05", "%a %Y-%m-%d" ), QDate( 2004, 1, 5 ) ); QCOMPARE( calendar->readDate( "Sunday 2004-01-11", "%A %Y-%m-%d" ), QDate( 2004, 1, 11 ) ); QCOMPARE( calendar->readDate( "Sun 2004-01-11", "%A %Y-%m-%d" ), QDate( 2004, 1, 11 ) ); QCOMPARE( calendar->readDate( "Sunday 2004-01-11", "%a %Y-%m-%d" ), QDate( 2004, 1, 11 ) ); QCOMPARE( calendar->readDate( "Sun 2004-01-11", "%a %Y-%m-%d" ), QDate( 2004, 1, 11 ) ); QCOMPARE( calendar->readDate( "2004 January 01", "%Y %B %d" ), QDate( 2004, 1, 1 ) ); QCOMPARE( calendar->readDate( "2004 Jan 01", "%Y %B %d" ), QDate( 2004, 1, 1 ) ); QCOMPARE( calendar->readDate( "2004 January 01", "%Y %b %d" ), QDate( 2004, 1, 1 ) ); QCOMPARE( calendar->readDate( "2004 Jan 01", "%Y %b %d" ), QDate( 2004, 1, 1 ) ); QCOMPARE( calendar->readDate( "2004 December 01", "%Y %B %d" ), QDate( 2004, 12, 1 ) ); QCOMPARE( calendar->readDate( "2004 Dec 01", "%Y %B %d" ), QDate( 2004, 12, 1 ) ); QCOMPARE( calendar->readDate( "2004 December 01", "%Y %b %d" ), QDate( 2004, 12, 1 ) ); QCOMPARE( calendar->readDate( "2004 Dec 01", "%Y %b %d" ), QDate( 2004, 12, 1 ) ); QCOMPARE( calendar->readDate( "Wednesday 2004 December 01", "%A %Y %B %d" ), QDate( 2004, 12, 1 ) ); QCOMPARE( calendar->readDate( "Wed 2004 Dec 01", "%A %Y %B %d" ), QDate( 2004, 12, 1 ) ); QCOMPARE( calendar->readDate( "Wednesday 2004 December 01", "%a %Y %b %d" ), QDate( 2004, 12, 1 ) ); QCOMPARE( calendar->readDate( "Wed 2004 Dec 01", "%a %Y %b %d" ), QDate( 2004, 12, 1 ) ); //Test ISO Week and Day-of-Week QCOMPARE( calendar->readDate( "2004-W01-01", "%Y-W%V-%u" ), QDate( 2003, 12, 29 ) ); QCOMPARE( calendar->readDate( "2004-W01-02", "%Y-W%V-%u" ), QDate( 2003, 12, 30 ) ); QCOMPARE( calendar->readDate( "2004-W01-03", "%Y-W%V-%u" ), QDate( 2003, 12, 31 ) ); QCOMPARE( calendar->readDate( "2004-W01-04", "%Y-W%V-%u" ), QDate( 2004, 1, 1 ) ); QCOMPARE( calendar->readDate( "2004-W01-05", "%Y-W%V-%u" ), QDate( 2004, 1, 2 ) ); QCOMPARE( calendar->readDate( "2004-W01-06", "%Y-W%V-%u" ), QDate( 2004, 1, 3 ) ); QCOMPARE( calendar->readDate( "2004-W01-07", "%Y-W%V-%u" ), QDate( 2004, 1, 4 ) ); QCOMPARE( calendar->readDate( "2004-W24-01", "%Y-W%V-%u" ), QDate( 2004, 6, 7 ) ); QCOMPARE( calendar->readDate( "2004-W24-02", "%Y-W%V-%u" ), QDate( 2004, 6, 8 ) ); QCOMPARE( calendar->readDate( "2004-W24-03", "%Y-W%V-%u" ), QDate( 2004, 6, 9 ) ); QCOMPARE( calendar->readDate( "2004-W24-04", "%Y-W%V-%u" ), QDate( 2004, 6, 10 ) ); QCOMPARE( calendar->readDate( "2004-W24-05", "%Y-W%V-%u" ), QDate( 2004, 6, 11 ) ); QCOMPARE( calendar->readDate( "2004-W24-06", "%Y-W%V-%u" ), QDate( 2004, 6, 12 ) ); QCOMPARE( calendar->readDate( "2004-W24-07", "%Y-W%V-%u" ), QDate( 2004, 6, 13 ) ); QCOMPARE( calendar->readDate( "2004-W53-01", "%Y-W%V-%u" ), QDate( 2004, 12, 27 ) ); QCOMPARE( calendar->readDate( "2004-W53-02", "%Y-W%V-%u" ), QDate( 2004, 12, 28 ) ); QCOMPARE( calendar->readDate( "2004-W53-03", "%Y-W%V-%u" ), QDate( 2004, 12, 29 ) ); QCOMPARE( calendar->readDate( "2004-W53-04", "%Y-W%V-%u" ), QDate( 2004, 12, 30 ) ); QCOMPARE( calendar->readDate( "2004-W53-05", "%Y-W%V-%u" ), QDate( 2004, 12, 31 ) ); QCOMPARE( calendar->readDate( "2004-W53-06", "%Y-W%V-%u" ), QDate( 2005, 1, 1 ) ); QCOMPARE( calendar->readDate( "2004-W53-07", "%Y-W%V-%u" ), QDate( 2005, 1, 2 ) ); //Need to fix each year! - QCOMPARE( calendar->readDate( "W46-05", "W%V-%u" ), QDate( 2009, 11, 13 ) ); + QCOMPARE( calendar->readDate( "W46-05", "W%V-%u" ), QDate( 2010, 11, 19 ) ); QCOMPARE( calendar->readDate( "2004-W00-01", "%Y-W%V-%u" ), QDate() ); QCOMPARE( calendar->readDate( "2004-W01-00", "%Y-W%V-%u" ), QDate() ); QCOMPARE( calendar->readDate( "2004-W01-08", "%Y-W%V-%u" ), QDate() ); QCOMPARE( calendar->readDate( "2004-W54-01", "%Y-W%V-%u" ), QDate() ); //Test all 3 options, should go with most accurate QCOMPARE( calendar->readDate( "2004-01-01-365-W52-01", "%Y-%m-%d-%j-W%V-%u" ), QDate( 2004, 1, 1 ) ); QCOMPARE( calendar->readDate( "2004-001-W52-01", "%Y-%j-W%V-%u" ), QDate( 2004, 1, 1 ) ); //Test spaces and literals QCOMPARE( calendar->readDate( "2004- 1-1", "%Y-%m-%d" ), QDate( 2004, 1, 1 ) ); QCOMPARE( calendar->readDate( "2004-1-1", "%Y-%m-%e" ), QDate( 2004, 1, 1 ) ); QCOMPARE( calendar->readDate( "2004 01 01", "%Y %m %d" ), QDate( 2004, 1, 1 ) ); QCOMPARE( calendar->readDate( "2004 01 01", "%Y %m %d" ), QDate( 2004, 1, 1 ) ); QCOMPARE( calendar->readDate( "2004W01-01", "%YW%m-%d" ), QDate( 2004, 1, 1 ) ); QCOMPARE( calendar->readDate( "2004w01-01", "%YW%m-%d" ), QDate( 2004, 1, 1 ) ); QCOMPARE( calendar->readDate( "2004W01-01", "%Yw%m-%d" ), QDate( 2004, 1, 1 ) ); //Test enums and non-strict version of readDate() locale->setDateFormat( "%d/%m/%Y" ); locale->setDateFormatShort( "%Y/%m/%d" ); QCOMPARE( calendar->readDate( "01/02/2004" ), QDate( 2004, 2, 1 ) ); //KLocale::NormalFormat QCOMPARE( calendar->readDate( "2004/02/01" ), QDate( 2004, 2, 1 ) ); //KLocale::ShortFormat QCOMPARE( calendar->readDate( "2004-02-01" ), QDate( 2004, 2, 1 ) ); //KLocale::IsoFormat QCOMPARE( calendar->readDate( "2004-W02-01" ), QDate( 2004, 1, 5 ) ); //KLocale::IsoWeekFormat QCOMPARE( calendar->readDate( "2004-001" ), QDate( 2004, 1, 1 ) ); //KLocale::IsoOrdinalFormat //Test enums and strict version of readDate() QCOMPARE( calendar->readDate( "01/02/2004", KLocale::NormalFormat ), QDate( 2004, 2, 1 ) ); QCOMPARE( calendar->readDate( "2004/02/01", KLocale::ShortFormat ), QDate( 2004, 2, 1 ) ); QCOMPARE( calendar->readDate( "2004-02-01", KLocale::IsoFormat ), QDate( 2004, 2, 1 ) ); QCOMPARE( calendar->readDate( "2004-W02-01", KLocale::IsoWeekFormat ), QDate( 2004, 1, 5 ) ); QCOMPARE( calendar->readDate( "2004-001", KLocale::IsoOrdinalFormat ), QDate( 2004, 1, 1 ) ); } void KCalendarTest::testStringForms() { testStrings( KLocale::ArabicDigits ); testStrings( KLocale::ArabicIndicDigits ); } void KCalendarTest::testStrings( KLocale::DigitSet testDigitSet ) { KLocale *locale = new KLocale( *KGlobal::locale() ); locale->setDigitSet( testDigitSet ); const KCalendarSystem *calendar = KCalendarSystem::create( QString( "gregorian" ), locale ); //Test default settings QDate testDate( 2005, 1, 3 ); //Test Year String QCOMPARE( calendar->yearString( testDate ), locale->convertDigits( "2005", locale->dateTimeDigitSet() ) ); QCOMPARE( calendar->yearString( testDate, KCalendarSystem::LongFormat ), locale->convertDigits( "2005", locale->dateTimeDigitSet() ) ); QCOMPARE( calendar->yearString( testDate, KCalendarSystem::ShortFormat ), locale->convertDigits( "05", locale->dateTimeDigitSet() ) ); //Test Month String QCOMPARE( calendar->monthString( testDate ), locale->convertDigits( "01", locale->dateTimeDigitSet() ) ); QCOMPARE( calendar->monthString( testDate, KCalendarSystem::LongFormat ), locale->convertDigits( "01", locale->dateTimeDigitSet() ) ); QCOMPARE( calendar->monthString( testDate, KCalendarSystem::ShortFormat ), locale->convertDigits( "1", locale->dateTimeDigitSet() ) ); //Test Day String QCOMPARE( calendar->dayString( testDate ), locale->convertDigits( "03", locale->dateTimeDigitSet() ) ); QCOMPARE( calendar->dayString( testDate, KCalendarSystem::LongFormat ), locale->convertDigits( "03", locale->dateTimeDigitSet() ) ); QCOMPARE( calendar->dayString( testDate, KCalendarSystem::ShortFormat ), locale->convertDigits( "3", locale->dateTimeDigitSet() ) ); //Test Day of Year String QCOMPARE( calendar->dayOfYearString( testDate ), locale->convertDigits( "003", locale->dateTimeDigitSet() ) ); QCOMPARE( calendar->dayOfYearString( testDate, KCalendarSystem::LongFormat ), locale->convertDigits( "003", locale->dateTimeDigitSet() ) ); QCOMPARE( calendar->dayOfYearString( testDate, KCalendarSystem::ShortFormat ), locale->convertDigits( "3", locale->dateTimeDigitSet() ) ); //Test Day of Week String QCOMPARE( calendar->dayOfWeekString( testDate ), locale->convertDigits( "1", locale->dateTimeDigitSet() ) ); //Test Week Number String QCOMPARE( calendar->weekNumberString( testDate ), locale->convertDigits( "01", locale->dateTimeDigitSet() ) ); QCOMPARE( calendar->weekNumberString( testDate, KCalendarSystem::LongFormat ), locale->convertDigits( "01", locale->dateTimeDigitSet() ) ); QCOMPARE( calendar->weekNumberString( testDate, KCalendarSystem::ShortFormat ), locale->convertDigits( "1", locale->dateTimeDigitSet() ) ); //Test Months In Year String QCOMPARE( calendar->monthsInYearString( testDate ), locale->convertDigits( "12", locale->dateTimeDigitSet() ) ); QCOMPARE( calendar->monthsInYearString( testDate, KCalendarSystem::LongFormat ), locale->convertDigits( "12", locale->dateTimeDigitSet() ) ); QCOMPARE( calendar->monthsInYearString( testDate, KCalendarSystem::ShortFormat ), locale->convertDigits( "12", locale->dateTimeDigitSet() ) ); //Test Weeks In Year String QCOMPARE( calendar->weeksInYearString( testDate ), locale->convertDigits( "52", locale->dateTimeDigitSet() ) ); QCOMPARE( calendar->weeksInYearString( testDate, KCalendarSystem::LongFormat ), locale->convertDigits( "52", locale->dateTimeDigitSet() ) ); QCOMPARE( calendar->weeksInYearString( testDate, KCalendarSystem::ShortFormat ), locale->convertDigits( "52", locale->dateTimeDigitSet() ) ); //Test Days In Year String QCOMPARE( calendar->daysInYearString( testDate ), locale->convertDigits( "365", locale->dateTimeDigitSet() ) ); QCOMPARE( calendar->daysInYearString( testDate, KCalendarSystem::LongFormat ), locale->convertDigits( "365", locale->dateTimeDigitSet() ) ); QCOMPARE( calendar->daysInYearString( testDate, KCalendarSystem::ShortFormat ), locale->convertDigits( "365", locale->dateTimeDigitSet() ) ); //Test Days In Month String QCOMPARE( calendar->daysInMonthString( testDate ), locale->convertDigits( "31", locale->dateTimeDigitSet() ) ); QCOMPARE( calendar->daysInMonthString( testDate, KCalendarSystem::LongFormat ), locale->convertDigits( "31", locale->dateTimeDigitSet() ) ); QCOMPARE( calendar->daysInMonthString( testDate, KCalendarSystem::ShortFormat ), locale->convertDigits( "31", locale->dateTimeDigitSet() ) ); //Test Days In Week String QCOMPARE( calendar->daysInWeekString( testDate ), locale->convertDigits( "7", locale->dateTimeDigitSet() ) ); } void KCalendarTest::testIsoWeekDate() { KLocale *locale = new KLocale( *KGlobal::locale() ); const KCalendarSystem *calendar = KCalendarSystem::create( QString( "gregorian" ), locale ); int earliestValidYear = calendar->year( calendar->earliestValidDate() ); int latestValidYear = calendar->year( calendar->latestValidDate() ); QCOMPARE( calendar->isValidIsoWeekDate( earliestValidYear - 1, 1, 1 ), false ); QCOMPARE( calendar->isValidIsoWeekDate( earliestValidYear, 1, 1 ), false ); //In prev year QCOMPARE( calendar->isValidIsoWeekDate( earliestValidYear, 1, 2 ), false ); //In prev year QCOMPARE( calendar->isValidIsoWeekDate( earliestValidYear, 1, 3 ), true ); QCOMPARE( calendar->isValidIsoWeekDate( -1, 1, 1 ), true ); QCOMPARE( calendar->isValidIsoWeekDate( 0, 1, 1 ), false ); QCOMPARE( calendar->isValidIsoWeekDate( 1, 1, 1 ), true ); QCOMPARE( calendar->isValidIsoWeekDate( latestValidYear, 52, 5 ), true ); QCOMPARE( calendar->isValidIsoWeekDate( latestValidYear, 52, 6 ), false ); //In following year QCOMPARE( calendar->isValidIsoWeekDate( latestValidYear, 52, 7 ), false ); //In following year QCOMPARE( calendar->isValidIsoWeekDate( latestValidYear + 1, 1, 1 ), false ); QCOMPARE( calendar->isValidIsoWeekDate( 2004, 0, 1 ), false ); QCOMPARE( calendar->isValidIsoWeekDate( 2004, 1, 1 ), true ); QCOMPARE( calendar->isValidIsoWeekDate( 2004, 53, 1 ), true ); QCOMPARE( calendar->isValidIsoWeekDate( 2000, 54, 1 ), false ); QCOMPARE( calendar->isValidIsoWeekDate( 2004, 1, 0 ), false ); QCOMPARE( calendar->isValidIsoWeekDate( 2004, 1, 1 ), true ); QCOMPARE( calendar->isValidIsoWeekDate( 2004, 1, 7 ), true ); QCOMPARE( calendar->isValidIsoWeekDate( 2004, 1, 8 ), false ); QCOMPARE( setIsoWeekDate( calendar, earliestValidYear - 1, 1, 1 ), QDate() ); QCOMPARE( setIsoWeekDate( calendar, earliestValidYear, 1, 1 ), QDate() ); //In prev year QCOMPARE( setIsoWeekDate( calendar, earliestValidYear, 1, 2 ), QDate() ); //In prev year QCOMPARE( setIsoWeekDate( calendar, earliestValidYear, 1, 3 ), QDate( -4712, 1, 1 ) ); QCOMPARE( setIsoWeekDate( calendar, -1, 1, 1 ), QDate( -2, 12, 29 ) ); QCOMPARE( setIsoWeekDate( calendar, 0, 1, 1 ), QDate() ); QCOMPARE( setIsoWeekDate( calendar, 1, 1, 1 ), QDate( 1, 1, 3 ) ); QCOMPARE( setIsoWeekDate( calendar, latestValidYear, 52, 5 ), QDate( 9999, 12, 31 ) ); QCOMPARE( setIsoWeekDate( calendar, latestValidYear, 52, 6 ), QDate() ); //In next year QCOMPARE( setIsoWeekDate( calendar, latestValidYear, 52, 7 ), QDate() ); //In next year QCOMPARE( setIsoWeekDate( calendar, latestValidYear + 1, 1, 1 ), QDate() ); QCOMPARE( setIsoWeekDate( calendar, 2004, 0, 1 ), QDate() ); QCOMPARE( setIsoWeekDate( calendar, 2004, 1, 1 ), QDate( 2003, 12, 29 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 53, 1 ), QDate( 2004, 12, 27 ) ); QCOMPARE( setIsoWeekDate( calendar, 2000, 54, 1 ), QDate() ); QCOMPARE( setIsoWeekDate( calendar, 2004, 1, 0 ), QDate() ); QCOMPARE( setIsoWeekDate( calendar, 2004, 1, 1 ), QDate( 2003, 12, 29 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 1, 7 ), QDate( 2004, 1, 4 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 1, 8 ), QDate() ); QCOMPARE( setIsoWeekDate( calendar, 2004, 1, 1 ), QDate( 2003, 12, 29 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 1, 2 ), QDate( 2003, 12, 30 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 1, 3 ), QDate( 2003, 12, 31 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 1, 4 ), QDate( 2004, 1, 1 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 1, 5 ), QDate( 2004, 1, 2 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 1, 6 ), QDate( 2004, 1, 3 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 1, 7 ), QDate( 2004, 1, 4 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 24, 1 ), QDate( 2004, 6, 7 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 24, 2 ), QDate( 2004, 6, 8 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 24, 3 ), QDate( 2004, 6, 9 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 24, 4 ), QDate( 2004, 6, 10 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 24, 5 ), QDate( 2004, 6, 11 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 24, 6 ), QDate( 2004, 6, 12 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 24, 7 ), QDate( 2004, 6, 13 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 53, 1 ), QDate( 2004, 12, 27 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 53, 2 ), QDate( 2004, 12, 28 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 53, 3 ), QDate( 2004, 12, 29 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 53, 4 ), QDate( 2004, 12, 30 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 53, 5 ), QDate( 2004, 12, 31 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 53, 6 ), QDate( 2005, 1, 1 ) ); QCOMPARE( setIsoWeekDate( calendar, 2004, 53, 7 ), QDate( 2005, 1, 2 ) ); } QDate KCalendarTest::setIsoWeekDate( const KCalendarSystem * calendar, int year, int isoWeek, int dayOfWeek ) { QDate resultDate; calendar->setDateIsoWeek( resultDate, year, isoWeek, dayOfWeek ); return resultDate; } void KCalendarTest::testDayOfYearDate() { KLocale *locale = new KLocale( *KGlobal::locale() ); const KCalendarSystem *calendar = KCalendarSystem::create( QString( "gregorian" ), locale ); int earliestValidYear = calendar->year( calendar->earliestValidDate() ); int latestValidYear = calendar->year( calendar->latestValidDate() ); QCOMPARE( calendar->isValid( earliestValidYear - 1, 1 ), false ); QCOMPARE( calendar->isValid( earliestValidYear, 1 ), true ); QCOMPARE( calendar->isValid( -1, 1 ), true ); QCOMPARE( calendar->isValid( 0, 1 ), false ); QCOMPARE( calendar->isValid( 1, 1 ), true ); QCOMPARE( calendar->isValid( latestValidYear, 1 ), true ); QCOMPARE( calendar->isValid( latestValidYear + 1, 1 ), false ); QCOMPARE( calendar->isValid( 2005, 0 ), false ); QCOMPARE( calendar->isValid( 2005, 1 ), true ); QCOMPARE( calendar->isValid( 2005, 365 ), true ); QCOMPARE( calendar->isValid( 2005, 366 ), false ); QCOMPARE( calendar->isValid( 2000, 366 ), true ); QCOMPARE( calendar->isValid( 2000, 367 ), false ); QCOMPARE( setDayOfYearDate( calendar, earliestValidYear - 1, 1 ), QDate() ); QCOMPARE( setDayOfYearDate( calendar, earliestValidYear, 1 ), QDate( earliestValidYear, 1, 1 ) ); QCOMPARE( setDayOfYearDate( calendar, -1, 1 ), QDate( -1, 1, 1 ) ); QCOMPARE( setDayOfYearDate( calendar, 0, 1 ), QDate() ); QCOMPARE( setDayOfYearDate( calendar, 1, 1 ), QDate( 1, 1, 1 ) ); QCOMPARE( setDayOfYearDate( calendar, latestValidYear, 1 ), QDate( latestValidYear, 1, 1 ) ); QCOMPARE( setDayOfYearDate( calendar, latestValidYear + 1, 1 ), QDate() ); QCOMPARE( setDayOfYearDate( calendar, 2005, 0 ), QDate() ); QCOMPARE( setDayOfYearDate( calendar, 2005, 1 ), QDate( 2005, 1, 1 ) ); QCOMPARE( setDayOfYearDate( calendar, 2005, 365 ), QDate( 2005, 12, 31 ) ); QCOMPARE( setDayOfYearDate( calendar, 2005, 366 ), QDate() ); QCOMPARE( setDayOfYearDate( calendar, 2000, 366 ), QDate( 2000, 12, 31 ) ); QCOMPARE( setDayOfYearDate( calendar, 2000, 367 ), QDate() ); } QDate KCalendarTest::setDayOfYearDate( const KCalendarSystem * calendar, int year, int dayOfYear ) { QDate resultDate; calendar->setDate( resultDate, year, dayOfYear ); return resultDate; } void KCalendarTest::testGregorian() { const KCalendarSystem *calendar = KCalendarSystem::create(QString( "gregorian" )); QDate testDate( 2005, 9, 10 ); QCOMPARE( calendar->dayOfYear( testDate ), 253 ); QVERIFY( calendar->setYMD( testDate, 2000, 3, 1 ) ); QCOMPARE( calendar->year( testDate ), 2000 ); QCOMPARE( calendar->month( testDate ), 3 ); QCOMPARE( calendar->day( testDate ), 1 ); QCOMPARE( calendar->daysInYear( testDate ), 366 ); QDate newDate = calendar->addYears( testDate, 4); QCOMPARE( newDate.year(), 2004 ); QCOMPARE( calendar->daysInYear( newDate ), 366 ); newDate = calendar->addMonths( testDate, -4 ); QCOMPARE( newDate.year(), 1999 ); QCOMPARE( newDate.month(), 11 ); QCOMPARE( newDate.day(), 1 ); newDate = calendar->addDays( newDate, 20 ); QCOMPARE( newDate.year(), 1999 ); QCOMPARE( newDate.month(), 11 ); QCOMPARE( newDate.day(), 21 ); } void KCalendarTest::testHebrew() { -/* const KCalendarSystem *calendar = KCalendarSystem::create(QString( "hebrew" )); - QDate testDate( 2005, 9, 10 ); - QCOMPARE( calendar->dayOfYear( testDate ), 253 ); + QDate testDate( 2005, 9, 10 ); // 5756-13-06 + QCOMPARE( calendar->dayOfYear( testDate ), 360 ); QVERIFY( calendar->setYMD( testDate, 5760, 12, 24 ) ); QCOMPARE( calendar->year( testDate ), 5760 ); QCOMPARE( calendar->month( testDate ), 12 ); QCOMPARE( calendar->day( testDate ), 24 ); - QCOMPARE( calendar->daysInYear( testDate ), 999 ); + QCOMPARE( calendar->daysInYear( testDate ), 385 ); - QDate newDate = calendar->addYears( testDate, 4); - QCOMPARE( newDate.year(), 5760 ); - QCOMPARE( calendar->daysInYear( newDate ), 999 ); + testDate = calendar->addYears( testDate, 4); + QCOMPARE( calendar->year( testDate ), 5764 ); + QCOMPARE( calendar->month( testDate ), 12 ); + QCOMPARE( calendar->day( testDate ), 24 ); + QCOMPARE( calendar->daysInYear( testDate ), 355 ); - newDate = calendar->addMonths( testDate, -4 ); - QCOMPARE( newDate.year(), 5760 ); - QCOMPARE( newDate.month(), 8 ); - QCOMPARE( newDate.day(), 24 ); + testDate = calendar->addMonths( testDate, -4 ); + QCOMPARE( calendar->year( testDate ), 5764 ); + QCOMPARE( calendar->month( testDate ), 8 ); + QCOMPARE( calendar->day( testDate ), 24 ); + QCOMPARE( calendar->daysInYear( testDate ), 355 ); - newDate = calendar->addDays( newDate, 20 ); - QCOMPARE( newDate.year(), 5760 ); - QCOMPARE( newDate.month(), 9 ); - QCOMPARE( newDate.day(), 11 ); -*/ + testDate = calendar->addDays( testDate, 20 ); + QCOMPARE( calendar->year( testDate ), 5764 ); + QCOMPARE( calendar->month( testDate ), 9 ); + QCOMPARE( calendar->day( testDate ), 15 ); + QCOMPARE( calendar->daysInYear( testDate ), 355 ); } void KCalendarTest::testHijri() { const KCalendarSystem *calendar = KCalendarSystem::create(QString( "hijri" )); QDate testDate( 2008, 12, 29 ); //1430-01-01, jd 2454830, not leap QCOMPARE( calendar->year(testDate), 1430 ); QCOMPARE( calendar->month(testDate), 1 ); QCOMPARE( calendar->day(testDate), 1 ); QCOMPARE( calendar->isLeapYear( testDate ), false ); QCOMPARE( calendar->daysInYear(testDate), 354 ); QCOMPARE( calendar->daysInMonth(testDate), 30 ); QCOMPARE( calendar->dayOfYear(testDate), 1 ); testDate.setDate( 2009, 12, 17 ); //1430-12-29, jd 2455183, not leap QCOMPARE( calendar->year(testDate), 1430 ); QCOMPARE( calendar->month(testDate), 12 ); QCOMPARE( calendar->day(testDate), 29 ); QCOMPARE( calendar->isLeapYear( testDate ), false ); QCOMPARE( calendar->daysInYear(testDate), 354 ); QCOMPARE( calendar->daysInMonth(testDate), 29 ); QCOMPARE( calendar->dayOfYear(testDate), 354 ); testDate.setDate( 2009, 12, 18 ); //1431-01-01, jd 2455184, leap QCOMPARE( calendar->year(testDate), 1431 ); QCOMPARE( calendar->month(testDate), 1 ); QCOMPARE( calendar->day(testDate), 1 ); QCOMPARE( calendar->isLeapYear( testDate ), true ); QCOMPARE( calendar->daysInYear(testDate), 355 ); QCOMPARE( calendar->daysInMonth(testDate), 30 ); QCOMPARE( calendar->dayOfYear(testDate), 1 ); testDate.setDate( 2010, 12, 7 ); //1431-12-30, jd 2455538, leap QCOMPARE( calendar->year(testDate), 1431 ); QCOMPARE( calendar->month(testDate), 12 ); QCOMPARE( calendar->day(testDate), 30 ); QCOMPARE( calendar->isLeapYear( testDate ), true ); QCOMPARE( calendar->daysInYear(testDate), 355 ); QCOMPARE( calendar->daysInMonth(testDate), 30 ); QCOMPARE( calendar->dayOfYear(testDate), 355 ); testDate.setDate( 2005, 9, 10 ); //1426-08-06 QCOMPARE( calendar->year(testDate), 1426 ); QCOMPARE( calendar->month(testDate), 8 ); QCOMPARE( calendar->day(testDate), 6 ); QCOMPARE( calendar->daysInYear(testDate), 355 ); QCOMPARE( calendar->daysInMonth(testDate), 29 ); QCOMPARE( calendar->weeksInYear(testDate.year()), 50 ); QCOMPARE( calendar->weekNumber(testDate), 31 ); QCOMPARE( calendar->dayOfYear(testDate), 213 ); QVERIFY( calendar->setYMD( testDate, 2000, 3, 1 ) ); QCOMPARE( calendar->year(testDate), 2000 ); QCOMPARE( calendar->month(testDate), 3 ); QCOMPARE( calendar->day(testDate), 1 ); QDate newDate = calendar->addYears(testDate, 4); QCOMPARE( newDate.year(), 2566 ); QCOMPARE( calendar->daysInYear(newDate), 355 ); newDate = calendar->addMonths( testDate, -4 ); QCOMPARE( newDate.year(), 2561 ); QCOMPARE( newDate.month(), 11 ); QCOMPARE( newDate.day(), 10 ); newDate = calendar->addDays( newDate, 20 ); QCOMPARE( newDate.year(), 2561 ); QCOMPARE( newDate.month(), 11 ); QCOMPARE( newDate.day(), 30 ); } void KCalendarTest::testIndianNational() { const KCalendarSystem *calendar = KCalendarSystem::create("indian-national"); QDate testDate( 2005, 9, 10 ); calendar->setYMD( testDate, 2000, 1, 1 ); calendar->setYMD( testDate, 2000, 1, 10 ); calendar->setYMD( testDate, 2000, 2, 10 ); calendar->setYMD( testDate, 2000, 5, 20 ); calendar->setYMD( testDate, 2000, 7, 1 ); calendar->setYMD( testDate, 2000, 7, 10 ); calendar->setYMD( testDate, 2000, 9, 10 ); calendar->setYMD( testDate, 2000, 12, 20 ); QCOMPARE( calendar->isLeapYear( 2009 ), false ); QCOMPARE( calendar->isLeapYear( testDate ), false ); QVERIFY( calendar->setYMD( testDate, 2000, 3, 1 ) ); QCOMPARE( calendar->year( testDate ), 2000 ); QCOMPARE( calendar->month( testDate ), 3 ); QCOMPARE( calendar->day( testDate ), 1 ); } void KCalendarTest::testGregorianBasic() { const KCalendarSystem *calendar = KCalendarSystem::create(QString("gregorian")); QCOMPARE( calendar->calendarType(), QString("gregorian") ); QCOMPARE( KCalendarSystem::calendarLabel( QString("gregorian") ), QString("Gregorian") ); QEXPECT_FAIL("", "QDate is Julian, get right date", Continue); QCOMPARE( calendar->epoch(), QDate( 1, 1, 1 ) ); QCOMPARE( calendar->earliestValidDate(), QDate( -4712, 1, 1 ) ); QCOMPARE( calendar->latestValidDate(), QDate( 9999, 12, 31 ) ); testValid( calendar, 10000, 13, 32, QDate( -5000, 1, 1 ) ); QCOMPARE( calendar->isLeapYear( 2007 ), false ); QCOMPARE( calendar->isLeapYear( 2008 ), true ); QCOMPARE( calendar->isLeapYear( 1900 ), false ); QCOMPARE( calendar->isLeapYear( 2000 ), true ); QCOMPARE( calendar->isLeapYear( QDate( 2007, 1, 1 ) ), false ); QCOMPARE( calendar->isLeapYear( QDate( 2008, 1, 1 ) ), true ); QCOMPARE( calendar->daysInWeek( QDate( 2007, 1, 1 ) ), 7 ); QCOMPARE( calendar->monthsInYear( QDate( 2007, 1, 1 ) ), 12 ); testYear( calendar, QDate( 2007, 7, 9 ), 2007, QString("07"), QString("2007") ); testMonth( calendar, QDate( 2007, 7, 9 ), 7, QString("7"), QString("07") ); testDay( calendar, QDate( 2007, 7, 9 ), 9, QString("9"), QString("09") ); testWeekDayName( calendar, 6, QDate( 2007, 7, 28 ), QString("Sat"), QString("Saturday") ); testMonthName( calendar, 12, 2007, QDate( 2007, 12, 20 ), QString("Dec"), QString("December"), QString("of Dec"), QString("of December") ); QCOMPARE( calendar->monthsInYear( QDate( 2007, 1, 1 ) ), 12 ); QCOMPARE( calendar->weekStartDay(), 1 ); QCOMPARE( calendar->weekDayOfPray(), 7 ); QCOMPARE( calendar->isProleptic(), false ); QCOMPARE( calendar->isLunar(), false ); QCOMPARE( calendar->isLunisolar(), false ); QCOMPARE( calendar->isSolar(), true ); + + testRoundTrip( calendar ); } void KCalendarTest::testGregorianYmd() { const KCalendarSystem *calendar = KCalendarSystem::create(QString("gregorian")); testYmd( calendar, 2007, 1, 1, QDate( 2007, 1, 1 ).toJulianDay() ); } // Test Hebrew Calendar System void KCalendarTest::testHebrewBasic() { -/* const KCalendarSystem *calendar = KCalendarSystem::create(QString("hebrew")); QCOMPARE( calendar->calendarType(), QString("hebrew") ); QCOMPARE( KCalendarSystem::calendarLabel( QString("hebrew") ), QString("Hebrew") ); - QCOMPARE( calendar->epoch(), QDate( 1, 1, 1 ) ); - QCOMPARE( calendar->earliestValidDate(), QDate( -4712, 1, 2 ) ); - QCOMPARE( calendar->latestValidDate(), QDate( 9999, 12, 31 ) ); + testEpoch( calendar, 1, 1, 1, 347998 ); + testEarliestValidDate( calendar, 5344, 1, 1, 2299498 ); + testLatestValidDate( calendar, 8119, 13, 29, 3313431 ); - testValid( calendar, 10000, 13, 32, QDate( -5000, 1, 1 ) ); + QDate testDate = QDate::fromJulianDay( 2450340 ); //5756-12-29 Not Leap + QCOMPARE( calendar->isLeapYear( 5756 ), false ); + QCOMPARE( calendar->isLeapYear( testDate ), false ); + QCOMPARE( calendar->monthsInYear( testDate ), 12 ); + QCOMPARE( calendar->month( testDate ), 12 ); + QCOMPARE( calendar->monthName( testDate ), QString("Elul") ); - QCOMPARE( calendar->isLeapYear( 2007 ), false ); - QCOMPARE( calendar->isLeapYear( 2008 ), true ); - QCOMPARE( calendar->isLeapYear( 1900 ), false ); - QCOMPARE( calendar->isLeapYear( 2000 ), true ); - QCOMPARE( calendar->isLeapYear( QDate( 2007, 1, 1 ) ), false ); - QCOMPARE( calendar->isLeapYear( QDate( 2008, 1, 1 ) ), true ); + testDate = QDate::fromJulianDay( 2450723 ); //5757-13-29 Leap + QCOMPARE( calendar->isLeapYear( 5757 ), true ); + QCOMPARE( calendar->isLeapYear( testDate ), true ); + QCOMPARE( calendar->monthsInYear( testDate ), 13 ); + QCOMPARE( calendar->month( testDate ), 13 ); + QCOMPARE( calendar->monthName( testDate ), QString("Elul") ); - QCOMPARE( calendar->daysInWeek( QDate( 2007, 1, 1 ) ), 7 ); - QCOMPARE( calendar->monthsInYear( QDate( 2007, 1, 1 ) ), 12 ); + QCOMPARE( calendar->daysInWeek( testDate ), 7 ); - testYear( calendar, QDate( 2007, 7, 9 ), 2007, QString("07"), QString("2007") ); - testMonth( calendar, QDate( 2007, 7, 9 ), 7, QString("7"), QString("07") ); - testDay( calendar, QDate( 2007, 7, 9 ), 9, QString("9"), QString("09") ); + testDate = QDate::fromJulianDay( 2432090 ); // 5707-01-01 Thursday + testYear( calendar, testDate, 5707, "07", "5707" ); + testMonth( calendar, testDate, 1, "1", "01" ); + testDay( calendar, testDate, 1, "1", "01" ); - testWeekDayName( calendar, 6, QDate( 2007, 7, 28 ), - QString("Sat"), QString("Saturday") ); - testMonthName( calendar, 12, 2007, QDate( 2007, 12, 20 ), - QString("Dec"), QString("December"), - QString("of Dec"), QString("of December") ); + testWeekDayName( calendar, 4, testDate, "Thu", "Thursday" ); + testMonthName( calendar, 1, 5707, testDate, "Tishrey", "Tishrey", "of Tishrey", "of Tishrey" ); - QCOMPARE( calendar->monthsInYear( QDate( 2007, 1, 1 ) ), 12 ); QCOMPARE( calendar->weekStartDay(), 1 ); QCOMPARE( calendar->weekDayOfPray(), 6 ); QCOMPARE( calendar->isProleptic(), false ); QCOMPARE( calendar->isLunar(), false ); QCOMPARE( calendar->isLunisolar(), true ); QCOMPARE( calendar->isSolar(), false ); -*/ + + testRoundTrip( calendar ); } void KCalendarTest::testHebrewYmd() { // const KCalendarSystem *calendar = KCalendarSystem::create(QString("hebrew")); // testYmd( calendar, 2007, 1, 1, QDate( 2007, 1, 1 ).toJulianDay() ); } void KCalendarTest::testHebrewCompare() { /* const KCalendarSystemHebrew *calendar = new KCalendarSystemHebrew(); QDate setOldDate, setNewDate; for ( QDate testDate( 1900, 1, 1 ); testDate.year() == 2100; testDate.addDays(1) ) { QCOMPARE( calendar->year( testDate ), calendar->oldyear( testDate ) ); QCOMPARE( calendar->month( testDate ), calendar->oldmonth( testDate ) ); QCOMPARE( calendar->day( testDate ), calendar->oldday( testDate ) ); calendar->setYMD(setOldDate, calendar->oldyear( testDate ), calendar->oldmonth( testDate ), calendar->oldday( testDate ) ); calendar->setDate(setNewDate, calendar->oldyear( testDate ), calendar->oldmonth( testDate ), calendar->oldday( testDate ) ); QCOMPARE( setNewDate.toJulianDay(), setOldDate.toJulianDay() ); } */ } // Test Hijri Calendar System void KCalendarTest::testHijriBasic() { const KCalendarSystem *calendar = KCalendarSystem::create(QString("hijri")); QCOMPARE( calendar->calendarType(), QString("hijri") ); QCOMPARE( KCalendarSystem::calendarLabel( QString("hijri") ), QString("Hijri") ); QCOMPARE( calendar->epoch(), QDate( 622, 7, 16 ) ); QCOMPARE( calendar->earliestValidDate(), QDate( 622, 7, 16 ) ); QCOMPARE( calendar->latestValidDate(), QDate( 10323, 10, 21) ); testValid( calendar, 10000, 13, 31, QDate( 1, 1, 1 ) ); QCOMPARE( calendar->isLeapYear( 1427 ), false ); QCOMPARE( calendar->isLeapYear( 1428 ), true ); QCOMPARE( calendar->isLeapYear( QDate( 2007, 1, 1 ) ), false ); QCOMPARE( calendar->isLeapYear( QDate( 2008, 1, 1 ) ), true ); QCOMPARE( calendar->daysInWeek( QDate( 2007, 1, 1 ) ), 7 ); QCOMPARE( calendar->monthsInYear( QDate( 2007, 1, 1 ) ), 12 ); testYear( calendar, QDate( 2005, 9, 10 ), 1426, QString("26"), QString("1426") ); testMonth( calendar, QDate( 2005, 9, 10 ), 8, QString("8"), QString("08") ); testDay( calendar, QDate( 2005, 9, 10 ), 6, QString("6"), QString("06") ); testWeekDayName( calendar, 6, QDate( 2005, 9, 10 ), QString("Sab"), QString("Yaum al-Sabt") ); testMonthName( calendar, 12, 1428, QDate( 2007, 12, 20 ), QString("Hijjah"), QString("Thu al-Hijjah"), QString("of Hijjah"), QString("of Thu al-Hijjah") ); QCOMPARE( calendar->monthsInYear( QDate( 2005, 9, 10 ) ), 12 ); QCOMPARE( calendar->weekStartDay(), 1 ); QCOMPARE( calendar->weekDayOfPray(), 5 ); QCOMPARE( calendar->isProleptic(), false ); QCOMPARE( calendar->isLunar(), true ); QCOMPARE( calendar->isLunisolar(), false ); QCOMPARE( calendar->isSolar(), false ); + + testRoundTrip( calendar ); } void KCalendarTest::testHijriYmd() { const KCalendarSystem *calendar = KCalendarSystem::create(QString("hijri")); testYmd( calendar, 1426, 8, 6, QDate( 2005, 9, 10 ).toJulianDay() ); } // Jalali Calendar System void KCalendarTest::testJalaliBasic() { const KCalendarSystem *calendar = KCalendarSystem::create(QString( "jalali" )); QCOMPARE( calendar->calendarType(), QString("jalali") ); QCOMPARE( KCalendarSystem::calendarLabel( QString("jalali") ), QString("Jalali") ); //Birashk argorithm only good between AP 1244-01-01 to 1530-12-29 (AD 1865 to 2152) QCOMPARE( calendar->epoch(), QDate( 622, 3, 19 ) ); QCOMPARE( calendar->earliestValidDate(), QDate( 1865, 03, 21 ) ); QCOMPARE( calendar->latestValidDate(), QDate( 2152, 03, 19 ) ); QCOMPARE( calendar->isValid( 1243, 12, 29 ), false ); QCOMPARE( calendar->isValid( 1531, 1, 1 ), false ); QCOMPARE( calendar->isLeapYear( 1386 ), false ); QCOMPARE( calendar->isLeapYear( 1387 ), true ); QCOMPARE( calendar->isLeapYear( QDate( 2008, 1, 1 ) ), false ); QCOMPARE( calendar->isLeapYear( QDate( 2009, 1, 1 ) ), true ); QCOMPARE( calendar->daysInWeek( QDate( 2007, 1, 1 ) ), 7 ); QCOMPARE( calendar->monthsInYear( QDate( 2007, 1, 1 ) ), 12 ); testYear( calendar, QDate( 2005, 8, 31 ), 1384, QString("84"), QString("1384") ); testMonth( calendar, QDate( 2005, 8, 31 ), 6, QString("6"), QString("06") ); testDay( calendar, QDate( 2005, 8, 31 ), 9, QString("9"), QString("09") ); testWeekDayName( calendar, 3, QDate( 2005, 8, 31 ), QString("4sh"), QString("Chahar shanbe") ); testMonthName( calendar, 6, 1384, QDate( 2005, 8, 31 ), QString("Sha"), QString("Shahrivar"), QString("of Sha"), QString("of Shahrivar") ); QCOMPARE( calendar->monthsInYear( QDate( 2005, 8, 31 ) ), 12 ); QCOMPARE( calendar->weekStartDay(), 1 ); QCOMPARE( calendar->weekDayOfPray(), 5 ); QCOMPARE( calendar->isProleptic(), false ); QCOMPARE( calendar->isLunar(), false ); QCOMPARE( calendar->isLunisolar(), false ); QCOMPARE( calendar->isSolar(), true ); testRoundTrip( calendar ); //Special cases using Birashk argorithm QCOMPARE( calendar->isLeapYear( 1403 ), true ); QCOMPARE( calendar->isLeapYear( 1404 ), false ); QCOMPARE( calendar->isLeapYear( 1436 ), true ); QCOMPARE( calendar->isLeapYear( 1437 ), false ); } void KCalendarTest::testJalaliYmd() { const KCalendarSystem *calendar = KCalendarSystem::create( QString("jalali") ); testYmd( calendar, 1384, 6, 9, QDate( 2005, 8, 31 ).toJulianDay() ); } // generic test functions, call from calendar system specific ones // Simply tests valid ranges of ymd values, testYmd covers all other dates void KCalendarTest::testValid( const KCalendarSystem *calendar, int highInvalidYear, int highInvalidMonth, int highInvalidDay, QDate invalidDate ) { // min/max year QCOMPARE( calendar->isValid( 0, 1, 1 ), false ); QCOMPARE( calendar->isValid( highInvalidYear, 1, 1 ), false ); // min/max month QCOMPARE( calendar->isValid( 1, 0, 1 ), false ); QCOMPARE( calendar->isValid( 1, highInvalidMonth, 1 ), false ); // min/max day QCOMPARE( calendar->isValid( 1, 1, 0 ), false ); QCOMPARE( calendar->isValid( 1, 1, highInvalidDay ), false ); QCOMPARE( calendar->isValid( 1, 1, 1 ), true ); QCOMPARE( calendar->isValid( invalidDate ), false ); QCOMPARE( calendar->isValid( QDate( 2000, 1, 1 ) ), true ); } +void KCalendarTest::testEpoch( const KCalendarSystem *calendar, int y, int m, int d, int jd ) +{ + QCOMPARE( calendar->epoch(), QDate::fromJulianDay( jd ) ); + if ( calendar->epoch() >= calendar->earliestValidDate() ) { + testYmd( calendar, y, m, d, jd ); + } +} + +void KCalendarTest::testEarliestValidDate( const KCalendarSystem *calendar, int y, int m, int d, int jd ) +{ + QCOMPARE( calendar->earliestValidDate(), QDate::fromJulianDay( jd ) ); + testYmd( calendar, y, m, d, jd ); +} + +void KCalendarTest::testLatestValidDate( const KCalendarSystem *calendar, int y, int m, int d, int jd ) +{ + QCOMPARE( calendar->latestValidDate(), QDate::fromJulianDay( jd ) ); + testYmd( calendar, y, m, d, jd ); +} + void KCalendarTest::testYmd( const KCalendarSystem *calendar, int y, int m, int d, int jd ) { QDate testDate1 = QDate::fromJulianDay( jd ); QCOMPARE( calendar->year( testDate1 ), y ); QCOMPARE( calendar->month( testDate1 ), m ); QCOMPARE( calendar->day( testDate1 ), d ); QDate testDate2; QVERIFY( calendar->setYMD( testDate2, y, m, d ) ); QCOMPARE( testDate1, testDate2 ); } void KCalendarTest::testYear( const KCalendarSystem *calendar, QDate date, int year, QString shortString, QString longString ) { QCOMPARE( calendar->year( date ), year ); QCOMPARE( calendar->yearString( date, KCalendarSystem::ShortFormat ), shortString ); QCOMPARE( calendar->yearString( date, KCalendarSystem::LongFormat ), longString ); int i; QCOMPARE( calendar->yearStringToInteger( longString, i ), year ); QCOMPARE( i, longString.length() ); } void KCalendarTest::testMonth( const KCalendarSystem *calendar, QDate date, int month, QString shortString, QString longString ) { QCOMPARE( calendar->month( date ), month ); QCOMPARE( calendar->monthString( date, KCalendarSystem::ShortFormat ), shortString ); QCOMPARE( calendar->monthString( date, KCalendarSystem::LongFormat ), longString ); int i; QCOMPARE( calendar->monthStringToInteger( longString, i ), month ); QCOMPARE( i, longString.length() ); } void KCalendarTest::testDay( const KCalendarSystem *calendar, QDate date, int day, QString shortString, QString longString ) { QCOMPARE( calendar->day( date ), day ); QCOMPARE( calendar->dayString( date, KCalendarSystem::ShortFormat ), shortString ); QCOMPARE( calendar->dayString( date, KCalendarSystem::LongFormat ), longString ); int i; QCOMPARE( calendar->dayStringToInteger( longString, i ), day ); QCOMPARE( i, longString.length() ); } // Pass in the week day number, the equivalent QDate, and the short and long day names // e.g. testWeekDayName( cal, 6, QDate(y,m,d), QString("Monday"), QString("Mon") ) void KCalendarTest::testWeekDayName( const KCalendarSystem *calendar, int weekDay, QDate date, QString shortName, QString longName ) { QCOMPARE( calendar->weekDayName( weekDay, KCalendarSystem::ShortDayName ), shortName ); QCOMPARE( calendar->weekDayName( weekDay, KCalendarSystem::LongDayName ), longName ); QCOMPARE( calendar->weekDayName( weekDay ), longName ); // Test week day < min QCOMPARE( calendar->weekDayName( 0, KCalendarSystem::ShortDayName ), QString() ); QCOMPARE( calendar->weekDayName( 0, KCalendarSystem::LongDayName ), QString() ); // Test week day > max QCOMPARE( calendar->weekDayName( calendar->daysInWeek( date ) + 1, KCalendarSystem::ShortDayName ), QString() ); QCOMPARE( calendar->weekDayName( calendar->daysInWeek( date ) + 1, KCalendarSystem::LongDayName ), QString() ); // QDate parameter tests, so always in valid range QCOMPARE( calendar->weekDayName( date, KCalendarSystem::ShortDayName ), shortName ); QCOMPARE( calendar->weekDayName( date, KCalendarSystem::LongDayName ), longName ); QCOMPARE( calendar->weekDayName( date ), longName ); } void KCalendarTest::testMonthName( const KCalendarSystem *calendar, int month, int year, QDate date, QString shortName, QString longName, QString shortNamePossessive, QString longNamePossessive ) { QCOMPARE( calendar->monthName( month, year, KCalendarSystem::ShortName ), shortName ); QCOMPARE( calendar->monthName( month, year, KCalendarSystem::LongName ), longName ); QCOMPARE( calendar->monthName( month, year, KCalendarSystem::ShortNamePossessive ), shortNamePossessive ); QCOMPARE( calendar->monthName( month, year, KCalendarSystem::LongNamePossessive ), longNamePossessive ); QCOMPARE( calendar->monthName( month, year ), longName ); // Test month < min QCOMPARE( calendar->monthName( 0, year, KCalendarSystem::ShortName ), QString() ); QCOMPARE( calendar->monthName( 0, year, KCalendarSystem::LongName ), QString() ); QCOMPARE( calendar->monthName( 0, year, KCalendarSystem::ShortName ), QString() ); QCOMPARE( calendar->monthName( 0, year, KCalendarSystem::LongName ), QString() ); // Test month > max QCOMPARE( calendar->monthName( calendar->monthsInYear( date ) + 1, year, KCalendarSystem::ShortName ), QString() ); QCOMPARE( calendar->monthName( calendar->monthsInYear( date ) + 1, year, KCalendarSystem::LongName ), QString() ); QCOMPARE( calendar->monthName( calendar->monthsInYear( date ) + 1, year, KCalendarSystem::ShortName ), QString() ); QCOMPARE( calendar->monthName( calendar->monthsInYear( date ) + 1, year, KCalendarSystem::LongName ), QString() ); // QDate parameter tests, so always in valid range QCOMPARE( calendar->monthName( date, KCalendarSystem::ShortName ), shortName ); QCOMPARE( calendar->monthName( date, KCalendarSystem::LongName ), longName ); QCOMPARE( calendar->monthName( date, KCalendarSystem::ShortNamePossessive ), shortNamePossessive ); QCOMPARE( calendar->monthName( date, KCalendarSystem::LongNamePossessive ), longNamePossessive ); QCOMPARE( calendar->monthName( date ), longName ); } +void KCalendarTest::testRoundTrip( const KCalendarSystem *calendar ) +{ + kDebug() << "Testing round trip of dates for Calendar System " << calendar->calendarType(); + kDebug() << "This may take some time, or you may have created an infinite loop."; + kDebug() << "Uncomment the loop debug message to see each date comparison."; + + int testYear, testMonth, testDay; + QDate testDate; + QDate loopDate = calendar->earliestValidDate(); + QByteArray msg; + while ( loopDate <= calendar->latestValidDate() ) { + testYear = calendar->year( loopDate ); + testMonth = calendar->month( loopDate ); + testDay = calendar->day( loopDate ); + calendar->setDate( testDate, testYear, testMonth, testDay ); + // Uncomment this to see each comparison printed + // or + // Uncomment the QEXPECT_FAIL statements to check all dates regardless of occasional failures + QByteArray msg = QByteArray::number( loopDate.toJulianDay() ) + " = " + + QByteArray::number( testYear ) + '-' + + QByteArray::number( testMonth ) + '-' + + QByteArray::number( testDay ) + " = " + + QByteArray::number( testDate.toJulianDay() ); + /* + kDebug() << msg; + */ + if ( testMonth <= 0 || testDay <= 0 ) { // year <= 0 is OK + msg.prepend( "Round Trip : JD to Date failed : " ); + QEXPECT_FAIL( "", msg.data(), Continue ); + } else if ( testDate.toJulianDay() == 0 ) { + msg.prepend( "Round Trip : Date to JD failed : " ); + QEXPECT_FAIL( "", msg.data(), Continue ); + } else if ( loopDate != testDate ) { + msg.prepend( "Round Trip : JD's differ : " ); + QEXPECT_FAIL( "", msg.data(), Continue ); + } + QCOMPARE( loopDate.toJulianDay(), testDate.toJulianDay() ); + loopDate = loopDate.addDays(1); + } +} -// Tests to compare new base methods are equal to QDate for Gregorian case +// Tests to compare new base methods are equal to QDate for Gregorian case void KCalendarTest::testQDateYearMonthDay() { const KCalendarSystem *calendar = KCalendarSystem::create(QString( "gregorian" )); testQDateYMD( calendar, 2000, 1, 1 ); testQDateYMD( calendar, 2000, 2, 28 ); testQDateYMD( calendar, 2000, 2, 29 ); testQDateYMD( calendar, 2000, 6, 15 ); testQDateYMD( calendar, 2000, 12, 31 ); testQDateYMD( calendar, 9999, 1, 1 ); testQDateYMD( calendar, 9999, 6, 15 ); testQDateYMD( calendar, 9999, 12, 31 ); testQDateYMD( calendar, 1, 1, 1 ); testQDateYMD( calendar, 1, 6, 15 ); testQDateYMD( calendar, 1, 12, 31 ); testQDateYMD( calendar, -4712, 1, 2 ); testQDateYMD( calendar, -4712, 6, 15 ); testQDateYMD( calendar, -4712, 12, 31 ); } void KCalendarTest::testQDateYMD( const KCalendarSystem *calendar, int y, int m, int d ) { QDate testDate; calendar->setDate( testDate, y, m, d ); QCOMPARE( calendar->year( testDate ), testDate.year() ); QCOMPARE( calendar->month( testDate ), testDate.month() ); QCOMPARE( calendar->day( testDate ), testDate.day() ); } void KCalendarTest::testQDateAddYears() { const KCalendarSystem *calendar = KCalendarSystem::create(QString( "gregorian" )); QDate testDate; calendar->setDate( testDate, 2000, 1, 1 ); QCOMPARE( calendar->addYears( testDate, -1 ), testDate.addYears( -1 ) ); QCOMPARE( calendar->addYears( testDate, 1 ), testDate.addYears( 1 ) ); calendar->setDate( testDate, 2000, 2, 29 ); QCOMPARE( calendar->addYears( testDate, 1 ), testDate.addYears( 1 ) ); calendar->setDate( testDate, -2000, 1, 1 ); QCOMPARE( calendar->addYears( testDate, -1 ), testDate.addYears( -1 ) ); QCOMPARE( calendar->addYears( testDate, 1 ), testDate.addYears( 1 ) ); } void KCalendarTest::testQDateAddMonths() { const KCalendarSystem *calendar = KCalendarSystem::create(QString( "gregorian" )); QDate testDate; calendar->setDate( testDate, 2000, 1, 1 ); QCOMPARE( calendar->addMonths( testDate, -1 ), testDate.addMonths( -1 ) ); QCOMPARE( calendar->addMonths( testDate, 1 ), testDate.addMonths( 1 ) ); calendar->setDate( testDate, 2000, 3, 1 ); QCOMPARE( calendar->addMonths( testDate, -1 ), testDate.addMonths( -1 ) ); QCOMPARE( calendar->addMonths( testDate, 1 ), testDate.addMonths( 1 ) ); calendar->setDate( testDate, 2000, 12, 1 ); QCOMPARE( calendar->addMonths( testDate, -1 ), testDate.addMonths( -1 ) ); QCOMPARE( calendar->addMonths( testDate, 1 ), testDate.addMonths( 1 ) ); calendar->setDate( testDate, 2000, 1, 1 ); QCOMPARE( calendar->addMonths( testDate, -30 ), testDate.addMonths( -30 ) ); QCOMPARE( calendar->addMonths( testDate, 30 ), testDate.addMonths( 30 ) ); calendar->setDate( testDate, -2000, 1, 1 ); QCOMPARE( calendar->addMonths( testDate, -1 ), testDate.addMonths( -1 ) ); QCOMPARE( calendar->addMonths( testDate, 1 ), testDate.addMonths( 1 ) ); calendar->setDate( testDate, -2000, 3, 1 ); QCOMPARE( calendar->addMonths( testDate, -1 ), testDate.addMonths( -1 ) ); QCOMPARE( calendar->addMonths( testDate, 1 ), testDate.addMonths( 1 ) ); calendar->setDate( testDate, -2000, 12, 1 ); QCOMPARE( calendar->addMonths( testDate, -1 ), testDate.addMonths( -1 ) ); QCOMPARE( calendar->addMonths( testDate, 1 ), testDate.addMonths( 1 ) ); calendar->setDate( testDate, -2000, 1, 1 ); QCOMPARE( calendar->addMonths( testDate, -30 ), testDate.addMonths( -30 ) ); QCOMPARE( calendar->addMonths( testDate, 30 ), testDate.addMonths( 30 ) ); } void KCalendarTest::testQDateAddDays() { const KCalendarSystem *calendar = KCalendarSystem::create(QString( "gregorian" )); QDate testDate; calendar->setDate( testDate, 2000, 1, 1 ); QCOMPARE( calendar->addDays( testDate, -1 ), testDate.addDays( -1 ) ); QCOMPARE( calendar->addDays( testDate, 1 ), testDate.addDays( 1 ) ); calendar->setDate( testDate, -2000, 1, 1 ); QCOMPARE( calendar->addDays( testDate, -1 ), testDate.addDays( -1 ) ); QCOMPARE( calendar->addDays( testDate, 1 ), testDate.addDays( 1 ) ); } void KCalendarTest::testQDateDaysInYear() { const KCalendarSystem *calendar = KCalendarSystem::create(QString( "gregorian" )); QDate testDate; calendar->setDate( testDate, 1900, 1, 1 ); QCOMPARE( calendar->daysInYear( testDate ), testDate.daysInYear() ); calendar->setDate( testDate, 1999, 1, 1 ); QCOMPARE( calendar->daysInYear( testDate ), testDate.daysInYear() ); calendar->setDate( testDate, 2000, 1, 1 ); QCOMPARE( calendar->daysInYear( testDate ), testDate.daysInYear() ); calendar->setDate( testDate, 2001, 1, 1 ); QCOMPARE( calendar->daysInYear( testDate ), testDate.daysInYear() ); calendar->setDate( testDate, 2002, 1, 1 ); QCOMPARE( calendar->daysInYear( testDate ), testDate.daysInYear() ); calendar->setDate( testDate, 2003, 1, 1 ); QCOMPARE( calendar->daysInYear( testDate ), testDate.daysInYear() ); calendar->setDate( testDate, 2004, 1, 1 ); QCOMPARE( calendar->daysInYear( testDate ), testDate.daysInYear() ); calendar->setDate( testDate, 2005, 1, 1 ); QCOMPARE( calendar->daysInYear( testDate ), testDate.daysInYear() ); calendar->setDate( testDate, -4700, 1, 1 ); QEXPECT_FAIL("", "Returns 365 instead of 366", Continue); QCOMPARE( calendar->daysInYear( testDate ), testDate.daysInYear() ); calendar->setDate( testDate, -4000, 1, 1 ); QEXPECT_FAIL("", "Returns 365 instead of 366", Continue); QCOMPARE( calendar->daysInYear( testDate ), testDate.daysInYear() ); calendar->setDate( testDate, 1, 1, 1 ); QCOMPARE( calendar->daysInYear( testDate ), testDate.daysInYear() ); calendar->setDate( testDate, 9996, 1, 1 ); QCOMPARE( calendar->daysInYear( testDate ), testDate.daysInYear() ); calendar->setDate( testDate, 9999, 1, 1 ); QCOMPARE( calendar->daysInYear( testDate ), testDate.daysInYear() ); } void KCalendarTest::testQDateDaysInMonth() { const KCalendarSystem *calendar = KCalendarSystem::create(QString( "gregorian" )); QDate testDate; // Test all months calendar->setDate( testDate, 2000, 1, 1 ); QCOMPARE( calendar->daysInMonth( testDate ), testDate.daysInMonth() ); calendar->setDate( testDate, 2000, 2, 1 ); QCOMPARE( calendar->daysInMonth( testDate ), testDate.daysInMonth() ); calendar->setDate( testDate, 2000, 3, 1 ); QCOMPARE( calendar->daysInMonth( testDate ), testDate.daysInMonth() ); calendar->setDate( testDate, 2000, 4, 1 ); QCOMPARE( calendar->daysInMonth( testDate ), testDate.daysInMonth() ); calendar->setDate( testDate, 2000, 5, 1 ); QCOMPARE( calendar->daysInMonth( testDate ), testDate.daysInMonth() ); calendar->setDate( testDate, 2000, 6, 1 ); QCOMPARE( calendar->daysInMonth( testDate ), testDate.daysInMonth() ); calendar->setDate( testDate, 2000, 7, 1 ); QCOMPARE( calendar->daysInMonth( testDate ), testDate.daysInMonth() ); calendar->setDate( testDate, 2000, 8, 1 ); QCOMPARE( calendar->daysInMonth( testDate ), testDate.daysInMonth() ); calendar->setDate( testDate, 2000, 9, 1 ); QCOMPARE( calendar->daysInMonth( testDate ), testDate.daysInMonth() ); calendar->setDate( testDate, 2000, 10, 1 ); QCOMPARE( calendar->daysInMonth( testDate ), testDate.daysInMonth() ); calendar->setDate( testDate, 2000, 11, 1 ); QCOMPARE( calendar->daysInMonth( testDate ), testDate.daysInMonth() ); calendar->setDate( testDate, 2000, 12, 1 ); QCOMPARE( calendar->daysInMonth( testDate ), testDate.daysInMonth() ); // Test Feb in leap and normal years calendar->setDate( testDate, 2000, 2, 1 ); QCOMPARE( calendar->daysInMonth( testDate ), testDate.daysInMonth() ); calendar->setDate( testDate, 2001, 2, 1 ); QCOMPARE( calendar->daysInMonth( testDate ), testDate.daysInMonth() ); // Test max date calendar->setDate( testDate, 9999, 12, 1 ); QCOMPARE( calendar->daysInMonth( testDate ), testDate.daysInMonth() ); } void KCalendarTest::testQDateDayOfYear() { const KCalendarSystem *calendar = KCalendarSystem::create(QString( "gregorian" )); QDate testDate; calendar->setDate( testDate, 2000, 1, 1 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); calendar->setDate( testDate, 2000, 2, 29 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); calendar->setDate( testDate, 2000, 6, 1 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); calendar->setDate( testDate, 2000, 12, 1 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); calendar->setDate( testDate, 2000, 12, 31 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); calendar->setDate( testDate, -2000, 1, 1 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); calendar->setDate( testDate, -2000, 2, 29 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); calendar->setDate( testDate, -2000, 6, 1 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); calendar->setDate( testDate, -2000, 12, 1 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); calendar->setDate( testDate, -2000, 12, 31 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); calendar->setDate( testDate, -4712, 1, 2 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); calendar->setDate( testDate, -4712, 2, 29 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); calendar->setDate( testDate, -4712, 6, 1 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); calendar->setDate( testDate, -4712, 12, 1 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); calendar->setDate( testDate, -4712, 12, 31 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); calendar->setDate( testDate, 9999, 1, 1 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); //QDate::dayOfYear doesn't check if invalid!!! //calendar->setDate( testDate, 9999, 2, 29 ); //QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); calendar->setDate( testDate, 9999, 6, 1 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); calendar->setDate( testDate, 9999, 12, 1 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); calendar->setDate( testDate, 9999, 12, 31 ); QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() ); } void KCalendarTest::testQDateDayOfWeek() { const KCalendarSystem *calendar = KCalendarSystem::create(QString( "gregorian" )); QDate testDate; calendar->setDate( testDate, 2000, 1, 1 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, 2000, 1, 2 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, 2000, 1, 3 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, 2000, 1, 4 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, 2000, 1, 5 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, 2000, 1, 6 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, 2000, 1, 7 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, -4712, 1, 2 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, -4712, 1, 3 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, -4712, 1, 4 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, -4712, 1, 5 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, -4712, 1, 6 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, -4712, 1, 7 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, -4712, 1, 8 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, 9999, 1, 1 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, 9999, 1, 2 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, 9999, 1, 3 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, 9999, 1, 4 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, 9999, 1, 5 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, 9999, 1, 6 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); calendar->setDate( testDate, 9999, 1, 7 ); QCOMPARE( calendar->dayOfWeek( testDate ), testDate.dayOfWeek() ); } // Don'r really need this as Gregorian currently uses QDate directly void KCalendarTest::testQDateIsLeapYear() { const KCalendarSystem *calendar = KCalendarSystem::create(QString( "gregorian" )); QDate testDate; calendar->setDate( testDate, 2000, 1, 1 ); QCOMPARE( calendar->isLeapYear( 2000 ), testDate.isLeapYear( 2000 ) ); QCOMPARE( calendar->isLeapYear( testDate ), testDate.isLeapYear( 2000 ) ); calendar->setDate( testDate, 2001, 1, 1 ); QCOMPARE( calendar->isLeapYear( 2001 ), testDate.isLeapYear( 2001 ) ); QCOMPARE( calendar->isLeapYear( testDate ), testDate.isLeapYear( 2001 ) ); calendar->setDate( testDate, 2004, 1, 1 ); QCOMPARE( calendar->isLeapYear( 2004 ), testDate.isLeapYear( 2004 ) ); QCOMPARE( calendar->isLeapYear( testDate ), testDate.isLeapYear( 2004 ) ); calendar->setDate( testDate, 1900, 1, 1 ); QCOMPARE( calendar->isLeapYear( 1900 ), testDate.isLeapYear( 1900 ) ); QCOMPARE( calendar->isLeapYear( testDate ), testDate.isLeapYear( 1900 ) ); calendar->setDate( testDate, 1901, 1, 1 ); QCOMPARE( calendar->isLeapYear( 1901 ), testDate.isLeapYear( 1901 ) ); QCOMPARE( calendar->isLeapYear( testDate ), testDate.isLeapYear( 1901 ) ); calendar->setDate( testDate, 1904, 1, 1 ); QCOMPARE( calendar->isLeapYear( 1904 ), testDate.isLeapYear( 1904 ) ); QCOMPARE( calendar->isLeapYear( testDate ), testDate.isLeapYear( 1904 ) ); calendar->setDate( testDate, -2000, 1, 1 ); QCOMPARE( calendar->isLeapYear( -2000 ), testDate.isLeapYear( -2000 ) ); QCOMPARE( calendar->isLeapYear( testDate ), testDate.isLeapYear( -2000 ) ); calendar->setDate( testDate, -2001, 1, 1 ); QCOMPARE( calendar->isLeapYear( -2001 ), testDate.isLeapYear( -2001 ) ); QCOMPARE( calendar->isLeapYear( testDate ), testDate.isLeapYear( -2001 ) ); calendar->setDate( testDate, -2004, 1, 1 ); QCOMPARE( calendar->isLeapYear( -2004 ), testDate.isLeapYear( -2004 ) ); QCOMPARE( calendar->isLeapYear( testDate ), testDate.isLeapYear( -2004 ) ); } - -void KCalendarTest::testRoundTrip( const KCalendarSystem *calendar ) -{ - int testYear, testMonth, testDay; - QDate testDate; - QDate loopDate = calendar->earliestValidDate(); - while ( loopDate <= calendar->latestValidDate() ) { - testYear = calendar->year( loopDate ); - testMonth = calendar->month( loopDate ); - testDay = calendar->day( loopDate ); - calendar->setDate( testDate, testYear, testMonth, testDay ); -//kDebug() << loopDate.toJulianDay() << " = " << testYear << "-" << testMonth << "-" << testDay << " = " << testDate.toJulianDay(); - if ( testYear == 0 && testMonth == 0 && testDay == 0 ) { -kDebug() << "JD to Date failed, JD = " << loopDate.toJulianDay(); - QEXPECT_FAIL("", "JD to Date failed, JD = " + loopDate.toJulianDay(), Continue); - } - if ( testDate.toJulianDay() == 0 ) { -kDebug() << "Date to JD failed, Date = " << testYear << '-' << testMonth << '-' << testDay; - QEXPECT_FAIL("", "Date to JD failed, Date = " + testYear + '-' + testMonth + '-' + testDay, Continue); - } - QCOMPARE( loopDate.toJulianDay(), testDate.toJulianDay() ); - loopDate = loopDate.addDays(1); - } - -} diff --git a/kdecore/tests/kcalendartest.h b/kdecore/tests/kcalendartest.h index f146c740ce..f1e0d0c9ef 100644 --- a/kdecore/tests/kcalendartest.h +++ b/kdecore/tests/kcalendartest.h @@ -1,65 +1,68 @@ #ifndef KCALENDARTEST_H #define KCALENDARTEST_H #include class KCalendarTest : public QObject { Q_OBJECT protected: void testValid( const KCalendarSystem *calendar, int highInvalidYear, int highInvalidMonth, int highInvalidDay, QDate invalidDate ); + void testEpoch( const KCalendarSystem *calendar, int y, int m, int d, int jd ); + void testEarliestValidDate( const KCalendarSystem *calendar, int y, int m, int d, int jd ); + void testLatestValidDate( const KCalendarSystem *calendar, int y, int m, int d, int jd ); void testYear( const KCalendarSystem *calendar, QDate date, int year, QString shortString, QString longString ); void testMonth( const KCalendarSystem *calendar, QDate date, int month, QString shortString, QString longString ); void testDay( const KCalendarSystem *calendar, QDate date, int day, QString shortString, QString longString ); void testYmd( const KCalendarSystem *calendar, int y, int m, int d, int jd ); void testWeekDayName( const KCalendarSystem *calendar, int weekDay, QDate date, QString shortName, QString longName ); void testMonthName( const KCalendarSystem *calendar, int month, int year, QDate date, QString shortName, QString longName, QString shortNamePossessive, QString longNamePossessive ); void testGregorianCompareQDate( const KCalendarSystem *calendar, int year, int month, int day ); void testQDateYMD( const KCalendarSystem *calendar, int y, int m, int d ); void testStrings( KLocale::DigitSet testDigitSet ); QDate setDayOfYearDate( const KCalendarSystem * calendar, int year, int dayOfYear ); QDate setIsoWeekDate( const KCalendarSystem * calendar, int year, int isoWeek, int dayOfWeek ); void testRoundTrip( const KCalendarSystem *calendar ); private Q_SLOTS: void testTypes(); void testLocale(); void testFormatDate(); void testReadDate(); void testStringForms(); void testIsoWeekDate(); void testDayOfYearDate(); void testGregorian(); void testHebrew(); void testHijri(); void testIndianNational(); void testGregorianBasic(); void testGregorianYmd(); void testHebrewBasic(); void testHebrewYmd(); void testHebrewCompare(); void testHijriBasic(); void testHijriYmd(); void testJalaliBasic(); void testJalaliYmd(); void testQDateYearMonthDay(); void testQDateAddYears(); void testQDateAddMonths(); void testQDateAddDays(); void testQDateDaysInYear(); void testQDateDaysInMonth(); void testQDateDayOfYear(); void testQDateDayOfWeek(); void testQDateIsLeapYear(); }; #endif