File indexing completed on 2022-12-06 19:12:05

0001 /*
0002     Copyright 2005 Brad Hards <bradh@kde.org>
0003     Copyright 2007, 2009, 2010, 2011 John Layt <john@layt.net>
0004 
0005     This library is free software; you can redistribute it and/or
0006     modify it under the terms of the GNU Library General Public
0007     License as published by the Free Software Foundation; either
0008     version 2 of the License, or (at your option) any later version.
0009 
0010     This library is distributed in the hope that it will be useful,
0011     but WITHOUT ANY WARRANTY; without even the implied warranty of
0012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013     Library General Public License for more details.
0014 
0015     You should have received a copy of the GNU Library General Public License
0016     along with this library; see the file COPYING.LIB.  If not, write to
0017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018     Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "kcalendartest.h"
0022 
0023 #include <locale.h>
0024 
0025 #include <QString>
0026 
0027 #include <QTest>
0028 #include "kcalendarsystem.h"
0029 #include "klocalizeddate.h"
0030 #include "klocalizedstring.h"
0031 #include "kconfiggroup.h"
0032 
0033 QTEST_MAIN(KCalendarTest)
0034 
0035 void KCalendarTest::testTypes()
0036 {
0037     const KCalendarSystem *calendar;
0038 
0039     calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
0040     QCOMPARE(calendar->calendarType(), QString("gregorian"));
0041     QCOMPARE(calendar->calendarSystem(), KLocale::GregorianCalendar);
0042     delete calendar;
0043     calendar = KCalendarSystem::create(KLocale::CopticCalendar);
0044     QCOMPARE(calendar->calendarType(), QString("coptic"));
0045     QCOMPARE(calendar->calendarSystem(), KLocale::CopticCalendar);
0046     delete calendar;
0047     calendar = KCalendarSystem::create(KLocale::EthiopianCalendar);
0048     QCOMPARE(calendar->calendarType(), QString("ethiopian"));
0049     QCOMPARE(calendar->calendarSystem(), KLocale::EthiopianCalendar);
0050     delete calendar;
0051     calendar = KCalendarSystem::create(KLocale::HebrewCalendar);
0052     QCOMPARE(calendar->calendarType(), QString("hebrew"));
0053     QCOMPARE(calendar->calendarSystem(), KLocale::HebrewCalendar);
0054     delete calendar;
0055     calendar = KCalendarSystem::create(KLocale::IslamicCivilCalendar);
0056     QCOMPARE(calendar->calendarType(), QString("hijri"));
0057     QCOMPARE(calendar->calendarSystem(), KLocale::IslamicCivilCalendar);
0058     delete calendar;
0059     calendar = KCalendarSystem::create(KLocale::IndianNationalCalendar);
0060     QCOMPARE(calendar->calendarType(), QString("indian-national"));
0061     QCOMPARE(calendar->calendarSystem(), KLocale::IndianNationalCalendar);
0062     delete calendar;
0063     calendar = KCalendarSystem::create(KLocale::JalaliCalendar);
0064     QCOMPARE(calendar->calendarType(), QString("jalali"));
0065     QCOMPARE(calendar->calendarSystem(), KLocale::JalaliCalendar);
0066     delete calendar;
0067     calendar = KCalendarSystem::create(KLocale::JapaneseCalendar);
0068     QCOMPARE(calendar->calendarType(), QString("japanese"));
0069     QCOMPARE(calendar->calendarSystem(), KLocale::JapaneseCalendar);
0070     delete calendar;
0071     calendar = KCalendarSystem::create(KLocale::JulianCalendar);
0072     QCOMPARE(calendar->calendarType(), QString("julian"));
0073     QCOMPARE(calendar->calendarSystem(), KLocale::JulianCalendar);
0074     delete calendar;
0075     calendar = KCalendarSystem::create(KLocale::MinguoCalendar);
0076     QCOMPARE(calendar->calendarType(), QString("minguo"));
0077     QCOMPARE(calendar->calendarSystem(), KLocale::MinguoCalendar);
0078     delete calendar;
0079     calendar = KCalendarSystem::create(KLocale::ThaiCalendar);
0080     QCOMPARE(calendar->calendarType(), QString("thai"));
0081     QCOMPARE(calendar->calendarSystem(), KLocale::ThaiCalendar);
0082     delete calendar;
0083     calendar = KCalendarSystem::create(KLocale::QDateCalendar);
0084     QCOMPARE(calendar->calendarType(), QString("gregorian"));
0085     QCOMPARE(calendar->calendarSystem(), KLocale::GregorianCalendar);
0086     delete calendar;
0087 
0088     QList<KLocale::CalendarSystem> list = KCalendarSystem::calendarSystemsList();
0089     QCOMPARE(list.count(), 11);
0090     QVERIFY(list.contains(KLocale::GregorianCalendar));
0091     QVERIFY(list.contains(KLocale::CopticCalendar));
0092     QVERIFY(list.contains(KLocale::EthiopianCalendar));
0093     QVERIFY(list.contains(KLocale::HebrewCalendar));
0094     QVERIFY(list.contains(KLocale::IslamicCivilCalendar));
0095     QVERIFY(list.contains(KLocale::IndianNationalCalendar));
0096     QVERIFY(list.contains(KLocale::JalaliCalendar));
0097     QVERIFY(list.contains(KLocale::JapaneseCalendar));
0098     QVERIFY(list.contains(KLocale::JulianCalendar));
0099     QVERIFY(list.contains(KLocale::MinguoCalendar));
0100     QVERIFY(list.contains(KLocale::ThaiCalendar));
0101     QVERIFY(list.contains(KLocale::QDateCalendar));
0102 
0103     QCOMPARE(KCalendarSystem::calendarLabel(KLocale::GregorianCalendar),      QString("Gregorian"));
0104     QCOMPARE(KCalendarSystem::calendarLabel(KLocale::CopticCalendar),         QString("Coptic"));
0105     QCOMPARE(KCalendarSystem::calendarLabel(KLocale::EthiopianCalendar),      QString("Ethiopian"));
0106     QCOMPARE(KCalendarSystem::calendarLabel(KLocale::HebrewCalendar),         QString("Hebrew"));
0107     QCOMPARE(KCalendarSystem::calendarLabel(KLocale::IslamicCivilCalendar),   QString("Islamic / Hijri (Civil)"));
0108     QCOMPARE(KCalendarSystem::calendarLabel(KLocale::IndianNationalCalendar), QString("Indian National"));
0109     QCOMPARE(KCalendarSystem::calendarLabel(KLocale::JalaliCalendar),         QString("Jalali"));
0110     QCOMPARE(KCalendarSystem::calendarLabel(KLocale::JapaneseCalendar),       QString("Japanese"));
0111     QCOMPARE(KCalendarSystem::calendarLabel(KLocale::JulianCalendar),         QString("Julian"));
0112     QCOMPARE(KCalendarSystem::calendarLabel(KLocale::MinguoCalendar),         QString("Taiwanese"));
0113     QCOMPARE(KCalendarSystem::calendarLabel(KLocale::ThaiCalendar),           QString("Thai"));
0114     QCOMPARE(KCalendarSystem::calendarLabel(KLocale::QDateCalendar),          QString("Gregorian"));
0115 
0116 }
0117 
0118 void KCalendarTest::testFormatDate()
0119 {
0120     KConfigGroup lcg(KSharedConfig::openConfig(), QString("Locale"));
0121     KConfigGroup cg = lcg.group(QString("KCalendarSystem %1").arg("gregorian"));
0122     cg.deleteGroup();
0123 
0124     KLocale *locale = new KLocale(*KLocale::global());
0125     locale->setDateFormatShort("%y-%m-%d");
0126     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar, locale);
0127 
0128     //Test default settings
0129     QDate testDate(2005, 10, 20);
0130     QCOMPARE(calendar->formatDate(testDate, "%Y"), QString("2005"));
0131     QCOMPARE(calendar->formatDate(testDate, "%C"), QString("20"));
0132     QCOMPARE(calendar->formatDate(testDate, "%y"), QString("05"));
0133     QCOMPARE(calendar->formatDate(testDate, "%m"), QString("10"));
0134     QCOMPARE(calendar->formatDate(testDate, "%n"), QString("10"));
0135     QCOMPARE(calendar->formatDate(testDate, "%d"), QString("20"));
0136     QCOMPARE(calendar->formatDate(testDate, "%e"), QString("20"));
0137     QCOMPARE(calendar->formatDate(testDate, "%B"), QString("October"));
0138     QCOMPARE(calendar->formatDate(testDate, "%b"), QString("Oct"));
0139     QCOMPARE(calendar->formatDate(testDate, "%h"), QString("Oct"));
0140     QCOMPARE(calendar->formatDate(testDate, "%A"), QString("Thursday"));
0141     QCOMPARE(calendar->formatDate(testDate, "%a"), QString("Thu"));
0142     QCOMPARE(calendar->formatDate(testDate, "%j"), QString("293"));
0143     QCOMPARE(calendar->formatDate(testDate, "%V"), QString("42"));
0144     QCOMPARE(calendar->formatDate(testDate, "%G"), QString("2005"));
0145     QCOMPARE(calendar->formatDate(testDate, "%g"), QString("05"));
0146     QCOMPARE(calendar->formatDate(testDate, "%u"), QString("4"));
0147     QCOMPARE(calendar->formatDate(testDate, "%D"), QString("10/20/05"));
0148     QCOMPARE(calendar->formatDate(testDate, "%F"), QString("2005-10-20"));
0149     QCOMPARE(calendar->formatDate(testDate, "%x"), QString("05-10-20"));
0150     QCOMPARE(calendar->formatDate(testDate, "%%"), QString("%"));
0151     QCOMPARE(calendar->formatDate(testDate, "%t"), QString("\t"));
0152 
0153     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-"),
0154              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-"));
0155 
0156     //Test the minimum widths are respected
0157     QCOMPARE(calendar->formatDate(testDate, "%1Y"),  QString("2005"));
0158     QCOMPARE(calendar->formatDate(testDate, "%1C"),  QString("20"));
0159     QCOMPARE(calendar->formatDate(testDate, "%1y"),  QString("05"));
0160     QCOMPARE(calendar->formatDate(testDate, "%1m"),  QString("10"));
0161     QCOMPARE(calendar->formatDate(testDate, "%1n"),  QString("10"));
0162     QCOMPARE(calendar->formatDate(testDate, "%1d"),  QString("20"));
0163     QCOMPARE(calendar->formatDate(testDate, "%1e"),  QString("20"));
0164     QCOMPARE(calendar->formatDate(testDate, "%01B"), QString("October"));
0165     QCOMPARE(calendar->formatDate(testDate, "%01b"), QString("Oct"));
0166     QCOMPARE(calendar->formatDate(testDate, "%01h"), QString("Oct"));
0167     QCOMPARE(calendar->formatDate(testDate, "%01A"), QString("Thursday"));
0168     QCOMPARE(calendar->formatDate(testDate, "%01a"), QString("Thu"));
0169     QCOMPARE(calendar->formatDate(testDate, "%1j"),  QString("293"));
0170     QCOMPARE(calendar->formatDate(testDate, "%1V"),  QString("42"));
0171     QCOMPARE(calendar->formatDate(testDate, "%1G"),  QString("2005"));
0172     QCOMPARE(calendar->formatDate(testDate, "%1g"),  QString("05"));
0173     QCOMPARE(calendar->formatDate(testDate, "%1u"),  QString("4"));
0174     QCOMPARE(calendar->formatDate(testDate, "%1D"),  QString("10/20/05"));
0175     QCOMPARE(calendar->formatDate(testDate, "%1F"),  QString("2005-10-20"));
0176     QCOMPARE(calendar->formatDate(testDate, "%1x"),  QString("05-10-20"));
0177     QCOMPARE(calendar->formatDate(testDate, "%1%"),  QString("%"));
0178     QCOMPARE(calendar->formatDate(testDate, "%1t"),  QString("\t"));
0179 
0180     testDate.setDate(2005, 1, 2);
0181 
0182     //Test various padding options with minimum width
0183     QCOMPARE(calendar->formatDate(testDate, "%"),     QString(""));
0184     QCOMPARE(calendar->formatDate(testDate, "%m"),    QString("01"));
0185     QCOMPARE(calendar->formatDate(testDate, "%n"),    QString("1"));
0186     QCOMPARE(calendar->formatDate(testDate, "%d"),    QString("02"));
0187     QCOMPARE(calendar->formatDate(testDate, "%e"),    QString("2"));
0188     QCOMPARE(calendar->formatDate(testDate, "%j"),    QString("002"));
0189     QCOMPARE(calendar->formatDate(testDate, "%_m"),   QString(" 1"));
0190     QCOMPARE(calendar->formatDate(testDate, "%_n"),   QString("1"));
0191     QCOMPARE(calendar->formatDate(testDate, "%_d"),   QString(" 2"));
0192     QCOMPARE(calendar->formatDate(testDate, "%_e"),   QString("2"));
0193     QCOMPARE(calendar->formatDate(testDate, "%_j"),   QString("  2"));
0194     QCOMPARE(calendar->formatDate(testDate, "%-m"),   QString("1"));
0195     QCOMPARE(calendar->formatDate(testDate, "%-n"),   QString("1"));
0196     QCOMPARE(calendar->formatDate(testDate, "%-d"),   QString("2"));
0197     QCOMPARE(calendar->formatDate(testDate, "%-e"),   QString("2"));
0198     QCOMPARE(calendar->formatDate(testDate, "%-j"),   QString("2"));
0199     QCOMPARE(calendar->formatDate(testDate, "%0m"),   QString("01"));
0200     QCOMPARE(calendar->formatDate(testDate, "%0n"),   QString("1"));
0201     QCOMPARE(calendar->formatDate(testDate, "%0d"),   QString("02"));
0202     QCOMPARE(calendar->formatDate(testDate, "%0e"),   QString("2"));
0203     QCOMPARE(calendar->formatDate(testDate, "%0j"),   QString("002"));
0204     QCOMPARE(calendar->formatDate(testDate, "%-_j"),  QString("  2"));
0205     QCOMPARE(calendar->formatDate(testDate, "%_-j"),  QString("2"));
0206     QCOMPARE(calendar->formatDate(testDate, "%-_0j"), QString("002"));
0207 
0208     //Test various padding options with width override
0209     QCOMPARE(calendar->formatDate(testDate, "%1m"),   QString("01"));
0210     QCOMPARE(calendar->formatDate(testDate, "%-05m"), QString("00001"));
0211     QCOMPARE(calendar->formatDate(testDate, "%10m"),  QString("0000000001"));
0212     QCOMPARE(calendar->formatDate(testDate, "%15m"),  QString("000000000000001"));
0213     QCOMPARE(calendar->formatDate(testDate, "%-5m"),  QString("1"));
0214     QCOMPARE(calendar->formatDate(testDate, "%_5m"),  QString("    1"));
0215     QCOMPARE(calendar->formatDate(testDate, "%_15m"), QString("              1"));
0216     QCOMPARE(calendar->formatDate(testDate, "%1B"),   QString("January"));
0217     QCOMPARE(calendar->formatDate(testDate, "%15B"),  QString("        January"));
0218     QCOMPARE(calendar->formatDate(testDate, "%-15B"), QString("January"));
0219     QCOMPARE(calendar->formatDate(testDate, "%_15B"), QString("        January"));
0220     QCOMPARE(calendar->formatDate(testDate, "%015B"), QString("00000000January"));
0221 
0222     //Test week number in previous year
0223     QCOMPARE(calendar->formatDate(testDate, "%V"), QString("53"));
0224     QCOMPARE(calendar->formatDate(testDate, "%G"), QString("2004"));
0225     QCOMPARE(calendar->formatDate(testDate, "%g"), QString("04"));
0226 
0227     //test case overrides
0228     QCOMPARE(calendar->formatDate(testDate, "%B"),  QString("January"));
0229     QCOMPARE(calendar->formatDate(testDate, "%^B"), QString("JANUARY"));
0230     QCOMPARE(calendar->formatDate(testDate, "%#B"), QString("JANUARY"));
0231     QCOMPARE(calendar->formatDate(testDate, "%m"),  QString("01"));
0232     QCOMPARE(calendar->formatDate(testDate, "%^m"), QString("01"));
0233     QCOMPARE(calendar->formatDate(testDate, "%#m"), QString("01"));
0234 
0235     //Test various year options
0236 
0237     testDate.setDate(789, 1, 1);
0238     QCOMPARE(calendar->formatDate(testDate, "%Y"),  QString("0789"));
0239     QCOMPARE(calendar->formatDate(testDate, "%C"),  QString("07"));
0240     QCOMPARE(calendar->formatDate(testDate, "%y"),  QString("89"));
0241     QCOMPARE(calendar->formatDate(testDate, "%-Y"), QString("789"));
0242     QCOMPARE(calendar->formatDate(testDate, "%-C"), QString("7"));
0243     QCOMPARE(calendar->formatDate(testDate, "%-y"), QString("89"));
0244     QCOMPARE(calendar->formatDate(testDate, "%_Y"), QString(" 789"));
0245     QCOMPARE(calendar->formatDate(testDate, "%_C"), QString(" 7"));
0246     QCOMPARE(calendar->formatDate(testDate, "%_y"), QString("89"));
0247     QCOMPARE(calendar->formatDate(testDate, "%0Y"), QString("0789"));
0248     QCOMPARE(calendar->formatDate(testDate, "%0C"), QString("07"));
0249     QCOMPARE(calendar->formatDate(testDate, "%0y"), QString("89"));
0250     QCOMPARE(calendar->formatDate(testDate, "%EC"), QString("AD"));
0251     QCOMPARE(calendar->formatDate(testDate, "%Ey"), QString("789"));
0252     QCOMPARE(calendar->formatDate(testDate, "%EY"), QString("789 AD"));
0253 
0254     testDate.setDate(709, 1, 1);
0255     QCOMPARE(calendar->formatDate(testDate, "%Y"),  QString("0709"));
0256     QCOMPARE(calendar->formatDate(testDate, "%C"),  QString("07"));
0257     QCOMPARE(calendar->formatDate(testDate, "%y"),  QString("09"));
0258     QCOMPARE(calendar->formatDate(testDate, "%-Y"), QString("709"));
0259     QCOMPARE(calendar->formatDate(testDate, "%-C"), QString("7"));
0260     QCOMPARE(calendar->formatDate(testDate, "%-y"), QString("9"));
0261     QCOMPARE(calendar->formatDate(testDate, "%_Y"), QString(" 709"));
0262     QCOMPARE(calendar->formatDate(testDate, "%_C"), QString(" 7"));
0263     QCOMPARE(calendar->formatDate(testDate, "%_y"), QString(" 9"));
0264     QCOMPARE(calendar->formatDate(testDate, "%0Y"), QString("0709"));
0265     QCOMPARE(calendar->formatDate(testDate, "%0C"), QString("07"));
0266     QCOMPARE(calendar->formatDate(testDate, "%0y"), QString("09"));
0267     QCOMPARE(calendar->formatDate(testDate, "%EC"), QString("AD"));
0268     QCOMPARE(calendar->formatDate(testDate, "%Ey"), QString("709"));
0269     QCOMPARE(calendar->formatDate(testDate, "%EY"), QString("709 AD"));
0270 
0271     testDate.setDate(89, 1, 1);
0272     QCOMPARE(calendar->formatDate(testDate, "%Y"),  QString("0089"));
0273     QCOMPARE(calendar->formatDate(testDate, "%C"),  QString("00"));
0274     QCOMPARE(calendar->formatDate(testDate, "%y"),  QString("89"));
0275     QCOMPARE(calendar->formatDate(testDate, "%-Y"), QString("89"));
0276     QCOMPARE(calendar->formatDate(testDate, "%-C"), QString("0"));
0277     QCOMPARE(calendar->formatDate(testDate, "%-y"), QString("89"));
0278     QCOMPARE(calendar->formatDate(testDate, "%_Y"), QString("  89"));
0279     QCOMPARE(calendar->formatDate(testDate, "%_C"), QString(" 0"));
0280     QCOMPARE(calendar->formatDate(testDate, "%_y"), QString("89"));
0281     QCOMPARE(calendar->formatDate(testDate, "%0Y"), QString("0089"));
0282     QCOMPARE(calendar->formatDate(testDate, "%0C"), QString("00"));
0283     QCOMPARE(calendar->formatDate(testDate, "%0y"), QString("89"));
0284     QCOMPARE(calendar->formatDate(testDate, "%EC"), QString("AD"));
0285     QCOMPARE(calendar->formatDate(testDate, "%Ey"), QString("89"));
0286     QCOMPARE(calendar->formatDate(testDate, "%EY"), QString("89 AD"));
0287 
0288     testDate.setDate(9, 1, 1);
0289     QCOMPARE(calendar->formatDate(testDate, "%Y"),  QString("0009"));
0290     QCOMPARE(calendar->formatDate(testDate, "%C"),  QString("00"));
0291     QCOMPARE(calendar->formatDate(testDate, "%y"),  QString("09"));
0292     QCOMPARE(calendar->formatDate(testDate, "%-Y"), QString("9"));
0293     QCOMPARE(calendar->formatDate(testDate, "%-C"), QString("0"));
0294     QCOMPARE(calendar->formatDate(testDate, "%-y"), QString("9"));
0295     QCOMPARE(calendar->formatDate(testDate, "%_Y"), QString("   9"));
0296     QCOMPARE(calendar->formatDate(testDate, "%_C"), QString(" 0"));
0297     QCOMPARE(calendar->formatDate(testDate, "%_y"), QString(" 9"));
0298     QCOMPARE(calendar->formatDate(testDate, "%0Y"), QString("0009"));
0299     QCOMPARE(calendar->formatDate(testDate, "%0C"), QString("00"));
0300     QCOMPARE(calendar->formatDate(testDate, "%0y"), QString("09"));
0301     QCOMPARE(calendar->formatDate(testDate, "%EC"), QString("AD"));
0302     QCOMPARE(calendar->formatDate(testDate, "%Ey"), QString("9"));
0303     QCOMPARE(calendar->formatDate(testDate, "%EY"), QString("9 AD"));
0304 
0305     testDate.setDate(-9, 1, 1);
0306     QCOMPARE(calendar->formatDate(testDate, "%Y"),  QString("-0009"));
0307     QCOMPARE(calendar->formatDate(testDate, "%C"),  QString("-00"));
0308     QCOMPARE(calendar->formatDate(testDate, "%y"),  QString("-09"));
0309     QCOMPARE(calendar->formatDate(testDate, "%-Y"), QString("-9"));
0310     QCOMPARE(calendar->formatDate(testDate, "%-C"), QString("-0"));
0311     QCOMPARE(calendar->formatDate(testDate, "%-y"), QString("-9"));
0312     QCOMPARE(calendar->formatDate(testDate, "%_Y"), QString("  -9"));
0313     QCOMPARE(calendar->formatDate(testDate, "%_C"), QString("-0"));
0314     QCOMPARE(calendar->formatDate(testDate, "%_y"), QString("-9"));
0315     QCOMPARE(calendar->formatDate(testDate, "%0Y"), QString("-0009"));
0316     QCOMPARE(calendar->formatDate(testDate, "%0C"), QString("-00"));
0317     QCOMPARE(calendar->formatDate(testDate, "%0y"), QString("-09"));
0318     QCOMPARE(calendar->formatDate(testDate, "%EC"), QString("BC"));
0319     QCOMPARE(calendar->formatDate(testDate, "%Ey"), QString("9"));
0320     QCOMPARE(calendar->formatDate(testDate, "%EY"), QString("9 BC"));
0321 
0322     testDate.setDate(-89, 1, 1);
0323     QCOMPARE(calendar->formatDate(testDate, "%Y"),  QString("-0089"));
0324     QCOMPARE(calendar->formatDate(testDate, "%C"),  QString("-00"));
0325     QCOMPARE(calendar->formatDate(testDate, "%y"),  QString("-89"));
0326     QCOMPARE(calendar->formatDate(testDate, "%-Y"), QString("-89"));
0327     QCOMPARE(calendar->formatDate(testDate, "%-C"), QString("-0"));
0328     QCOMPARE(calendar->formatDate(testDate, "%-y"), QString("-89"));
0329     QCOMPARE(calendar->formatDate(testDate, "%_Y"), QString(" -89"));
0330     QCOMPARE(calendar->formatDate(testDate, "%_C"), QString("-0"));
0331     QCOMPARE(calendar->formatDate(testDate, "%_y"), QString("-89"));
0332     QCOMPARE(calendar->formatDate(testDate, "%0Y"), QString("-0089"));
0333     QCOMPARE(calendar->formatDate(testDate, "%0C"), QString("-00"));
0334     QCOMPARE(calendar->formatDate(testDate, "%0y"), QString("-89"));
0335     QCOMPARE(calendar->formatDate(testDate, "%EC"), QString("BC"));
0336     QCOMPARE(calendar->formatDate(testDate, "%Ey"), QString("89"));
0337     QCOMPARE(calendar->formatDate(testDate, "%EY"), QString("89 BC"));
0338 
0339     testDate.setDate(-789, 1, 1);
0340     QCOMPARE(calendar->formatDate(testDate, "%Y"),  QString("-0789"));
0341     QCOMPARE(calendar->formatDate(testDate, "%C"),  QString("-07"));
0342     QCOMPARE(calendar->formatDate(testDate, "%y"),  QString("-89"));
0343     QCOMPARE(calendar->formatDate(testDate, "%-Y"), QString("-789"));
0344     QCOMPARE(calendar->formatDate(testDate, "%-C"), QString("-7"));
0345     QCOMPARE(calendar->formatDate(testDate, "%-y"), QString("-89"));
0346     QCOMPARE(calendar->formatDate(testDate, "%_Y"), QString("-789"));
0347     QCOMPARE(calendar->formatDate(testDate, "%_C"), QString("-7"));
0348     QCOMPARE(calendar->formatDate(testDate, "%_y"), QString("-89"));
0349     QCOMPARE(calendar->formatDate(testDate, "%0Y"), QString("-0789"));
0350     QCOMPARE(calendar->formatDate(testDate, "%0C"), QString("-07"));
0351     QCOMPARE(calendar->formatDate(testDate, "%0y"), QString("-89"));
0352     QCOMPARE(calendar->formatDate(testDate, "%EC"), QString("BC"));
0353     QCOMPARE(calendar->formatDate(testDate, "%Ey"), QString("789"));
0354     QCOMPARE(calendar->formatDate(testDate, "%EY"), QString("789 BC"));
0355 
0356     testDate.setDate(-709, 1, 1);
0357     QCOMPARE(calendar->formatDate(testDate, "%Y"),  QString("-0709"));
0358     QCOMPARE(calendar->formatDate(testDate, "%C"),  QString("-07"));
0359     QCOMPARE(calendar->formatDate(testDate, "%y"),  QString("-09"));
0360     QCOMPARE(calendar->formatDate(testDate, "%-Y"), QString("-709"));
0361     QCOMPARE(calendar->formatDate(testDate, "%-C"), QString("-7"));
0362     QCOMPARE(calendar->formatDate(testDate, "%-y"), QString("-9"));
0363     QCOMPARE(calendar->formatDate(testDate, "%_Y"), QString("-709"));
0364     QCOMPARE(calendar->formatDate(testDate, "%_C"), QString("-7"));
0365     QCOMPARE(calendar->formatDate(testDate, "%_y"), QString("-9"));
0366     QCOMPARE(calendar->formatDate(testDate, "%0Y"), QString("-0709"));
0367     QCOMPARE(calendar->formatDate(testDate, "%0C"), QString("-07"));
0368     QCOMPARE(calendar->formatDate(testDate, "%0y"), QString("-09"));
0369     QCOMPARE(calendar->formatDate(testDate, "%EC"), QString("BC"));
0370     QCOMPARE(calendar->formatDate(testDate, "%Ey"), QString("709"));
0371     QCOMPARE(calendar->formatDate(testDate, "%EY"), QString("709 BC"));
0372 
0373     testDate.setDate(-1789, 1, 1);
0374     QCOMPARE(calendar->formatDate(testDate, "%Y"),  QString("-1789"));
0375     QCOMPARE(calendar->formatDate(testDate, "%C"),  QString("-17"));
0376     QCOMPARE(calendar->formatDate(testDate, "%y"),  QString("-89"));
0377     QCOMPARE(calendar->formatDate(testDate, "%-Y"), QString("-1789"));
0378     QCOMPARE(calendar->formatDate(testDate, "%-C"), QString("-17"));
0379     QCOMPARE(calendar->formatDate(testDate, "%-y"), QString("-89"));
0380     QCOMPARE(calendar->formatDate(testDate, "%_Y"), QString("-1789"));
0381     QCOMPARE(calendar->formatDate(testDate, "%_C"), QString("-17"));
0382     QCOMPARE(calendar->formatDate(testDate, "%_y"), QString("-89"));
0383     QCOMPARE(calendar->formatDate(testDate, "%0Y"), QString("-1789"));
0384     QCOMPARE(calendar->formatDate(testDate, "%0C"), QString("-17"));
0385     QCOMPARE(calendar->formatDate(testDate, "%0y"), QString("-89"));
0386     QCOMPARE(calendar->formatDate(testDate, "%EC"), QString("BC"));
0387     QCOMPARE(calendar->formatDate(testDate, "%Ey"), QString("1789"));
0388     QCOMPARE(calendar->formatDate(testDate, "%EY"), QString("1789 BC"));
0389 
0390     testDate.setDate(-1709, 1, 1);
0391     QCOMPARE(calendar->formatDate(testDate, "%Y"),  QString("-1709"));
0392     QCOMPARE(calendar->formatDate(testDate, "%C"),  QString("-17"));
0393     QCOMPARE(calendar->formatDate(testDate, "%y"),  QString("-09"));
0394     QCOMPARE(calendar->formatDate(testDate, "%-Y"), QString("-1709"));
0395     QCOMPARE(calendar->formatDate(testDate, "%-C"), QString("-17"));
0396     QCOMPARE(calendar->formatDate(testDate, "%-y"), QString("-9"));
0397     QCOMPARE(calendar->formatDate(testDate, "%_Y"), QString("-1709"));
0398     QCOMPARE(calendar->formatDate(testDate, "%_C"), QString("-17"));
0399     QCOMPARE(calendar->formatDate(testDate, "%_y"), QString("-9"));
0400     QCOMPARE(calendar->formatDate(testDate, "%0Y"), QString("-1709"));
0401     QCOMPARE(calendar->formatDate(testDate, "%0C"), QString("-17"));
0402     QCOMPARE(calendar->formatDate(testDate, "%0y"), QString("-09"));
0403     QCOMPARE(calendar->formatDate(testDate, "%EC"), QString("BC"));
0404     QCOMPARE(calendar->formatDate(testDate, "%Ey"), QString("1709"));
0405     QCOMPARE(calendar->formatDate(testDate, "%EY"), QString("1709 BC"));
0406 
0407     // Test POSIX format
0408     testDate.setDate(2010, 1, 2);
0409     QCOMPARE(calendar->formatDate(testDate, "%Y-%n-%d", KLocale::KdeFormat),   QString("2010-1-02"));
0410     QCOMPARE(calendar->formatDate(testDate, "%Y-%n-%d", KLocale::PosixFormat), QString("2010-\n-02"));
0411     QCOMPARE(calendar->formatDate(testDate, "%e", KLocale::KdeFormat),   QString("2"));
0412     QCOMPARE(calendar->formatDate(testDate, "%e", KLocale::PosixFormat), QString(" 2"));
0413 
0414     delete calendar;
0415     delete locale;
0416 }
0417 
0418 void KCalendarTest::testFormatUnicode()
0419 {
0420     KLocale *locale = new KLocale(*KLocale::global());
0421     locale->setDateFormatShort("%y-%m-%d");
0422     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar, locale);
0423 
0424     QDate testDate(2005, 10, 20);
0425     compareFormatUnicode(calendar, testDate, "yyyy");
0426     compareFormatUnicode(calendar, testDate, "yy");
0427     compareFormatUnicode(calendar, testDate, "MMMM");
0428     compareFormatUnicode(calendar, testDate, "MMM");
0429     compareFormatUnicode(calendar, testDate, "MM");
0430     compareFormatUnicode(calendar, testDate, "M");
0431     compareFormatUnicode(calendar, testDate, "dddd");
0432     compareFormatUnicode(calendar, testDate, "ddd");
0433     compareFormatUnicode(calendar, testDate, "dd");
0434     compareFormatUnicode(calendar, testDate, "d");
0435     compareFormatUnicode(calendar, testDate, "yyyy-MM-dd");
0436 
0437     testDate.setDate(-2005, 10, 20);
0438     compareFormatUnicode(calendar, testDate, "yyyy");
0439     compareFormatUnicode(calendar, testDate, "yy");
0440     compareFormatUnicode(calendar, testDate, "MMMM");
0441     compareFormatUnicode(calendar, testDate, "MMM");
0442     compareFormatUnicode(calendar, testDate, "MM");
0443     compareFormatUnicode(calendar, testDate, "M");
0444     compareFormatUnicode(calendar, testDate, "dddd");
0445     compareFormatUnicode(calendar, testDate, "ddd");
0446     compareFormatUnicode(calendar, testDate, "dd");
0447     compareFormatUnicode(calendar, testDate, "d");
0448     compareFormatUnicode(calendar, testDate, "yyyy-MM-dd");
0449 }
0450 
0451 void KCalendarTest::compareFormatUnicode(const KCalendarSystem *calendar, const QDate &testDate, const QString &testFormat)
0452 {
0453     QCOMPARE(calendar->formatDate(testDate, testFormat, KLocale::UnicodeFormat), testDate.toString(testFormat));
0454 }
0455 
0456 void KCalendarTest::testReadDate()
0457 {
0458     KLocale *locale = new KLocale(*KLocale::global());
0459     locale->setDateFormatShort("%y-%m-%d");
0460     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar, locale);
0461     int currentYear = QDate::currentDate().year();
0462 
0463     //Test default settings
0464     QDate testDate(2005, 10, 20);
0465 
0466     //Test Month and Day
0467     QCOMPARE(calendar->readDate("2005-01-01",  "%Y-%m-%d"), QDate(2005, 1, 1));
0468     QCOMPARE(calendar->readDate("-2005-01-01", "%Y-%m-%d"), QDate(-2005, 1, 1));
0469     QCOMPARE(calendar->readDate("05-01-01",    "%y-%m-%d"), QDate(2005, 1, 1));
0470     QCOMPARE(calendar->readDate("01-01",       "%m-%d"),    QDate(currentYear, 1, 1));
0471 
0472     //Test Era and Year In Era
0473     QCOMPARE(calendar->readDate("1 1 2005 AD", "%d %m %Ey %EC"), QDate(2005, 1, 1));
0474     QCOMPARE(calendar->readDate("1 1 2005 Anno Domini", "%d %m %Ey %EC"), QDate(2005, 1, 1));
0475     QCOMPARE(calendar->readDate("1 1 2005 BC", "%d %m %Ey %EC"), QDate(-2005, 1, 1));
0476     QCOMPARE(calendar->readDate("1 1 2005 Before Christ", "%d %m %Ey %EC"), QDate(-2005, 1, 1));
0477     QCOMPARE(calendar->readDate("1 1 2005 AD", "%d %m %EY"), QDate(2005, 1, 1));
0478     QCOMPARE(calendar->readDate("1 1 2005 Anno Domini", "%d %m %EY"), QDate(2005, 1, 1));
0479     QCOMPARE(calendar->readDate("1 1 2005 BC", "%d %m %EY"), QDate(-2005, 1, 1));
0480     QCOMPARE(calendar->readDate("1 1 2005 Before Christ", "%d %m %EY"), QDate(-2005, 1, 1));
0481     QCOMPARE(calendar->readDate("1 2005 AD January", "%d %Ey %EC %B"), QDate(2005, 1, 1));
0482     QCOMPARE(calendar->readDate("1 2005 AD January", "%d %EY %B"), QDate(2005, 1, 1));
0483     QCOMPARE(calendar->readDate("2005 AD January 1", "%Ey %EC %B %d"), QDate(2005, 1, 1));
0484     QCOMPARE(calendar->readDate("2005 AD January 1", "%EY %B %d"), QDate(2005, 1, 1));
0485 
0486     //Test Day-of-Year
0487     QCOMPARE(calendar->readDate("2005-001", "%Y-%j"), QDate(2005, 1, 1));
0488     QCOMPARE(calendar->readDate("2005-1",   "%Y-%j"), QDate(2005, 1, 1));
0489     QCOMPARE(calendar->readDate("001",      "%j"),    QDate(currentYear, 1, 1));
0490     QCOMPARE(calendar->readDate("2005-365", "%Y-%j"), QDate(2005, 12, 31));
0491     QCOMPARE(calendar->readDate("2000-366", "%Y-%j"), QDate(2000, 12, 31));
0492 
0493     QCOMPARE(calendar->readDate("2005-000", "%Y-%j"), QDate());
0494     QCOMPARE(calendar->readDate("2005-366", "%Y-%j"), QDate());
0495     QCOMPARE(calendar->readDate("2000-367", "%Y-%j"), QDate());
0496 
0497     //Test Weekday and Month names
0498     QCOMPARE(calendar->readDate("Monday 2004-01-05", "%A %Y-%m-%d"), QDate(2004,  1,  5));
0499     QCOMPARE(calendar->readDate("Mon 2004-01-05",    "%A %Y-%m-%d"), QDate(2004,  1,  5));
0500     QCOMPARE(calendar->readDate("Monday 2004-01-05", "%a %Y-%m-%d"), QDate(2004,  1,  5));
0501     QCOMPARE(calendar->readDate("Mon 2004-01-05",    "%a %Y-%m-%d"), QDate(2004,  1,  5));
0502 
0503     QCOMPARE(calendar->readDate("Sunday 2004-01-11", "%A %Y-%m-%d"), QDate(2004,  1, 11));
0504     QCOMPARE(calendar->readDate("Sun 2004-01-11",    "%A %Y-%m-%d"), QDate(2004,  1, 11));
0505     QCOMPARE(calendar->readDate("Sunday 2004-01-11", "%a %Y-%m-%d"), QDate(2004,  1, 11));
0506     QCOMPARE(calendar->readDate("Sun 2004-01-11",    "%a %Y-%m-%d"), QDate(2004,  1, 11));
0507 
0508     QCOMPARE(calendar->readDate("2004 January 01",  "%Y %B %d"), QDate(2004,  1,  1));
0509     QCOMPARE(calendar->readDate("2004 Jan 01",      "%Y %B %d"), QDate(2004,  1,  1));
0510     QCOMPARE(calendar->readDate("2004 January 01",  "%Y %b %d"), QDate(2004,  1,  1));
0511     QCOMPARE(calendar->readDate("2004 Jan 01",      "%Y %b %d"), QDate(2004,  1,  1));
0512 
0513     QCOMPARE(calendar->readDate("2004 December 01", "%Y %B %d"), QDate(2004, 12,  1));
0514     QCOMPARE(calendar->readDate("2004 Dec 01",      "%Y %B %d"), QDate(2004, 12,  1));
0515     QCOMPARE(calendar->readDate("2004 December 01", "%Y %b %d"), QDate(2004, 12,  1));
0516     QCOMPARE(calendar->readDate("2004 Dec 01",      "%Y %b %d"), QDate(2004, 12,  1));
0517 
0518     QCOMPARE(calendar->readDate("Wednesday 2004 December 01", "%A %Y %B %d"), QDate(2004, 12,  1));
0519     QCOMPARE(calendar->readDate("Wed 2004 Dec 01",            "%A %Y %B %d"), QDate(2004, 12,  1));
0520     QCOMPARE(calendar->readDate("Wednesday 2004 December 01", "%a %Y %b %d"), QDate(2004, 12,  1));
0521     QCOMPARE(calendar->readDate("Wed 2004 Dec 01",            "%a %Y %b %d"), QDate(2004, 12,  1));
0522 
0523     //Test ISO Week and Day-of-Week
0524     QCOMPARE(calendar->readDate("2004-W01-1", "%Y-W%V-%u"), QDate(2003, 12, 29));
0525     QCOMPARE(calendar->readDate("2004-W01-2", "%Y-W%V-%u"), QDate(2003, 12, 30));
0526     QCOMPARE(calendar->readDate("2004-W01-3", "%Y-W%V-%u"), QDate(2003, 12, 31));
0527     QCOMPARE(calendar->readDate("2004-W01-4", "%Y-W%V-%u"), QDate(2004, 1, 1));
0528     QCOMPARE(calendar->readDate("2004-W01-5", "%Y-W%V-%u"), QDate(2004, 1, 2));
0529     QCOMPARE(calendar->readDate("2004-W01-6", "%Y-W%V-%u"), QDate(2004, 1, 3));
0530     QCOMPARE(calendar->readDate("2004-W01-7", "%Y-W%V-%u"), QDate(2004, 1, 4));
0531 
0532     QCOMPARE(calendar->readDate("2004-W24-1", "%Y-W%V-%u"), QDate(2004, 6, 7));
0533     QCOMPARE(calendar->readDate("2004-W24-2", "%Y-W%V-%u"), QDate(2004, 6, 8));
0534     QCOMPARE(calendar->readDate("2004-W24-3", "%Y-W%V-%u"), QDate(2004, 6, 9));
0535     QCOMPARE(calendar->readDate("2004-W24-4", "%Y-W%V-%u"), QDate(2004, 6, 10));
0536     QCOMPARE(calendar->readDate("2004-W24-5", "%Y-W%V-%u"), QDate(2004, 6, 11));
0537     QCOMPARE(calendar->readDate("2004-W24-6", "%Y-W%V-%u"), QDate(2004, 6, 12));
0538     QCOMPARE(calendar->readDate("2004-W24-7", "%Y-W%V-%u"), QDate(2004, 6, 13));
0539 
0540     QCOMPARE(calendar->readDate("2004-W53-1", "%Y-W%V-%u"), QDate(2004, 12, 27));
0541     QCOMPARE(calendar->readDate("2004-W53-2", "%Y-W%V-%u"), QDate(2004, 12, 28));
0542     QCOMPARE(calendar->readDate("2004-W53-3", "%Y-W%V-%u"), QDate(2004, 12, 29));
0543     QCOMPARE(calendar->readDate("2004-W53-4", "%Y-W%V-%u"), QDate(2004, 12, 30));
0544     QCOMPARE(calendar->readDate("2004-W53-5", "%Y-W%V-%u"), QDate(2004, 12, 31));
0545     QCOMPARE(calendar->readDate("2004-W53-6", "%Y-W%V-%u"), QDate(2005, 1, 1));
0546     QCOMPARE(calendar->readDate("2004-W53-7", "%Y-W%V-%u"), QDate(2005, 1, 2));
0547 
0548     calendar->setDateIsoWeek(testDate, currentYear, 46, 5);
0549     QCOMPARE(calendar->readDate("W46-5", "W%V-%u"), testDate);
0550 
0551     QCOMPARE(calendar->readDate("2004-W00-1", "%Y-W%V-%u"),   QDate());
0552     QCOMPARE(calendar->readDate("2004-W01-0", "%Y-W%V-%u"),   QDate());
0553     QCOMPARE(calendar->readDate("2004-W01-8", "%Y-W%V-%u"),   QDate());
0554     QCOMPARE(calendar->readDate("2004-W54-1", "%Y-W%V-%u"),   QDate());
0555     QCOMPARE(calendar->readDate("2004-W53-01", "%Y-W%V-%u"),  QDate());
0556     QCOMPARE(calendar->readDate("2004-W053-01", "%Y-W%V-%u"), QDate());
0557 
0558     //Test all 3 options, should go with most accurate
0559     QCOMPARE(calendar->readDate("2004-01-01-365-W52-1", "%Y-%m-%d-%j-W%V-%u"), QDate(2004, 1, 1));
0560     QCOMPARE(calendar->readDate("2004-001-W52-1",       "%Y-%j-W%V-%u"),       QDate(2004, 1, 1));
0561 
0562     //Test spaces and literals
0563     QCOMPARE(calendar->readDate("2004- 1-1",    "%Y-%m-%d"), QDate(2004, 1, 1));
0564     QCOMPARE(calendar->readDate("2004-1-1",     "%Y-%m-%e"), QDate(2004, 1, 1));
0565     QCOMPARE(calendar->readDate("2004 01 01",   "%Y %m %d"), QDate(2004, 1, 1));
0566     QCOMPARE(calendar->readDate("2004  01  01", "%Y %m %d"), QDate(2004, 1, 1));
0567     QCOMPARE(calendar->readDate("2004W01-1",   "%YW%m-%d"),  QDate(2004, 1, 1));
0568     QCOMPARE(calendar->readDate("2004w01-1",   "%YW%m-%d"),  QDate(2004, 1, 1));
0569     QCOMPARE(calendar->readDate("2004W01-1",   "%Yw%m-%d"),  QDate(2004, 1, 1));
0570 
0571     //Test enums and non-strict version of readDate()
0572     locale->setDateFormat("%d/%m/%Y");
0573     locale->setDateFormatShort("%Y/%m/%d");
0574     QCOMPARE(calendar->readDate("01/02/2004"),  QDate(2004, 2, 1));        //KLocale::NormalFormat
0575     QCOMPARE(calendar->readDate("01/02/-2004"), QDate(-2004, 2, 1));       //KLocale::NormalFormat
0576     QCOMPARE(calendar->readDate("2004/02/01"),  QDate(2004, 2, 1));        //KLocale::ShortFormat
0577     QCOMPARE(calendar->readDate("-2004/02/01"), QDate(-2004, 2, 1));       //KLocale::ShortFormat
0578     QCOMPARE(calendar->readDate("2004-02-01"),  QDate(2004, 2, 1));        //KLocale::IsoFormat
0579     QCOMPARE(calendar->readDate("-2004-02-01"), QDate(-2004, 2, 1));       //KLocale::IsoFormat
0580     QCOMPARE(calendar->readDate("2004-W02-1"),  QDate(2004, 1, 5));        //KLocale::IsoWeekFormat
0581     QCOMPARE(calendar->readDate("2004-001"),    QDate(2004, 1, 1));        //KLocale::IsoOrdinalFormat
0582     QCOMPARE(calendar->readDate("-2004-001"),   QDate(-2004, 1, 1));       //KLocale::IsoOrdinalFormat
0583 
0584     //Test enums and strict version of readDate()
0585     QCOMPARE(calendar->readDate("01/02/2004",   KLocale::NormalFormat),     QDate(2004, 2, 1));
0586     QCOMPARE(calendar->readDate("01/02/-2004",  KLocale::NormalFormat),     QDate(-2004, 2, 1));
0587     QCOMPARE(calendar->readDate("2004/02/01",   KLocale::ShortFormat),      QDate(2004, 2, 1));
0588     QCOMPARE(calendar->readDate("-2004/02/01",  KLocale::ShortFormat),      QDate(-2004, 2, 1));
0589     QCOMPARE(calendar->readDate("2004-02-01",   KLocale::IsoFormat),        QDate(2004, 2, 1));
0590     QCOMPARE(calendar->readDate("-2004-02-01",  KLocale::IsoFormat),        QDate(-2004, 2, 1));
0591     QCOMPARE(calendar->readDate("2004-W02-1",   KLocale::IsoWeekFormat),    QDate(2004, 1, 5));
0592     QCOMPARE(calendar->readDate("2004-001",     KLocale::IsoOrdinalFormat), QDate(2004, 1, 1));
0593     QCOMPARE(calendar->readDate("-2004-001",    KLocale::IsoOrdinalFormat), QDate(-2004, 1, 1));
0594 
0595     //Test ISO standard date form with no spaces
0596     locale->setDateFormatShort("%Y%m%d");
0597     QCOMPARE(calendar->readDate("20040201"),  QDate(2004, 2, 1));
0598     QCOMPARE(calendar->readDate("-20040201"), QDate(-2004, 2, 1));
0599 
0600     // Test POSIX format
0601     testDate.setDate(2010, 1, 2);
0602     QCOMPARE(calendar->readDate("2010-1-2", "%Y-%n-%d", nullptr, KLocale::KdeFormat),       QDate(2010, 1, 2));
0603     QCOMPARE(calendar->readDate("2010-\n-2",  "%Y-%n-%d", nullptr, KLocale::PosixFormat),   QDate());
0604     QCOMPARE(calendar->readDate("2010-1\n-2", "%Y-%n\n-%d", nullptr, KLocale::KdeFormat),   QDate(2010, 1, 2));
0605     QCOMPARE(calendar->readDate("2010-1\n-2", "%Y-%m%n-%d", nullptr, KLocale::PosixFormat), QDate(2010, 1, 2));
0606     QCOMPARE(calendar->readDate("2010-1-2", "%Y-%m-%e", nullptr, KLocale::KdeFormat),       QDate(2010, 1, 2));
0607     QCOMPARE(calendar->readDate("2010-1-2", "%Y-%m-%e", nullptr, KLocale::PosixFormat),     QDate(2010, 1, 2));
0608 
0609     delete calendar;
0610     delete locale;
0611 }
0612 
0613 void KCalendarTest::testStringForms()
0614 {
0615     testStrings(KLocale::ArabicDigits);
0616     testStrings(KLocale::ArabicIndicDigits);
0617 }
0618 
0619 void KCalendarTest::testStrings(KLocale::DigitSet testDigitSet)
0620 {
0621     KLocale *locale = new KLocale(*KLocale::global());
0622     locale->setDigitSet(testDigitSet);
0623     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar, locale);
0624 
0625     //Test default settings
0626     QDate testDate(2005, 1, 3);
0627 
0628     //Test Year String
0629     QCOMPARE(calendar->yearString(testDate), locale->convertDigits("2005", locale->dateTimeDigitSet()));
0630     QCOMPARE(calendar->yearString(testDate, KCalendarSystem::LongFormat), locale->convertDigits("2005", locale->dateTimeDigitSet()));
0631     QCOMPARE(calendar->yearString(testDate, KCalendarSystem::ShortFormat), locale->convertDigits("05", locale->dateTimeDigitSet()));
0632 
0633     //Test Month String
0634     QCOMPARE(calendar->monthString(testDate), locale->convertDigits("01", locale->dateTimeDigitSet()));
0635     QCOMPARE(calendar->monthString(testDate, KCalendarSystem::LongFormat), locale->convertDigits("01", locale->dateTimeDigitSet()));
0636     QCOMPARE(calendar->monthString(testDate, KCalendarSystem::ShortFormat), locale->convertDigits("1", locale->dateTimeDigitSet()));
0637 
0638     //Test Day String
0639     QCOMPARE(calendar->dayString(testDate), locale->convertDigits("03", locale->dateTimeDigitSet()));
0640     QCOMPARE(calendar->dayString(testDate, KCalendarSystem::LongFormat), locale->convertDigits("03", locale->dateTimeDigitSet()));
0641     QCOMPARE(calendar->dayString(testDate, KCalendarSystem::ShortFormat), locale->convertDigits("3", locale->dateTimeDigitSet()));
0642 
0643     //Test Day of Year String
0644     QCOMPARE(calendar->dayOfYearString(testDate), locale->convertDigits("003", locale->dateTimeDigitSet()));
0645     QCOMPARE(calendar->dayOfYearString(testDate, KCalendarSystem::LongFormat), locale->convertDigits("003", locale->dateTimeDigitSet()));
0646     QCOMPARE(calendar->dayOfYearString(testDate, KCalendarSystem::ShortFormat), locale->convertDigits("3", locale->dateTimeDigitSet()));
0647 
0648     //Test Day of Week String
0649     QCOMPARE(calendar->dayOfWeekString(testDate), locale->convertDigits("1", locale->dateTimeDigitSet()));
0650 
0651     //Test Week Number String
0652     QCOMPARE(calendar->weekNumberString(testDate), locale->convertDigits("01", locale->dateTimeDigitSet()));
0653     QCOMPARE(calendar->weekNumberString(testDate, KCalendarSystem::LongFormat), locale->convertDigits("01", locale->dateTimeDigitSet()));
0654     QCOMPARE(calendar->weekNumberString(testDate, KCalendarSystem::ShortFormat), locale->convertDigits("1", locale->dateTimeDigitSet()));
0655 
0656     //Test Months In Year String
0657     QCOMPARE(calendar->monthsInYearString(testDate), locale->convertDigits("12", locale->dateTimeDigitSet()));
0658     QCOMPARE(calendar->monthsInYearString(testDate, KCalendarSystem::LongFormat), locale->convertDigits("12", locale->dateTimeDigitSet()));
0659     QCOMPARE(calendar->monthsInYearString(testDate, KCalendarSystem::ShortFormat), locale->convertDigits("12", locale->dateTimeDigitSet()));
0660 
0661     //Test Weeks In Year String
0662     QCOMPARE(calendar->weeksInYearString(testDate), locale->convertDigits("52", locale->dateTimeDigitSet()));
0663     QCOMPARE(calendar->weeksInYearString(testDate, KCalendarSystem::LongFormat), locale->convertDigits("52", locale->dateTimeDigitSet()));
0664     QCOMPARE(calendar->weeksInYearString(testDate, KCalendarSystem::ShortFormat), locale->convertDigits("52", locale->dateTimeDigitSet()));
0665 
0666     //Test Days In Year String
0667     QCOMPARE(calendar->daysInYearString(testDate), locale->convertDigits("365", locale->dateTimeDigitSet()));
0668     QCOMPARE(calendar->daysInYearString(testDate, KCalendarSystem::LongFormat), locale->convertDigits("365", locale->dateTimeDigitSet()));
0669     QCOMPARE(calendar->daysInYearString(testDate, KCalendarSystem::ShortFormat), locale->convertDigits("365", locale->dateTimeDigitSet()));
0670 
0671     //Test Days In Month String
0672     QCOMPARE(calendar->daysInMonthString(testDate), locale->convertDigits("31", locale->dateTimeDigitSet()));
0673     QCOMPARE(calendar->daysInMonthString(testDate, KCalendarSystem::LongFormat), locale->convertDigits("31", locale->dateTimeDigitSet()));
0674     QCOMPARE(calendar->daysInMonthString(testDate, KCalendarSystem::ShortFormat), locale->convertDigits("31", locale->dateTimeDigitSet()));
0675 
0676     //Test Days In Week String
0677     QCOMPARE(calendar->daysInWeekString(testDate), locale->convertDigits("7", locale->dateTimeDigitSet()));
0678 
0679     //Test Year In Era String
0680     testDate.setDate(5, 1, 1);
0681     QCOMPARE(calendar->yearInEraString(testDate), locale->convertDigits("5", locale->dateTimeDigitSet()));
0682     QCOMPARE(calendar->yearInEraString(testDate, KCalendarSystem::LongFormat), locale->convertDigits("0005", locale->dateTimeDigitSet()));
0683     QCOMPARE(calendar->yearInEraString(testDate, KCalendarSystem::ShortFormat), locale->convertDigits("5", locale->dateTimeDigitSet()));
0684 
0685     delete calendar;
0686     delete locale;
0687 }
0688 
0689 void KCalendarTest::testIsoWeekDate()
0690 {
0691     KLocale *locale = new KLocale(*KLocale::global());
0692     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar, locale);
0693 
0694     int earliestValidYear = calendar->year(calendar->earliestValidDate());
0695     int latestValidYear = calendar->year(calendar->latestValidDate());
0696 
0697     QCOMPARE(calendar->isValidIsoWeekDate(earliestValidYear - 1, 1, 1), false);
0698     QCOMPARE(calendar->isValidIsoWeekDate(earliestValidYear,     1, 1), false);     //In prev year
0699     QCOMPARE(calendar->isValidIsoWeekDate(earliestValidYear,     1, 2), true);
0700     QCOMPARE(calendar->isValidIsoWeekDate(-1,                    1, 1), true);
0701     QCOMPARE(calendar->isValidIsoWeekDate(0,                     1, 1), false);
0702     QCOMPARE(calendar->isValidIsoWeekDate(1,                     1, 1), true);
0703     QCOMPARE(calendar->isValidIsoWeekDate(latestValidYear,      52, 5), true);
0704     QCOMPARE(calendar->isValidIsoWeekDate(latestValidYear,      52, 6), false);     //In following year
0705     QCOMPARE(calendar->isValidIsoWeekDate(latestValidYear,      52, 7), false);     //In following year
0706     QCOMPARE(calendar->isValidIsoWeekDate(latestValidYear + 1,   1, 1), false);
0707 
0708     QCOMPARE(calendar->isValidIsoWeekDate(2004,  0, 1), false);
0709     QCOMPARE(calendar->isValidIsoWeekDate(2004,  1, 1), true);
0710     QCOMPARE(calendar->isValidIsoWeekDate(2004, 53, 1), true);
0711     QCOMPARE(calendar->isValidIsoWeekDate(2000, 54, 1), false);
0712 
0713     QCOMPARE(calendar->isValidIsoWeekDate(2004, 1, 0), false);
0714     QCOMPARE(calendar->isValidIsoWeekDate(2004, 1, 1), true);
0715     QCOMPARE(calendar->isValidIsoWeekDate(2004, 1, 7), true);
0716     QCOMPARE(calendar->isValidIsoWeekDate(2004, 1, 8), false);
0717 
0718     QCOMPARE(setIsoWeekDate(calendar, earliestValidYear - 1, 1, 1), QDate());
0719     QCOMPARE(setIsoWeekDate(calendar, earliestValidYear,     1, 1), QDate());     //In prev year
0720     QCOMPARE(setIsoWeekDate(calendar, earliestValidYear,     1, 2), QDate(-9999,  1,  1));
0721     QCOMPARE(setIsoWeekDate(calendar, -1,                    1, 1), QDate(-1, 1, 3));
0722     QCOMPARE(setIsoWeekDate(calendar, 0,                     1, 1), QDate());
0723     QCOMPARE(setIsoWeekDate(calendar, 1,                     1, 1), QDate(1,  1,  1));
0724     QCOMPARE(setIsoWeekDate(calendar, latestValidYear,      52, 5), QDate(9999, 12, 31));
0725     QCOMPARE(setIsoWeekDate(calendar, latestValidYear,      52, 6), QDate());     //In next year
0726     QCOMPARE(setIsoWeekDate(calendar, latestValidYear,      52, 7), QDate());     //In next year
0727     QCOMPARE(setIsoWeekDate(calendar, latestValidYear + 1,   1, 1), QDate());
0728 
0729     QCOMPARE(setIsoWeekDate(calendar, 2004,  0, 1), QDate());
0730     QCOMPARE(setIsoWeekDate(calendar, 2004,  1, 1), QDate(2003, 12, 29));
0731     QCOMPARE(setIsoWeekDate(calendar, 2004, 53, 1), QDate(2004, 12, 27));
0732     QCOMPARE(setIsoWeekDate(calendar, 2000, 54, 1), QDate());
0733 
0734     QCOMPARE(setIsoWeekDate(calendar, 2004,  1, 0), QDate());
0735     QCOMPARE(setIsoWeekDate(calendar, 2004,  1, 1), QDate(2003, 12, 29));
0736     QCOMPARE(setIsoWeekDate(calendar, 2004,  1, 7), QDate(2004,  1,  4));
0737     QCOMPARE(setIsoWeekDate(calendar, 2004,  1, 8), QDate());
0738 
0739     QCOMPARE(setIsoWeekDate(calendar, 2004,  1, 1), QDate(2003, 12, 29));
0740     QCOMPARE(setIsoWeekDate(calendar, 2004,  1, 2), QDate(2003, 12, 30));
0741     QCOMPARE(setIsoWeekDate(calendar, 2004,  1, 3), QDate(2003, 12, 31));
0742     QCOMPARE(setIsoWeekDate(calendar, 2004,  1, 4), QDate(2004,  1,  1));
0743     QCOMPARE(setIsoWeekDate(calendar, 2004,  1, 5), QDate(2004,  1,  2));
0744     QCOMPARE(setIsoWeekDate(calendar, 2004,  1, 6), QDate(2004,  1,  3));
0745     QCOMPARE(setIsoWeekDate(calendar, 2004,  1, 7), QDate(2004,  1,  4));
0746 
0747     QCOMPARE(setIsoWeekDate(calendar, 2004, 24, 1), QDate(2004, 6,  7));
0748     QCOMPARE(setIsoWeekDate(calendar, 2004, 24, 2), QDate(2004, 6,  8));
0749     QCOMPARE(setIsoWeekDate(calendar, 2004, 24, 3), QDate(2004, 6,  9));
0750     QCOMPARE(setIsoWeekDate(calendar, 2004, 24, 4), QDate(2004, 6, 10));
0751     QCOMPARE(setIsoWeekDate(calendar, 2004, 24, 5), QDate(2004, 6, 11));
0752     QCOMPARE(setIsoWeekDate(calendar, 2004, 24, 6), QDate(2004, 6, 12));
0753     QCOMPARE(setIsoWeekDate(calendar, 2004, 24, 7), QDate(2004, 6, 13));
0754 
0755     QCOMPARE(setIsoWeekDate(calendar, 2004, 53, 1), QDate(2004, 12, 27));
0756     QCOMPARE(setIsoWeekDate(calendar, 2004, 53, 2), QDate(2004, 12, 28));
0757     QCOMPARE(setIsoWeekDate(calendar, 2004, 53, 3), QDate(2004, 12, 29));
0758     QCOMPARE(setIsoWeekDate(calendar, 2004, 53, 4), QDate(2004, 12, 30));
0759     QCOMPARE(setIsoWeekDate(calendar, 2004, 53, 5), QDate(2004, 12, 31));
0760     QCOMPARE(setIsoWeekDate(calendar, 2004, 53, 6), QDate(2005,  1,  1));
0761     QCOMPARE(setIsoWeekDate(calendar, 2004, 53, 7), QDate(2005,  1,  2));
0762 
0763     delete calendar;
0764     delete locale;
0765 }
0766 
0767 void KCalendarTest::testHebrewStrings()
0768 {
0769     /*
0770         Ref table for numbers to Hebrew chars
0771 
0772         Value     1       2       3        4        5       6         7        8      9
0773 
0774         x 1    Alef א  Bet  ב  Gimel ג  Dalet ד  He   ה  Vav  ו    Zayen ז  Het  ח  Tet  ט
0775                0x05D0  0x05D1  0x05D2   0x05D3   0x05D4  0x05D5    0x05D6   0x05D7  0x05D8
0776 
0777         x 10   Yod  י  Kaf  כ  Lamed ל  Mem  מ   Nun  נ  Samekh ס  Ayin ע   Pe   פ  Tzadi צ
0778                0x05D9  0x05DB  0x05DC   0x05DE   0x05E0  0x05E1    0x05E2   0x05E4  0x05E6
0779 
0780         x 100  Qof  ק  Resh ר  Shin ש   Tav  ת
0781                0x05E7  0x05E8  0x05E9   0x05EA
0782 
0783         Note special cases 15 = 9 + 6 טו and 16 = 9 + 7 טז
0784     */
0785 
0786     QString oldLocale = setlocale(LC_ALL, "he.utf8");
0787     KLocale *locale = new KLocale(*KLocale::global());
0788     locale->setLanguage(QStringList() << "he");
0789     locale->setDateFormat("%d %B %Y");
0790     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::HebrewCalendar, locale);
0791     if (locale->language() == "he") {
0792         QDate testDate;
0793         QString yearString, monthString, dayString, testString, resultString;
0794         calendar->setDate(testDate, 5765, 1, 1);
0795         // Have to use unicode values, for some reason directly using the chars doesn't work?
0796         yearString.append(QChar(0x05EA)).append(QChar(0x05E9)).append(QChar(0x05E1)).append(QChar('\"')).append(QChar(0x05D4));
0797         monthString = QString::fromUtf8("תשרי");
0798         dayString.append(QChar(0x05D0)).append(QChar('\''));
0799         testString = yearString + ' ' + monthString + ' ' + dayString;
0800         QCOMPARE(calendar->readDate(calendar->formatDate(testDate, KLocale::LongDate), KLocale::NormalFormat), testDate);
0801         QCOMPARE(calendar->formatDate(testDate, "%Y"), yearString);
0802         // Skip month name for now as won't translate for some reason, confirmed works visually in GUI
0803         //QCOMPARE( calendar->formatDate( testDate, "%B" ), monthString );
0804         QCOMPARE(calendar->formatDate(testDate, "%d"), dayString);
0805         //QCOMPARE( calendar->formatDate( testDate,  locale->dateFormat() ), testString );
0806         //QCOMPARE( calendar->readDate( testString ), testDate );
0807         for (int yy = 5400; yy <= 6400; ++yy) {
0808             calendar->setDate(testDate, yy, 1, 1);
0809             QCOMPARE(calendar->readDate(calendar->formatDate(testDate, KLocale::LongDate), KLocale::NormalFormat), testDate);
0810         }
0811     } else {
0812         qDebug() << "Cannot set Hebrew language, please install and re-test!";
0813     }
0814     setlocale(LC_ALL, oldLocale.toLatin1());
0815 }
0816 
0817 QDate KCalendarTest::setIsoWeekDate(const KCalendarSystem *calendar, int year, int isoWeek, int dayOfWeek)
0818 {
0819     QDate resultDate;
0820     calendar->setDateIsoWeek(resultDate, year, isoWeek, dayOfWeek);
0821     return resultDate;
0822 }
0823 
0824 void KCalendarTest::testDayOfYearDate()
0825 {
0826     KLocale *locale = new KLocale(*KLocale::global());
0827     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar, locale);
0828 
0829     int earliestValidYear = calendar->year(calendar->earliestValidDate());
0830     int latestValidYear = calendar->year(calendar->latestValidDate());
0831 
0832     QCOMPARE(calendar->isValid(earliestValidYear - 1, 1), false);
0833     QCOMPARE(calendar->isValid(earliestValidYear,     1), true);
0834     QCOMPARE(calendar->isValid(-1,                    1), true);
0835     QCOMPARE(calendar->isValid(0,                     1), false);
0836     QCOMPARE(calendar->isValid(1,                     1), true);
0837     QCOMPARE(calendar->isValid(latestValidYear,       1), true);
0838     QCOMPARE(calendar->isValid(latestValidYear + 1,   1), false);
0839 
0840     QCOMPARE(calendar->isValid(2005,   0), false);
0841     QCOMPARE(calendar->isValid(2005,   1), true);
0842     QCOMPARE(calendar->isValid(2005, 365), true);
0843     QCOMPARE(calendar->isValid(2005, 366), false);
0844     QCOMPARE(calendar->isValid(2000, 366), true);
0845     QCOMPARE(calendar->isValid(2000, 367), false);
0846 
0847     QCOMPARE(setDayOfYearDate(calendar, earliestValidYear - 1, 1), QDate());
0848     QCOMPARE(setDayOfYearDate(calendar, earliestValidYear,     1), QDate(earliestValidYear, 1, 1));
0849     QCOMPARE(setDayOfYearDate(calendar, -1,                    1), QDate(-1,                1, 1));
0850     QCOMPARE(setDayOfYearDate(calendar, 0,                     1), QDate());
0851     QCOMPARE(setDayOfYearDate(calendar, 1,                     1), QDate(1,                 1, 1));
0852     QCOMPARE(setDayOfYearDate(calendar, latestValidYear,       1), QDate(latestValidYear,   1, 1));
0853     QCOMPARE(setDayOfYearDate(calendar, latestValidYear + 1,   1), QDate());
0854 
0855     QCOMPARE(setDayOfYearDate(calendar, 2005,   0), QDate());
0856     QCOMPARE(setDayOfYearDate(calendar, 2005,   1), QDate(2005,  1,  1));
0857     QCOMPARE(setDayOfYearDate(calendar, 2005, 365), QDate(2005, 12, 31));
0858     QCOMPARE(setDayOfYearDate(calendar, 2005, 366), QDate());
0859     QCOMPARE(setDayOfYearDate(calendar, 2000, 366), QDate(2000, 12, 31));
0860     QCOMPARE(setDayOfYearDate(calendar, 2000, 367), QDate());
0861 
0862     delete calendar;
0863     delete locale;
0864 }
0865 
0866 QDate KCalendarTest::setDayOfYearDate(const KCalendarSystem *calendar, int year, int dayOfYear)
0867 {
0868     QDate resultDate;
0869     calendar->setDate(resultDate, year, dayOfYear);
0870     return resultDate;
0871 }
0872 
0873 void KCalendarTest::testDateDifference()
0874 {
0875     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
0876     QDate testDate1, testDate2;
0877 
0878     // Date Difference
0879 
0880     compareDateDifference(calendar, QDate(2010, 6,  1), QDate(2010, 6,  1),  0,  0,  0);
0881     compareDateDifference(calendar, QDate(2010, 6, 10), QDate(2010, 6, 20),  0,  0, 10);
0882     compareDateDifference(calendar, QDate(2010, 6,  1), QDate(2010, 7,  1),  0,  1,  0);
0883     compareDateDifference(calendar, QDate(2010, 6, 10), QDate(2010, 7, 20),  0,  1, 10);
0884     compareDateDifference(calendar, QDate(2010, 6, 20), QDate(2010, 7, 10),  0,  0, 20);
0885     compareDateDifference(calendar, QDate(2010, 6, 30), QDate(2010, 7, 31),  0,  1,  1);
0886     compareDateDifference(calendar, QDate(2010, 8, 31), QDate(2010, 9, 30),  0,  1,  0);
0887     compareDateDifference(calendar, QDate(2010, 7,  1), QDate(2011, 6,  1),  0, 11,  0);
0888     compareDateDifference(calendar, QDate(2010, 7, 10), QDate(2011, 6, 20),  0, 11, 10);
0889     compareDateDifference(calendar, QDate(2010, 7, 20), QDate(2011, 6, 10),  0, 10, 21);
0890     compareDateDifference(calendar, QDate(2010, 7, 31), QDate(2011, 6, 30),  0, 11,  0);
0891     compareDateDifference(calendar, QDate(2010, 8, 30), QDate(2011, 7, 31),  0, 11,  1);
0892     compareDateDifference(calendar, QDate(2010, 6, 10), QDate(2011, 6, 10),  1,  0,  0);
0893     compareDateDifference(calendar, QDate(2010, 6, 10), QDate(2011, 6, 20),  1,  0, 10);
0894     compareDateDifference(calendar, QDate(2010, 6, 10), QDate(2011, 7, 10),  1,  1,  0);
0895     compareDateDifference(calendar, QDate(2010, 6, 10), QDate(2011, 7, 20),  1,  1, 10);
0896     compareDateDifference(calendar, QDate(2010, 6, 20), QDate(2011, 8, 10),  1,  1, 21);
0897     compareDateDifference(calendar, QDate(2010, 6, 30), QDate(2011, 7, 31),  1,  1,  1);
0898     compareDateDifference(calendar, QDate(2010, 8, 31), QDate(2011, 9, 30),  1,  1,  0);
0899     compareDateDifference(calendar, QDate(2010, 7, 31), QDate(2012, 6, 30),  1, 11,  0);
0900     compareDateDifference(calendar, QDate(2010, 8, 30), QDate(2012, 7, 31),  1, 11,  1);
0901     compareDateDifference(calendar, QDate(2000, 2, 29), QDate(2001, 2, 28),  1,  0,  0);
0902     compareDateDifference(calendar, QDate(2000, 2, 29), QDate(2001, 3,  1),  1,  0,  1);
0903     compareDateDifference(calendar, QDate(2000, 2, 29), QDate(2001, 4,  1),  1,  1,  3);
0904     calendar->setDate(testDate1, -1, 1, 1);
0905     calendar->setDate(testDate2,  1, 1, 1);
0906     compareDateDifference(calendar, testDate1, testDate2,  1,  0,  0);
0907 
0908     // Years Difference
0909 
0910     compareYearDifference(calendar, QDate(2010, 1,  1), QDate(2010,  1,  1), 0);
0911     compareYearDifference(calendar, QDate(2010, 1,  1), QDate(2010, 12, 31), 0);
0912     compareYearDifference(calendar, QDate(2010, 6,  1), QDate(2010,  7,  1), 0);
0913     compareYearDifference(calendar, QDate(2010, 6,  1), QDate(2011,  5,  1), 0);
0914     compareYearDifference(calendar, QDate(2010, 6,  1), QDate(2011,  7,  1), 1);
0915     compareYearDifference(calendar, QDate(2010, 6, 20), QDate(2012,  6, 20), 2);
0916     compareYearDifference(calendar, QDate(2010, 6, 20), QDate(2012,  6, 30), 2);
0917     compareYearDifference(calendar, QDate(2010, 6, 20), QDate(2012,  6, 10), 1);
0918     compareYearDifference(calendar, QDate(2000, 2, 29), QDate(2001,  2, 28), 1);
0919     calendar->setDate(testDate1, -1, 12, 31);
0920     calendar->setDate(testDate2,  1,  1,  1);
0921     compareYearDifference(calendar, testDate1, testDate2, 0);
0922     calendar->setDate(testDate1, -1,  1,  1);
0923     compareYearDifference(calendar, testDate1, testDate2, 1);
0924 
0925     // Months Difference
0926 
0927     compareMonthDifference(calendar, QDate(2010, 1,  1), QDate(2010,  1,  1), 0);
0928     compareMonthDifference(calendar, QDate(2010, 1,  1), QDate(2010,  2,  1), 1);
0929     compareMonthDifference(calendar, QDate(2010, 1, 10), QDate(2010,  2,  1), 0);
0930     compareMonthDifference(calendar, QDate(2010, 1, 10), QDate(2010,  2, 20), 1);
0931     compareMonthDifference(calendar, QDate(2010, 1,  1), QDate(2011,  1,  1), 12);
0932     compareMonthDifference(calendar, QDate(2010, 1,  1), QDate(2011,  2,  1), 13);
0933     compareMonthDifference(calendar, QDate(2010, 1, 10), QDate(2011,  2,  1), 12);
0934     compareMonthDifference(calendar, QDate(2010, 1, 10), QDate(2011,  2, 20), 13);
0935     compareMonthDifference(calendar, QDate(2000, 2, 29), QDate(2001,  2, 27), 11);
0936     compareMonthDifference(calendar, QDate(2000, 2, 29), QDate(2001,  2, 28), 12);
0937     compareMonthDifference(calendar, QDate(2000, 2, 27), QDate(2001,  2, 28), 12);
0938     compareMonthDifference(calendar, QDate(2000, 2, 29), QDate(2001,  3,  1), 12);
0939     calendar->setDate(testDate1, -1, 12, 31);
0940     calendar->setDate(testDate2,  1,  1,  1);
0941     compareMonthDifference(calendar, testDate1, testDate2, 0);
0942     calendar->setDate(testDate1, -1, 12,  1);
0943     compareMonthDifference(calendar, testDate1, testDate2, 1);
0944 
0945     // Days Difference
0946 
0947     // Directly uses QDate::toJulianDay() so only basic testing needed
0948     testDate1.setDate(2010, 1, 1);
0949     testDate2.setDate(2010, 1, 2);
0950     QCOMPARE(calendar->daysDifference(testDate1, testDate2), 1);
0951     QCOMPARE(calendar->daysDifference(testDate2, testDate1), -1);
0952 
0953     // Test for crossing Julian/Gregorian switch!!!
0954 }
0955 
0956 void KCalendarTest::compareDateDifference(const KCalendarSystem *calendar,
0957         const QDate &lowDate, const QDate &highDate,
0958         int yearsDiff, int monthsDiff, int daysDiff)
0959 {
0960     int y, m, d, s;
0961     if (highDate >= lowDate) {
0962         calendar->dateDifference(lowDate, highDate, &y, &m, &d, &s);
0963         QCOMPARE(y, yearsDiff);
0964         QCOMPARE(m, monthsDiff);
0965         QCOMPARE(d, daysDiff);
0966         QCOMPARE(s, 1);
0967     }
0968     if (highDate > lowDate) {
0969         calendar->dateDifference(highDate, lowDate, &y, &m, &d, &s);
0970         QCOMPARE(y, yearsDiff);
0971         QCOMPARE(m, monthsDiff);
0972         QCOMPARE(d, daysDiff);
0973         QCOMPARE(s, -1);
0974     }
0975 }
0976 
0977 void KCalendarTest::compareYearDifference(const KCalendarSystem *calendar,
0978         const QDate &lowDate, const QDate &highDate,
0979         int yearsDiff)
0980 {
0981     if (highDate >= lowDate) {
0982         QCOMPARE(calendar->yearsDifference(lowDate, highDate), yearsDiff);
0983     }
0984     if (highDate > lowDate) {
0985         QCOMPARE(calendar->yearsDifference(highDate, lowDate), -yearsDiff);
0986     }
0987 }
0988 
0989 void KCalendarTest::compareMonthDifference(const KCalendarSystem *calendar,
0990         const QDate &lowDate, const QDate &highDate,
0991         int monthsDiff)
0992 {
0993     if (highDate >= lowDate) {
0994         QCOMPARE(calendar->monthsDifference(lowDate, highDate), monthsDiff);
0995     }
0996     if (highDate > lowDate) {
0997         QCOMPARE(calendar->monthsDifference(highDate, lowDate), -monthsDiff);
0998     }
0999 }
1000 
1001 void KCalendarTest::testFirstLast()
1002 {
1003     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
1004     QDate testDate = QDate(2010, 6, 15);
1005 
1006     QCOMPARE(calendar->firstDayOfYear(testDate),  QDate(2010,  1, 1));
1007     QCOMPARE(calendar->lastDayOfYear(testDate),   QDate(2010, 12, 31));
1008     QCOMPARE(calendar->firstDayOfYear(2010),      QDate(2010,  1,  1));
1009     QCOMPARE(calendar->lastDayOfYear(2010),       QDate(2010, 12, 31));
1010     QCOMPARE(calendar->firstDayOfMonth(testDate), QDate(2010,  6,  1));
1011     QCOMPARE(calendar->lastDayOfMonth(testDate),  QDate(2010,  6, 30));
1012     QCOMPARE(calendar->firstDayOfMonth(2010, 6),  QDate(2010,  6,  1));
1013     QCOMPARE(calendar->lastDayOfMonth(2010, 6),   QDate(2010,  6, 30));
1014 }
1015 
1016 void KCalendarTest::testEra()
1017 {
1018     KConfigGroup lcg(KSharedConfig::openConfig(), QString("Locale"));
1019     KConfigGroup cg = lcg.group(QString("KCalendarSystem %1").arg("gregorian"));
1020     cg.deleteGroup();
1021 
1022     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
1023 
1024     int earliestValidYear = calendar->year(calendar->earliestValidDate());
1025     int latestValidYear = calendar->year(calendar->latestValidDate());
1026 
1027     QDate testDate;
1028     calendar->setDate(testDate, 2005, 1, 1);
1029     QCOMPARE(calendar->yearInEra(testDate), 2005);
1030     QCOMPARE(calendar->eraName(testDate), QString("AD"));
1031     QCOMPARE(calendar->eraName(testDate, KCalendarSystem::ShortFormat), QString("AD"));
1032     QCOMPARE(calendar->eraName(testDate, KCalendarSystem::LongFormat), QString("Anno Domini"));
1033     calendar->setDate(testDate, -5, 1, 1);
1034     QCOMPARE(calendar->yearInEra(testDate), 5);
1035     QCOMPARE(calendar->eraName(testDate), QString("BC"));
1036     QCOMPARE(calendar->eraName(testDate, KCalendarSystem::ShortFormat), QString("BC"));
1037     QCOMPARE(calendar->eraName(testDate, KCalendarSystem::LongFormat), QString("Before Christ"));
1038 
1039     QCOMPARE(calendar->isValid("BC",            qAbs(earliestValidYear - 1), 1, 1), false);
1040     QCOMPARE(calendar->isValid("BC",            qAbs(earliestValidYear),     1, 1), true);
1041     QCOMPARE(calendar->isValid("Before Christ", qAbs(earliestValidYear),     1, 1), true);
1042     QCOMPARE(calendar->isValid("BC",            -1,                            1, 1), true);
1043     QCOMPARE(calendar->isValid("Before Christ", -1,                            1, 1), true);
1044     QCOMPARE(calendar->isValid("BC", 0,                                        1, 1), false);
1045     QCOMPARE(calendar->isValid("AD", 1,                                        1, 1), true);
1046     QCOMPARE(calendar->isValid("Anno Domini",   1,                             1, 1), true);
1047     QCOMPARE(calendar->isValid("AD",            latestValidYear,               1, 1), true);
1048     QCOMPARE(calendar->isValid("Anno Domini",   latestValidYear,               1, 1), true);
1049     QCOMPARE(calendar->isValid("AD",            latestValidYear + 1,           1, 1), false);
1050 
1051     QCOMPARE(calendar->isValid("AD", 2005,  0,  1), false);
1052     QCOMPARE(calendar->isValid("AD", 2005, 13,  1), false);
1053     QCOMPARE(calendar->isValid("AD", 2005,  1,  0), false);
1054     QCOMPARE(calendar->isValid("AD", 2005,  1, 32), false);
1055     QCOMPARE(calendar->isValid("AD", 2005,  1,  1), true);
1056     QCOMPARE(calendar->isValid("AD", 2005,  1, 31), true);
1057     QCOMPARE(calendar->isValid("AD", 2005, 12,  1), true);
1058     QCOMPARE(calendar->isValid("AD", 2005, 12, 31), true);
1059 
1060     QCOMPARE(setEraDate(calendar, "BC",            qAbs(earliestValidYear - 1), 1, 1), QDate());
1061     QCOMPARE(setEraDate(calendar, "BC",            qAbs(earliestValidYear),     1, 1), QDate(earliestValidYear, 1, 1));
1062     QCOMPARE(setEraDate(calendar, "Before Christ", qAbs(earliestValidYear),     1, 1), QDate(earliestValidYear, 1, 1));
1063     QCOMPARE(setEraDate(calendar, "BC",            1,                             1, 1), QDate(-1,                1, 1));
1064     QCOMPARE(setEraDate(calendar, "Before Christ", 1,                             1, 1), QDate(-1,                1, 1));
1065     QCOMPARE(setEraDate(calendar, "BC",            0,                             1, 1), QDate());
1066     QCOMPARE(setEraDate(calendar, "AD",            1,                             1, 1), QDate(1,                 1, 1));
1067     QCOMPARE(setEraDate(calendar, "Anno Domini",   1,                             1, 1), QDate(1,                 1, 1));
1068     QCOMPARE(setEraDate(calendar, "AD",            latestValidYear,               1, 1), QDate(latestValidYear,   1, 1));
1069     QCOMPARE(setEraDate(calendar, "Anno Domini",   latestValidYear,               1, 1), QDate(latestValidYear,   1, 1));
1070     QCOMPARE(setEraDate(calendar, "AD",            latestValidYear + 1,           1, 1), QDate());
1071 
1072     QCOMPARE(setEraDate(calendar, "AD", 2005,  0,  1), QDate());
1073     QCOMPARE(setEraDate(calendar, "AD", 2005, 13,  1), QDate());
1074     QCOMPARE(setEraDate(calendar, "AD", 2005,  1,  0), QDate());
1075     QCOMPARE(setEraDate(calendar, "AD", 2005,  1, 32), QDate());
1076     QCOMPARE(setEraDate(calendar, "AD", 2005,  1,  1), QDate(2005,  1,  1));
1077     QCOMPARE(setEraDate(calendar, "AD", 2005,  1, 31), QDate(2005,  1, 31));
1078     QCOMPARE(setEraDate(calendar, "AD", 2005, 12,  1), QDate(2005, 12,  1));
1079     QCOMPARE(setEraDate(calendar, "AD", 2005, 12, 31), QDate(2005, 12, 31));
1080 
1081     delete calendar;
1082     cg.writeEntry("Era1", QString::fromUtf8("-:1:-0001-01-01::Test Era 1:TE1:£%Ey£%EC£"));
1083     cg.writeEntry("Era2", QString::fromUtf8("+:1:0001-01-01::Test Era 2:TE2:^%Ey^%EC^"));
1084     calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
1085     testEraDate(calendar, QDate(2010,  1,  1), 2010, "2010", "2010", "TE2", "Test Era 2");
1086     testEraDate(calendar, QDate(-5,  1,  1),    5, "5",    "0005", "TE1", "Test Era 1");
1087     QCOMPARE(calendar->formatDate(QDate(2010, 1, 1), "%EY"), QString::fromUtf8("^2010^TE2^"));
1088     QCOMPARE(calendar->formatDate(QDate(-5, 1, 1), "%EY"), QString::fromUtf8("£5£TE1£"));
1089 
1090     cg.deleteGroup();
1091     cg.markAsClean();
1092     lcg.markAsClean();
1093     delete calendar;
1094 }
1095 
1096 QDate KCalendarTest::setEraDate(const KCalendarSystem *calendar, const QString &era, int yearInEra, int month, int day)
1097 {
1098     QDate resultDate;
1099     calendar->setDate(resultDate, era, yearInEra, month, day);
1100     return resultDate;
1101 }
1102 
1103 void KCalendarTest::testGregorianCalendar()
1104 {
1105     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
1106     QDate testDate(2005, 9, 10);
1107 
1108     QCOMPARE(calendar->dayOfYear(testDate), 253);
1109 
1110     QVERIFY(calendar->setDate(testDate, 2000, 3, 1));
1111     QCOMPARE(calendar->year(testDate), 2000);
1112     QCOMPARE(calendar->month(testDate), 3);
1113     QCOMPARE(calendar->day(testDate), 1);
1114     QCOMPARE(calendar->daysInYear(testDate), 366);
1115 
1116     QDate newDate = calendar->addYears(testDate, 4);
1117     QCOMPARE(newDate.year(), 2004);
1118     QCOMPARE(calendar->daysInYear(newDate), 366);
1119 
1120     newDate = calendar->addMonths(testDate, -4);
1121     QCOMPARE(newDate.year(), 1999);
1122     QCOMPARE(newDate.month(), 11);
1123     QCOMPARE(newDate.day(), 1);
1124 
1125     newDate = calendar->addDays(newDate, 20);
1126     QCOMPARE(newDate.year(), 1999);
1127     QCOMPARE(newDate.month(), 11);
1128     QCOMPARE(newDate.day(), 21);
1129 
1130     delete calendar;
1131 }
1132 
1133 void KCalendarTest::testHebrew()
1134 {
1135     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::HebrewCalendar);
1136     QDate testDate(2005, 9, 10);   // 5756-13-06
1137     QCOMPARE(calendar->dayOfYear(testDate), 360);
1138 
1139     QVERIFY(calendar->setDate(testDate, 5760, 12, 24));
1140     QCOMPARE(calendar->year(testDate), 5760);
1141     QCOMPARE(calendar->month(testDate), 12);
1142     QCOMPARE(calendar->day(testDate), 24);
1143     QCOMPARE(calendar->daysInYear(testDate), 385);
1144 
1145     testDate = calendar->addYears(testDate, 4);
1146     QCOMPARE(calendar->year(testDate), 5764);
1147     QCOMPARE(calendar->month(testDate), 12);
1148     QCOMPARE(calendar->day(testDate), 24);
1149     QCOMPARE(calendar->daysInYear(testDate), 355);
1150 
1151     testDate = calendar->addMonths(testDate, -4);
1152     QCOMPARE(calendar->year(testDate), 5764);
1153     QCOMPARE(calendar->month(testDate), 8);
1154     QCOMPARE(calendar->day(testDate), 24);
1155     QCOMPARE(calendar->daysInYear(testDate), 355);
1156 
1157     testDate = calendar->addDays(testDate, 20);
1158     QCOMPARE(calendar->year(testDate), 5764);
1159     QCOMPARE(calendar->month(testDate), 9);
1160     QCOMPARE(calendar->day(testDate), 15);
1161     QCOMPARE(calendar->daysInYear(testDate), 355);
1162 
1163     delete calendar;
1164 }
1165 
1166 void KCalendarTest::testIslamicCivil()
1167 {
1168     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::IslamicCivilCalendar);
1169 
1170     QDate testDate(2008, 12, 29);   //1430-01-01, jd 2454830, not leap
1171     QCOMPARE(calendar->year(testDate), 1430);
1172     QCOMPARE(calendar->month(testDate), 1);
1173     QCOMPARE(calendar->day(testDate), 1);
1174     QCOMPARE(calendar->isLeapYear(testDate), false);
1175     QCOMPARE(calendar->daysInYear(testDate), 354);
1176     QCOMPARE(calendar->daysInMonth(testDate), 30);
1177     QCOMPARE(calendar->dayOfYear(testDate), 1);
1178 
1179     testDate.setDate(2009, 12, 17);   //1430-12-29, jd 2455183, not leap
1180     QCOMPARE(calendar->year(testDate), 1430);
1181     QCOMPARE(calendar->month(testDate), 12);
1182     QCOMPARE(calendar->day(testDate), 29);
1183     QCOMPARE(calendar->isLeapYear(testDate), false);
1184     QCOMPARE(calendar->daysInYear(testDate), 354);
1185     QCOMPARE(calendar->daysInMonth(testDate), 29);
1186     QCOMPARE(calendar->dayOfYear(testDate), 354);
1187 
1188     testDate.setDate(2009, 12, 18);   //1431-01-01, jd 2455184, leap
1189     QCOMPARE(calendar->year(testDate), 1431);
1190     QCOMPARE(calendar->month(testDate), 1);
1191     QCOMPARE(calendar->day(testDate), 1);
1192     QCOMPARE(calendar->isLeapYear(testDate), true);
1193     QCOMPARE(calendar->daysInYear(testDate), 355);
1194     QCOMPARE(calendar->daysInMonth(testDate), 30);
1195     QCOMPARE(calendar->dayOfYear(testDate), 1);
1196 
1197     testDate.setDate(2010, 12, 7);   //1431-12-30, jd 2455538, leap
1198     QCOMPARE(calendar->year(testDate), 1431);
1199     QCOMPARE(calendar->month(testDate), 12);
1200     QCOMPARE(calendar->day(testDate), 30);
1201     QCOMPARE(calendar->isLeapYear(testDate), true);
1202     QCOMPARE(calendar->daysInYear(testDate), 355);
1203     QCOMPARE(calendar->daysInMonth(testDate), 30);
1204     QCOMPARE(calendar->dayOfYear(testDate), 355);
1205 
1206     testDate.setDate(2005, 9, 10);   //1426-08-06
1207     QCOMPARE(calendar->year(testDate), 1426);
1208     QCOMPARE(calendar->month(testDate), 8);
1209     QCOMPARE(calendar->day(testDate), 6);
1210     QCOMPARE(calendar->daysInYear(testDate), 355);
1211     QCOMPARE(calendar->daysInMonth(testDate), 29);
1212     QCOMPARE(calendar->weeksInYear(testDate.year()), 50);
1213     QCOMPARE(calendar->weekNumber(testDate), 31);
1214     QCOMPARE(calendar->dayOfYear(testDate), 213);
1215 
1216     QVERIFY(calendar->setDate(testDate, 2000, 3, 1));
1217     QCOMPARE(calendar->year(testDate), 2000);
1218     QCOMPARE(calendar->month(testDate), 3);
1219     QCOMPARE(calendar->day(testDate), 1);
1220 
1221     QDate newDate = calendar->addYears(testDate, 4);
1222     QCOMPARE(newDate.year(), 2566);
1223     QCOMPARE(calendar->daysInYear(newDate), 355);
1224 
1225     newDate = calendar->addMonths(testDate, -4);
1226     QCOMPARE(newDate.year(), 2561);
1227     QCOMPARE(newDate.month(), 11);
1228     QCOMPARE(newDate.day(), 10);
1229 
1230     newDate = calendar->addDays(newDate, 20);
1231     QCOMPARE(newDate.year(), 2561);
1232     QCOMPARE(newDate.month(), 11);
1233     QCOMPARE(newDate.day(), 30);
1234 
1235     delete calendar;
1236 }
1237 
1238 void KCalendarTest::testIndianNational()
1239 {
1240     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::IndianNationalCalendar);
1241     QDate testDate(2005, 9, 10);
1242 
1243     calendar->setDate(testDate, 2000, 1, 1);
1244     calendar->setDate(testDate, 2000, 1, 10);
1245     calendar->setDate(testDate, 2000, 2, 10);
1246     calendar->setDate(testDate, 2000, 5, 20);
1247     calendar->setDate(testDate, 2000, 7, 1);
1248     calendar->setDate(testDate, 2000, 7, 10);
1249     calendar->setDate(testDate, 2000, 9, 10);
1250     calendar->setDate(testDate, 2000, 12, 20);
1251 
1252     QCOMPARE(calendar->isLeapYear(2009), false);
1253     QCOMPARE(calendar->isLeapYear(testDate), false);
1254 
1255     QVERIFY(calendar->setDate(testDate, 2000, 3, 1));
1256     QCOMPARE(calendar->year(testDate), 2000);
1257     QCOMPARE(calendar->month(testDate), 3);
1258     QCOMPARE(calendar->day(testDate), 1);
1259 
1260     // Test some key dates
1261     testYmd(calendar,    0,  1,  1, 1749630);
1262     testYmd(calendar,    0, 12, 30, 1749994);
1263     testYmd(calendar, 1922,  1,  1, QDate(2000,  3, 21).toJulianDay());
1264     testYmd(calendar, 1922, 12, 30, QDate(2001,  3, 21).toJulianDay());
1265     testYmd(calendar, 1932,  1,  1, QDate(2010,  3, 22).toJulianDay());
1266     testYmd(calendar, 1932, 12, 30, QDate(2011,  3, 21).toJulianDay());
1267 
1268     testRoundTrip(calendar);
1269 
1270     delete calendar;
1271 }
1272 
1273 void KCalendarTest::testGregorianCalendarBasic()
1274 {
1275     KConfigGroup lcg(KSharedConfig::openConfig(), QString("Locale"));
1276     KConfigGroup cg = lcg.group(QString("KCalendarSystem %1").arg("gregorian"));
1277     cg.deleteGroup();
1278 
1279     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
1280 
1281     testCalendarSystemType(calendar, KLocale::GregorianCalendar, "gregorian", "Gregorian");
1282 
1283     QCOMPARE(calendar->epoch(), QDate::fromJulianDay(1721426));
1284     QCOMPARE(calendar->epoch(), QDate(1, 1, 1));
1285     QCOMPARE(calendar->earliestValidDate(), QDate(-9999, 1, 1));
1286     QCOMPARE(calendar->latestValidDate(), QDate(9999, 12, 31));
1287 
1288     testValid(calendar, 0, 10000, 13, 32, QDate(-10000, 1, 1));
1289 
1290     QCOMPARE(calendar->isLeapYear(2007), false);
1291     QCOMPARE(calendar->isLeapYear(2008), true);
1292     QCOMPARE(calendar->isLeapYear(1900), false);
1293     QCOMPARE(calendar->isLeapYear(2000), true);
1294     QCOMPARE(calendar->isLeapYear(QDate(2007, 1, 1)), false);
1295     QCOMPARE(calendar->isLeapYear(QDate(2008, 1, 1)), true);
1296 
1297     QCOMPARE(calendar->daysInWeek(QDate(2007, 1, 1)), 7);
1298 
1299     QCOMPARE(calendar->monthsInYear(2007),                12);
1300     QCOMPARE(calendar->monthsInYear(QDate(2007, 1, 1)), 12);
1301 
1302     testYear(calendar, QDate(2007, 7, 9), 2007, QString("07"), QString("2007"));
1303     testMonth(calendar, QDate(2007, 7, 9),    7, QString("7"),  QString("07"));
1304     testDay(calendar, QDate(2007, 7, 9),    9, QString("9"),  QString("09"));
1305 
1306     testEraDate(calendar, QDate(2005, 1, 1), 2005, "2005", "2005", "AD", "Anno Domini");
1307     testEraDate(calendar, QDate(-5, 1, 1),    5, "5",    "0005", "BC", "Before Christ");
1308 
1309     testWeekDayName(calendar, 6, QDate(2007, 7, 28), "S", "Sat", "Saturday");
1310     testMonthName(calendar, 12, 2007, QDate(2007, 12, 20), "D", "Dec", "December", "of Dec", "of December");
1311 
1312     QCOMPARE(calendar->monthsInYear(QDate(2007, 1, 1)), 12);
1313 
1314     QCOMPARE(calendar->isProleptic(), true);
1315     QCOMPARE(calendar->isLunar(), false);
1316     QCOMPARE(calendar->isLunisolar(), false);
1317     QCOMPARE(calendar->isSolar(), true);
1318 
1319     testRoundTrip(calendar);
1320 
1321     delete calendar;
1322 }
1323 
1324 void KCalendarTest::testGregorianCalendarYmd()
1325 {
1326     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
1327     testYmd(calendar, 2007, 1, 1, QDate(2007, 1, 1).toJulianDay());
1328     delete calendar;
1329 }
1330 
1331 void KCalendarTest::testGregorianCalendarSpecialCases()
1332 {
1333     KConfigGroup lcg(KSharedConfig::openConfig(), QString("Locale"));
1334     KConfigGroup cg = lcg.group(QString("KCalendarSystem %1").arg("gregorian"));
1335     cg.deleteGroup();
1336 
1337     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
1338 
1339     testEraDate(calendar, QDate(2010,  1,  1), 2010, "2010", "2010", "AD", "Anno Domini");
1340     testEraDate(calendar, QDate(-5,  1,  1),    5, "5",    "0005", "BC", "Before Christ");
1341 
1342     delete calendar;
1343     cg.writeEntry("UseCommonEra", false, KConfigGroup::Global);
1344     calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
1345     testEraDate(calendar, QDate(2010,  1,  1), 2010, "2010", "2010", "AD", "Anno Domini");
1346     testEraDate(calendar, QDate(-5,  1,  1),    5, "5",    "0005", "BC", "Before Christ");
1347 
1348     delete calendar;
1349     cg.writeEntry("UseCommonEra", true, KConfigGroup::Global);
1350     calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
1351     testEraDate(calendar, QDate(2010,  1,  1), 2010, "2010", "2010", "CE", "Common Era");
1352     testEraDate(calendar, QDate(-5,  1,  1),    5, "5",    "0005", "BCE", "Before Common Era");
1353 
1354     cg.deleteGroup();
1355     cg.markAsClean();
1356     delete calendar;
1357 }
1358 
1359 void KCalendarTest::testHebrewBasic()
1360 {
1361     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::HebrewCalendar);
1362     QDate testDate;
1363 
1364     testCalendarSystemType(calendar, KLocale::HebrewCalendar, "hebrew", "Hebrew");
1365 
1366     testEpoch(calendar,                1,  1,  1,  347998);
1367     testEarliestValidDate(calendar, 5344,  1,  1, 2299498);
1368     testLatestValidDate(calendar, 8119, 13, 29, 3313431);
1369 
1370     testDate = QDate::fromJulianDay(2450340);   //5756-12-29 Not Leap
1371     QCOMPARE(calendar->isLeapYear(5756), false);
1372     QCOMPARE(calendar->isLeapYear(testDate), false);
1373     QCOMPARE(calendar->monthsInYear(testDate), 12);
1374     QCOMPARE(calendar->month(testDate), 12);
1375     QCOMPARE(calendar->monthName(testDate), QString("Elul"));
1376 
1377     testDate = QDate::fromJulianDay(2450723);   //5757-13-29 Leap
1378     QCOMPARE(calendar->isLeapYear(5757), true);
1379     QCOMPARE(calendar->isLeapYear(testDate), true);
1380     QCOMPARE(calendar->monthsInYear(testDate), 13);
1381     QCOMPARE(calendar->month(testDate), 13);
1382     QCOMPARE(calendar->monthName(testDate), QString("Elul"));
1383 
1384     // 5707-01-01 Thursday
1385     testDate = QDate::fromJulianDay(2432090);
1386 
1387     QCOMPARE(calendar->daysInWeek(testDate), 7);
1388 
1389     testYear(calendar, testDate, 5707, "07", "5707");
1390     testMonth(calendar, testDate,    1,  "1",   "01");
1391     testDay(calendar, testDate,    1,  "1",   "01");
1392 
1393     testEraDate(calendar, testDate, 5707, "5707", "5707", "AM", "Anno Mundi");
1394 
1395     testWeekDayName(calendar, 4, testDate, "T", "Thu", "Thursday");
1396     testMonthName(calendar, 1, 5707, testDate, "T", "Tis", "Tishrey", "of Tis", "of Tishrey");
1397 
1398     QCOMPARE(calendar->isProleptic(), false);
1399     QCOMPARE(calendar->isLunar(), false);
1400     QCOMPARE(calendar->isLunisolar(), true);
1401     QCOMPARE(calendar->isSolar(), false);
1402 
1403     testRoundTrip(calendar);
1404 
1405     delete calendar;
1406 }
1407 
1408 void KCalendarTest::testHebrewYmd()
1409 {
1410 //    const KCalendarSystem *calendar = KCalendarSystem::create( KLocale::HebrewCalendar );
1411 //    testYmd( calendar, 2007, 1, 1, QDate( 2007, 1, 1 ).toJulianDay() );
1412 //    delete calendar;
1413 }
1414 
1415 void KCalendarTest::testHebrewCompare()
1416 {
1417     /*
1418         const KCalendarSystemHebrew *calendar = new KCalendarSystemHebrew();
1419         QDate setOldDate, setNewDate;
1420 
1421         for ( QDate testDate( 1900, 1, 1 ); testDate.year() == 2100; testDate.addDays(1) ) {
1422             QCOMPARE( calendar->year(  testDate ), calendar->oldyear(  testDate ) );
1423             QCOMPARE( calendar->month( testDate ), calendar->oldmonth( testDate ) );
1424             QCOMPARE( calendar->day(   testDate ), calendar->oldday(   testDate ) );
1425 
1426             calendar->setDate(setOldDate, calendar->oldyear( testDate ), calendar->oldmonth( testDate ), calendar->oldday( testDate ) );
1427             calendar->setDate(setNewDate, calendar->oldyear( testDate ), calendar->oldmonth( testDate ), calendar->oldday( testDate ) );
1428             QCOMPARE( setNewDate.toJulianDay(), setOldDate.toJulianDay() );
1429         }
1430 
1431         delete calendar;
1432     */
1433 }
1434 
1435 // Test Islamic Civil Calendar System
1436 
1437 void KCalendarTest::testIslamicCivilBasic()
1438 {
1439     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::IslamicCivilCalendar);
1440     QDate testDate;
1441 
1442     testCalendarSystemType(calendar, KLocale::IslamicCivilCalendar, "hijri", "Islamic / Hijri (Civil)");
1443 
1444     QCOMPARE(calendar->epoch(), QDate::fromJulianDay(1948440));
1445     QCOMPARE(calendar->earliestValidDate(), QDate::fromJulianDay(1948440));
1446     QCOMPARE(calendar->latestValidDate(), QDate(10323, 10, 21));
1447 
1448     testValid(calendar, 0, 10000, 13, 31, QDate(1, 1, 1));
1449 
1450     QCOMPARE(calendar->isLeapYear(1427), false);
1451     QCOMPARE(calendar->isLeapYear(1428), true);
1452     QCOMPARE(calendar->isLeapYear(QDate(2007, 1, 1)), false);
1453     QCOMPARE(calendar->isLeapYear(QDate(2008, 1, 1)), true);
1454 
1455     // 1426-08-06 Saturday
1456     testDate = QDate(2005, 9, 10);
1457 
1458     QCOMPARE(calendar->daysInWeek(testDate), 7);
1459 
1460     QCOMPARE(calendar->monthsInYear(1426),     12);
1461     QCOMPARE(calendar->monthsInYear(testDate), 12);
1462 
1463     testYear(calendar, testDate, 1426, "26", "1426");
1464     testMonth(calendar, testDate,    8, "8",  "08");
1465     testDay(calendar, testDate,    6, "6",  "06");
1466 
1467     testEraDate(calendar, testDate, 1426, "1426", "1426", "AH", "Anno Hegirae");
1468 
1469     testWeekDayName(calendar, 6, testDate, "S", "Sab", "Yaum al-Sabt");
1470     testMonthName(calendar, 12, 1428, QDate(2007, 12, 20), "H", "Hij", "Thu al-Hijjah", "of Hij", "of Thu al-Hijjah");
1471 
1472     QCOMPARE(calendar->monthsInYear(testDate), 12);
1473 
1474     QCOMPARE(calendar->isProleptic(), false);
1475     QCOMPARE(calendar->isLunar(), true);
1476     QCOMPARE(calendar->isLunisolar(), false);
1477     QCOMPARE(calendar->isSolar(), false);
1478 
1479     testRoundTrip(calendar);
1480 
1481     delete calendar;
1482 }
1483 
1484 void KCalendarTest::testIslamicCivilYmd()
1485 {
1486     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::IslamicCivilCalendar);
1487     testYmd(calendar, 1426, 8, 6, QDate(2005, 9, 10).toJulianDay());
1488     delete calendar;
1489 }
1490 
1491 // Jalali Calendar System
1492 
1493 void KCalendarTest::testJalaliBasic()
1494 {
1495     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::JalaliCalendar);
1496     QDate testDate;
1497 
1498     testCalendarSystemType(calendar, KLocale::JalaliCalendar, "jalali", "Jalali");
1499 
1500     //Birashk argorithm only good between AP 1244-01-01 to 1530-12-29 (AD 1865 to 2152)
1501     QCOMPARE(calendar->epoch(), QDate::fromJulianDay(1948321));
1502     QCOMPARE(calendar->earliestValidDate(), QDate(1865, 03, 21));
1503     QCOMPARE(calendar->latestValidDate(), QDate(2152, 03, 19));
1504     QCOMPARE(calendar->isValid(1243, 12, 29), false);
1505     QCOMPARE(calendar->isValid(1531, 1, 1), false);
1506 
1507     QCOMPARE(calendar->isLeapYear(1386), false);
1508     QCOMPARE(calendar->isLeapYear(1387), true);
1509     QCOMPARE(calendar->isLeapYear(QDate(2008, 1, 1)), false);
1510     QCOMPARE(calendar->isLeapYear(QDate(2009, 1, 1)), true);
1511 
1512     testDate = QDate(2005, 8, 31);   // 1384-06-09 Wednesday
1513 
1514     QCOMPARE(calendar->daysInWeek(testDate),    7);
1515     QCOMPARE(calendar->monthsInYear(1384),     12);
1516     QCOMPARE(calendar->monthsInYear(testDate), 12);
1517 
1518     testYear(calendar, testDate, 1384, "84", "1384");
1519     testMonth(calendar, testDate,    6, "6",  "06");
1520     testDay(calendar, testDate,    9, "9",  "09");
1521 
1522     testEraDate(calendar, testDate, 1384, "1384", "1384", "AP", "Anno Persico");
1523 
1524     testWeekDayName(calendar, 3, testDate, "4", "4sh", "Chahar shanbe");
1525     testMonthName(calendar, 6, 1384, testDate, "S", "Sha", "Shahrivar", "of Sha", "of Shahrivar");
1526 
1527     QCOMPARE(calendar->monthsInYear(testDate), 12);
1528 
1529     QCOMPARE(calendar->isProleptic(), false);
1530     QCOMPARE(calendar->isLunar(), false);
1531     QCOMPARE(calendar->isLunisolar(), false);
1532     QCOMPARE(calendar->isSolar(), true);
1533 
1534     testRoundTrip(calendar);
1535 
1536     //Special cases using Birashk argorithm
1537     QCOMPARE(calendar->isLeapYear(1403), true);
1538     QCOMPARE(calendar->isLeapYear(1404), false);
1539     QCOMPARE(calendar->isLeapYear(1436), true);
1540     QCOMPARE(calendar->isLeapYear(1437), false);
1541 
1542     delete calendar;
1543 }
1544 
1545 void KCalendarTest::testJalaliYmd()
1546 {
1547     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::JalaliCalendar);
1548     testYmd(calendar, 1384, 6, 9, QDate(2005, 8, 31).toJulianDay());
1549     delete calendar;
1550 }
1551 
1552 void KCalendarTest::testJapanese()
1553 {
1554     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::JapaneseCalendar);
1555     QDate testDate(2005, 9, 10);
1556 
1557     QCOMPARE(calendar->dayOfYear(testDate), 253);
1558 
1559     QVERIFY(calendar->setDate(testDate, 2000, 3, 1));
1560     QCOMPARE(calendar->year(testDate), 2000);
1561     QCOMPARE(calendar->month(testDate), 3);
1562     QCOMPARE(calendar->day(testDate), 1);
1563     QCOMPARE(calendar->daysInYear(testDate), 366);
1564 
1565     QDate newDate = calendar->addYears(testDate, 4);
1566     QCOMPARE(newDate.year(), 2004);
1567     QCOMPARE(calendar->daysInYear(newDate), 366);
1568 
1569     newDate = calendar->addMonths(testDate, -4);
1570     QCOMPARE(newDate.year(), 1999);
1571     QCOMPARE(newDate.month(), 11);
1572     QCOMPARE(newDate.day(), 1);
1573 
1574     newDate = calendar->addDays(newDate, 20);
1575     QCOMPARE(newDate.year(), 1999);
1576     QCOMPARE(newDate.month(), 11);
1577     QCOMPARE(newDate.day(), 21);
1578 
1579     delete calendar;
1580 }
1581 
1582 void KCalendarTest::testJapaneseBasic()
1583 {
1584     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::JapaneseCalendar);
1585     QDate testDate;
1586 
1587     testCalendarSystemType(calendar, KLocale::JapaneseCalendar, "japanese", "Japanese");
1588 
1589     QCOMPARE(calendar->epoch(), QDate::fromJulianDay(1721426));
1590     QCOMPARE(calendar->earliestValidDate(), QDate::fromJulianDay(1721426));
1591     QCOMPARE(calendar->latestValidDate(), QDate(9999, 12, 31));
1592 
1593     testValid(calendar, 0, 10000, 13, 32, QDate(-5000, 1, 1));
1594 
1595     QCOMPARE(calendar->isLeapYear(2007), false);
1596     QCOMPARE(calendar->isLeapYear(2008), true);
1597     QCOMPARE(calendar->isLeapYear(1900), false);
1598     QCOMPARE(calendar->isLeapYear(2000), true);
1599     QCOMPARE(calendar->isLeapYear(QDate(2007, 1, 1)), false);
1600     QCOMPARE(calendar->isLeapYear(QDate(2008, 1, 1)), true);
1601 
1602     QCOMPARE(calendar->daysInWeek(QDate(2007, 1, 1)), 7);
1603     QCOMPARE(calendar->monthsInYear(QDate(2007, 1, 1)), 12);
1604 
1605     testDate = QDate(2007, 7, 9);   // 2007-07-09
1606     testYear(calendar, testDate, 2007, "07", "2007");
1607     testMonth(calendar, testDate,    7, "7",  "07");
1608     testDay(calendar, testDate,    9, "9",  "09");
1609 
1610     testEraDate(calendar, testDate,   19, "19", "0019", "Heisei", "Heisei");
1611 
1612     testWeekDayName(calendar, 6, QDate(2007, 7, 28), "S", "Sat", "Saturday");
1613     testMonthName(calendar, 12, 2007, QDate(2007, 12, 20), "D", "Dec", "December", "of Dec", "of December");
1614 
1615     QCOMPARE(calendar->monthsInYear(QDate(2007, 1, 1)), 12);
1616 
1617     QCOMPARE(calendar->isProleptic(), false);
1618     QCOMPARE(calendar->isLunar(), false);
1619     QCOMPARE(calendar->isLunisolar(), false);
1620     QCOMPARE(calendar->isSolar(), true);
1621 
1622     testRoundTrip(calendar);
1623 
1624     delete calendar;
1625 }
1626 
1627 void KCalendarTest::testJapaneseYmd()
1628 {
1629     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::JapaneseCalendar);
1630     testYmd(calendar, 2007, 1, 1, QDate(2007, 1, 1).toJulianDay());
1631     delete calendar;
1632 }
1633 
1634 void KCalendarTest::testJapaneseSpecialCases()
1635 {
1636     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::JapaneseCalendar);
1637 
1638     // Test Japanese Eras
1639     // Heisei > 1        1990-01-01 onwards
1640     // Heisei 1 (Gannen) 1989-01-08 to 1989-12-31
1641     // Shōwa > 1         1927-01-01 to 1989-01-07
1642     // Shōwa 1 (Gannen)  1926-12-25 to 1926-12-31
1643     // Taishō > 1        1913-01-01 to 1926-12-24
1644     // Taishō 1 (Gannen) 1912-07-30 to 1912-12-31
1645     // Meiji > 1         1869-01-01 to 1912-07-29
1646     // Meiji 1 (Gannen)  1868-09-08 to 1868-12-31
1647     // Gregorian         0001-01-01 to 1868-09-07
1648 
1649     testEraDate(calendar, QDate(1990,  1,  1),    2,    "2", "0002", "Heisei", "Heisei");
1650     testEraDate(calendar, QDate(1989,  1,  8),    1,    "1", "0001", "Heisei", "Heisei");
1651     //testEraDate( calendar, QDate( 1989,  1,  7 ),   64,   "64", "0064", "Shōwa",  "Shōwa" );
1652     //testEraDate( calendar, QDate( 1927,  1,  1 ),    2,    "2", "0002", "Shōwa",  "Shōwa" );
1653     //testEraDate( calendar, QDate( 1926, 12, 25 ),    1,    "1", "0001", "Shōwa",  "Shōwa" );
1654     //testEraDate( calendar, QDate( 1926, 12, 24 ),   15,   "15", "0015", "Taishō", "Taishō" );
1655     //testEraDate( calendar, QDate( 1913,  1,  1 ),    2,    "2", "0002", "Taishō", "Taishō" );
1656     //testEraDate( calendar, QDate( 1912,  7, 30 ),    1,    "1", "0001", "Taishō", "Taishō" );
1657     testEraDate(calendar, QDate(1912,  7, 29),   45,   "45", "0045", "Meiji",  "Meiji");
1658     testEraDate(calendar, QDate(1869,  1,  1),    2,    "2", "0002", "Meiji",  "Meiji");
1659     testEraDate(calendar, QDate(1868,  9,  8),    1,    "1", "0001", "Meiji",  "Meiji");
1660     testEraDate(calendar, QDate(1868,  9,  7), 1868, "1868", "1868", "AD",     "Anno Domini");
1661 
1662     // Should always be able to read Gannen as year 1
1663     QCOMPARE(calendar->readDate("1 2 Heisei 1", "%e %n %EC %Ey"), QDate(1989, 2, 1));
1664     QCOMPARE(calendar->readDate("1 2 Heisei 1", "%e %n %EY"), QDate(1989, 2, 1));
1665     QCOMPARE(calendar->readDate("1 2 Heisei Gannen", "%e %n %EC %Ey"), QDate(1989, 2, 1));
1666     QCOMPARE(calendar->readDate("1 2 Heisei Gannen", "%e %n %EY"), QDate(1989, 2, 1));
1667 
1668     // Should only format Gannen if defined in %EY format, never for %Ey
1669     QCOMPARE(calendar->formatDate(QDate(1989, 2, 1), "%e %n %EC %Ey"), QString("1 2 Heisei 1"));
1670     QCOMPARE(calendar->formatDate(QDate(1989, 2, 1), "%e %n %EY"),     QString("1 2 Heisei Gannen"));
1671     QCOMPARE(calendar->formatDate(QDate(1990, 2, 1), "%e %n %EC %Ey"), QString("1 2 Heisei 2"));
1672     QCOMPARE(calendar->formatDate(QDate(1990, 2, 1), "%e %n %EY"),     QString("1 2 Heisei 2"));
1673 
1674     delete calendar;
1675 }
1676 
1677 void KCalendarTest::testMinguoBasic()
1678 {
1679     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::MinguoCalendar);
1680 
1681     testCalendarSystemType(calendar, KLocale::MinguoCalendar, "minguo", "Taiwanese");
1682 
1683     QCOMPARE(calendar->epoch(), QDate(1912, 1, 1));
1684     QCOMPARE(calendar->earliestValidDate(), QDate(1912, 1, 1));
1685     QCOMPARE(calendar->latestValidDate(), QDate(11910, 12, 31));
1686 
1687     testValid(calendar, -1, 10000, 13, 32, QDate());
1688 
1689     QCOMPARE(calendar->isLeapYear(2007 - 1911), false);
1690     QCOMPARE(calendar->isLeapYear(2008 - 1911), true);
1691     QCOMPARE(calendar->isLeapYear(1900 - 1911), false);
1692     QCOMPARE(calendar->isLeapYear(2000 - 1911), true);
1693     QCOMPARE(calendar->isLeapYear(QDate(2007, 1, 1)), false);
1694     QCOMPARE(calendar->isLeapYear(QDate(2008, 1, 1)), true);
1695 
1696     QCOMPARE(calendar->daysInWeek(QDate(2007, 1, 1)), 7);
1697 
1698     QCOMPARE(calendar->monthsInYear(2007 - 1911),         12);
1699     QCOMPARE(calendar->monthsInYear(QDate(2007, 1, 1)), 12);
1700 
1701     testYear(calendar, QDate(2007, 7, 9), 2007 - 1911, QString("96"), QString("0096"));
1702     testMonth(calendar, QDate(2007, 7, 9),    7, QString("7"),  QString("07"));
1703     testDay(calendar, QDate(2007, 7, 9),    9, QString("9"),  QString("09"));
1704 
1705     testEraDate(calendar, QDate(2007, 1, 1), 2007 - 1911, "96", "0096", "ROC", "Republic of China Era");
1706 
1707     testWeekDayName(calendar, 6, QDate(2007, 7, 28), "S", "Sat", "Saturday");
1708     testMonthName(calendar, 12, 2007 - 1911, QDate(2007, 12, 20), "D", "Dec", "December", "of Dec", "of December");
1709 
1710     QCOMPARE(calendar->monthsInYear(QDate(2007, 1, 1)), 12);
1711 
1712     QCOMPARE(calendar->isProleptic(), false);
1713     QCOMPARE(calendar->isLunar(), false);
1714     QCOMPARE(calendar->isLunisolar(), false);
1715     QCOMPARE(calendar->isSolar(), true);
1716 
1717     testRoundTrip(calendar);
1718 
1719     delete calendar;
1720 }
1721 
1722 void KCalendarTest::testThaiBasic()
1723 {
1724     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::ThaiCalendar);
1725 
1726     testCalendarSystemType(calendar, KLocale::ThaiCalendar, "thai", "Thai");
1727 
1728     QCOMPARE(calendar->epoch(), QDate::fromJulianDay(1522734));
1729     QCOMPARE(calendar->earliestValidDate(), QDate::fromJulianDay(1522734));
1730     QCOMPARE(calendar->latestValidDate(), QDate(9456, 12, 31));
1731 
1732     testValid(calendar, -1, 10000, 13, 32, QDate());
1733 
1734     QCOMPARE(calendar->isLeapYear(2007 + 543), false);
1735     QCOMPARE(calendar->isLeapYear(2008 + 543), true);
1736     QCOMPARE(calendar->isLeapYear(1900 + 543), false);
1737     QCOMPARE(calendar->isLeapYear(2000 + 543), true);
1738     QCOMPARE(calendar->isLeapYear(QDate(2007, 1, 1)), false);
1739     QCOMPARE(calendar->isLeapYear(QDate(2008, 1, 1)), true);
1740 
1741     QCOMPARE(calendar->daysInWeek(QDate(2007, 1, 1)), 7);
1742 
1743     QCOMPARE(calendar->monthsInYear(2007 + 543),          12);
1744     QCOMPARE(calendar->monthsInYear(QDate(2007, 1, 1)), 12);
1745 
1746     testYear(calendar, QDate(2007, 7, 9), 2007 + 543, QString("50"), QString("2550"));
1747     testMonth(calendar, QDate(2007, 7, 9),    7, QString("7"),  QString("07"));
1748     testDay(calendar, QDate(2007, 7, 9),    9, QString("9"),  QString("09"));
1749 
1750     testEraDate(calendar, QDate(2007, 1, 1), 2007 + 543, "2550", "2550", "BE", "Buddhist Era");
1751 
1752     testWeekDayName(calendar, 6, QDate(2007, 7, 28), "S", "Sat", "Saturday");
1753     testMonthName(calendar, 12, 2007 + 543, QDate(2007, 12, 20), "D", "Dec", "December", "of Dec", "of December");
1754 
1755     QCOMPARE(calendar->monthsInYear(QDate(2007, 1, 1)), 12);
1756 
1757     QCOMPARE(calendar->isProleptic(), false);
1758     QCOMPARE(calendar->isLunar(), false);
1759     QCOMPARE(calendar->isLunisolar(), false);
1760     QCOMPARE(calendar->isSolar(), true);
1761 
1762     testRoundTrip(calendar);
1763 
1764     delete calendar;
1765 }
1766 
1767 // generic test functions, call from calendar system specific ones
1768 void KCalendarTest::testCalendarSystemType(const KCalendarSystem *calendar,
1769         KLocale::CalendarSystem system,
1770         const QString &type, const QString &label)
1771 {
1772     QCOMPARE(calendar->calendarType(),               type);
1773     QCOMPARE(calendar->calendarSystem(),             system);
1774     QCOMPARE(calendar->calendarLabel(),              label);
1775     QCOMPARE(KCalendarSystem::calendarLabel(system), label);
1776 }
1777 
1778 // Simply tests valid ranges of ymd values, testYmd covers all other dates
1779 void KCalendarTest::testValid(const KCalendarSystem *calendar, int lowInvalidYear, int highInvalidYear,
1780                               int highInvalidMonth, int highInvalidDay, const QDate &invalidDate)
1781 {
1782     // min/max year
1783     QCOMPARE(calendar->isValid(lowInvalidYear, 1, 1), false);
1784     QCOMPARE(calendar->isValid(highInvalidYear, 1, 1), false);
1785 
1786     // min/max month
1787     QCOMPARE(calendar->isValid(1, 0, 1), false);
1788     QCOMPARE(calendar->isValid(1, highInvalidMonth, 1), false);
1789 
1790     // min/max day
1791     QCOMPARE(calendar->isValid(1, 1, 0), false);
1792     QCOMPARE(calendar->isValid(1, 1, highInvalidDay), false);
1793 
1794     QCOMPARE(calendar->isValid(1, 1,  1), true);
1795 
1796     QCOMPARE(calendar->isValid(invalidDate), false);
1797     QCOMPARE(calendar->isValid(QDate(2000, 1, 1)), true);
1798 }
1799 
1800 void KCalendarTest::testEpoch(const KCalendarSystem *calendar, int y, int m, int d, int jd)
1801 {
1802     QCOMPARE(calendar->epoch(), QDate::fromJulianDay(jd));
1803     if (calendar->epoch() >= calendar->earliestValidDate()) {
1804         testYmd(calendar, y, m, d, jd);
1805     }
1806 }
1807 
1808 void KCalendarTest::testEarliestValidDate(const KCalendarSystem *calendar, int y, int m, int d, int jd)
1809 {
1810     QCOMPARE(calendar->earliestValidDate(), QDate::fromJulianDay(jd));
1811     testYmd(calendar, y, m, d, jd);
1812 }
1813 
1814 void KCalendarTest::testLatestValidDate(const KCalendarSystem *calendar, int y, int m, int d, int jd)
1815 {
1816     QCOMPARE(calendar->latestValidDate(), QDate::fromJulianDay(jd));
1817     testYmd(calendar, y, m, d, jd);
1818 }
1819 
1820 void KCalendarTest::testYmd(const KCalendarSystem *calendar, int y, int m, int d, int jd)
1821 {
1822     QDate testDate1 = QDate::fromJulianDay(jd);
1823     QCOMPARE(calendar->year(testDate1), y);
1824     QCOMPARE(calendar->month(testDate1), m);
1825     QCOMPARE(calendar->day(testDate1), d);
1826 
1827     QDate testDate2;
1828     QVERIFY(calendar->setDate(testDate2, y, m, d));
1829     QCOMPARE(testDate1, testDate2);
1830 }
1831 
1832 void KCalendarTest::testYear(const KCalendarSystem *calendar, const QDate &date,
1833                              int year, const QString &shortString, const QString &longString)
1834 {
1835     QCOMPARE(calendar->year(date), year);
1836     QCOMPARE(calendar->yearString(date, KCalendarSystem::ShortFormat), shortString);
1837     QCOMPARE(calendar->yearString(date, KCalendarSystem::LongFormat), longString);
1838     int i;
1839     QCOMPARE(calendar->yearStringToInteger(longString, i), year);
1840     QCOMPARE(i, longString.length());
1841 }
1842 
1843 void KCalendarTest::testMonth(const KCalendarSystem *calendar, const QDate &date,
1844                               int month, const QString &shortString, const QString &longString)
1845 {
1846     QCOMPARE(calendar->month(date), month);
1847     QCOMPARE(calendar->monthString(date, KCalendarSystem::ShortFormat), shortString);
1848     QCOMPARE(calendar->monthString(date, KCalendarSystem::LongFormat), longString);
1849     int i;
1850     QCOMPARE(calendar->monthStringToInteger(longString, i), month);
1851     QCOMPARE(i, longString.length());
1852 }
1853 
1854 void KCalendarTest::testDay(const KCalendarSystem *calendar, const QDate &date,
1855                             int day, const QString &shortString, const QString &longString)
1856 {
1857     QCOMPARE(calendar->day(date), day);
1858     QCOMPARE(calendar->dayString(date, KCalendarSystem::ShortFormat), shortString);
1859     QCOMPARE(calendar->dayString(date, KCalendarSystem::LongFormat), longString);
1860     int i;
1861     QCOMPARE(calendar->dayStringToInteger(longString, i), day);
1862     QCOMPARE(i, longString.length());
1863 }
1864 
1865 void KCalendarTest::testEraDate(const KCalendarSystem *calendar, const QDate &date,
1866                                 int yearInEra, const QString &shortYearInEraString, const QString &longYearInEraString,
1867                                 const QString &shortEraName, const QString &longEraName)
1868 {
1869     QCOMPARE(calendar->yearInEra(date),                                     yearInEra);
1870     QCOMPARE(calendar->yearInEraString(date),                               shortYearInEraString);
1871     QCOMPARE(calendar->yearInEraString(date, KCalendarSystem::ShortFormat), shortYearInEraString);
1872     QCOMPARE(calendar->yearInEraString(date, KCalendarSystem::LongFormat),  longYearInEraString);
1873     QCOMPARE(calendar->eraName(date),                                       shortEraName);
1874     QCOMPARE(calendar->eraName(date, KCalendarSystem::ShortFormat),         shortEraName);
1875     QCOMPARE(calendar->eraName(date, KCalendarSystem::LongFormat),          longEraName);
1876 }
1877 
1878 // Pass in the week day number, the equivalent QDate, and the short and long day names
1879 //  e.g. testWeekDayName( cal, 6, QDate(y,m,d), QString("Monday"), QString("Mon") )
1880 
1881 void KCalendarTest::testWeekDayName(const KCalendarSystem *calendar, int weekDay, const QDate &date,
1882                                     const QString &narrowName, const QString &shortName, const QString &longName)
1883 {
1884     QCOMPARE(calendar->weekDayName(weekDay, KCalendarSystem::ShortDayName), shortName);
1885     QCOMPARE(calendar->weekDayName(weekDay, KCalendarSystem::LongDayName), longName);
1886     QCOMPARE(calendar->weekDayName(weekDay), longName);
1887 
1888     // Test week day < min
1889     QCOMPARE(calendar->weekDayName(0, KCalendarSystem::ShortDayName), QString());
1890     QCOMPARE(calendar->weekDayName(0, KCalendarSystem::LongDayName), QString());
1891 
1892     // Test week day > max
1893     QCOMPARE(calendar->weekDayName(calendar->daysInWeek(date) + 1, KCalendarSystem::ShortDayName), QString());
1894     QCOMPARE(calendar->weekDayName(calendar->daysInWeek(date) + 1, KCalendarSystem::LongDayName), QString());
1895 
1896     // QDate parameter tests, so always in valid range
1897     QCOMPARE(calendar->weekDayName(date, KCalendarSystem::ShortDayName), shortName);
1898     QCOMPARE(calendar->weekDayName(date, KCalendarSystem::LongDayName), longName);
1899     QCOMPARE(calendar->weekDayName(date), longName);
1900 
1901     // Test format date componant
1902     QCOMPARE(calendar->formatDate(date, KLocale::DayOfWeekName, KLocale::NarrowName), narrowName);
1903     QCOMPARE(calendar->formatDate(date, KLocale::DayOfWeekName, KLocale::ShortName), shortName);
1904     QCOMPARE(calendar->formatDate(date, KLocale::DayOfWeekName, KLocale::LongName), longName);
1905 }
1906 
1907 void KCalendarTest::testMonthName(const KCalendarSystem *calendar, int month, int year, const QDate &date,
1908                                   const QString &narrowName, const QString &shortName, const QString &longName,
1909                                   const QString &shortNamePossessive, const QString &longNamePossessive)
1910 {
1911     QCOMPARE(calendar->monthName(month, year, KCalendarSystem::ShortName), shortName);
1912     QCOMPARE(calendar->monthName(month, year, KCalendarSystem::LongName), longName);
1913     QCOMPARE(calendar->monthName(month, year, KCalendarSystem::ShortNamePossessive),
1914              shortNamePossessive);
1915     QCOMPARE(calendar->monthName(month, year, KCalendarSystem::LongNamePossessive),
1916              longNamePossessive);
1917     QCOMPARE(calendar->monthName(month, year), longName);
1918 
1919     // Test month < min
1920     QCOMPARE(calendar->monthName(0, year, KCalendarSystem::ShortName), QString());
1921     QCOMPARE(calendar->monthName(0, year, KCalendarSystem::LongName),  QString());
1922     QCOMPARE(calendar->monthName(0, year, KCalendarSystem::ShortName), QString());
1923     QCOMPARE(calendar->monthName(0, year, KCalendarSystem::LongName),  QString());
1924 
1925     // Test month > max
1926     QCOMPARE(calendar->monthName(calendar->monthsInYear(date) + 1, year,
1927                                  KCalendarSystem::ShortName), QString());
1928     QCOMPARE(calendar->monthName(calendar->monthsInYear(date) + 1, year,
1929                                  KCalendarSystem::LongName), QString());
1930     QCOMPARE(calendar->monthName(calendar->monthsInYear(date) + 1, year,
1931                                  KCalendarSystem::ShortName), QString());
1932     QCOMPARE(calendar->monthName(calendar->monthsInYear(date) + 1, year,
1933                                  KCalendarSystem::LongName), QString());
1934 
1935     // QDate parameter tests, so always in valid range
1936     QCOMPARE(calendar->monthName(date, KCalendarSystem::ShortName), shortName);
1937     QCOMPARE(calendar->monthName(date, KCalendarSystem::LongName), longName);
1938     QCOMPARE(calendar->monthName(date, KCalendarSystem::ShortNamePossessive),
1939              shortNamePossessive);
1940     QCOMPARE(calendar->monthName(date, KCalendarSystem::LongNamePossessive),
1941              longNamePossessive);
1942     QCOMPARE(calendar->monthName(date), longName);
1943 
1944     // Test format date componant
1945     QCOMPARE(calendar->formatDate(date, KLocale::MonthName, KLocale::NarrowName), narrowName);
1946     QCOMPARE(calendar->formatDate(date, KLocale::MonthName, KLocale::ShortName), shortName);
1947     QCOMPARE(calendar->formatDate(date, KLocale::MonthName, KLocale::LongName), longName);
1948 }
1949 
1950 void KCalendarTest::testRoundTrip(const KCalendarSystem *calendar, bool testFullRange)
1951 {
1952     qDebug() << "Testing round trip of dates for Calendar System " << calendar->calendarLabel();
1953     qDebug() << "This may take some time, or you may have created an infinite loop.";
1954     qDebug() << "Uncomment the loop debug message to see each date comparison.";
1955 
1956     int testYear, testMonth, testDay;
1957     QDate testDate;
1958     // Limit to a short interesting date range for regular testing
1959     // Later add an Env var to choose full test whenever the formulas are changed.
1960     QDate loopDate, terminateDate;
1961     if (testFullRange) {
1962         loopDate = calendar->earliestValidDate();
1963         terminateDate = calendar->latestValidDate();
1964     } else {
1965         loopDate = qMax(QDate(1990, 1, 1), calendar->earliestValidDate());
1966         terminateDate = qMin(QDate(2020, 1, 1), calendar->latestValidDate());
1967     }
1968     QByteArray msg;
1969     while (loopDate <= terminateDate) {
1970         testYear = calendar->year(loopDate);
1971         testMonth = calendar->month(loopDate);
1972         testDay = calendar->day(loopDate);
1973         calendar->setDate(testDate, testYear, testMonth, testDay);
1974         // Uncomment this to see each comparison printed
1975         // or
1976         // Uncomment the QEXPECT_FAIL statements to check all dates regardless of occasional failures
1977         QByteArray msg = QByteArray::number(loopDate.toJulianDay()) + " = " +
1978                          QByteArray::number(testYear) + '-' +
1979                          QByteArray::number(testMonth) + '-' +
1980                          QByteArray::number(testDay) + " = " +
1981                          QByteArray::number(testDate.toJulianDay());
1982         /*
1983         // qDebug() << msg;
1984         */
1985         if (testMonth <= 0 || testDay <= 0) {   // year <= 0 is OK
1986             msg.prepend("Round Trip : JD to Date failed : ");
1987             QEXPECT_FAIL("", msg.data(), Continue);
1988         } else if (testDate.toJulianDay() == 0) {
1989             msg.prepend("Round Trip : Date to JD failed : ");
1990             QEXPECT_FAIL("", msg.data(), Continue);
1991         } else if (loopDate != testDate) {
1992             msg.prepend("Round Trip : JD's differ       : ");
1993             QEXPECT_FAIL("", msg.data(), Continue);
1994         }
1995         QCOMPARE(loopDate.toJulianDay(), testDate.toJulianDay());
1996         loopDate = loopDate.addDays(1);
1997     }
1998 }
1999 
2000 void KCalendarTest::testGregorianAddYears()
2001 {
2002     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
2003 
2004     QDate testDate;
2005 
2006     calendar->setDate(testDate, 2000,  1,  1);
2007     QCOMPARE(calendar->addYears(testDate, -1),  testDate.addYears(-1));
2008     QCOMPARE(calendar->addYears(testDate,  1),  testDate.addYears(1));
2009 
2010     calendar->setDate(testDate, 2000,  2, 29);
2011     QCOMPARE(calendar->addYears(testDate,  1),  testDate.addYears(1));
2012 
2013     calendar->setDate(testDate, -2000,  1,  1);
2014     QCOMPARE(calendar->addYears(testDate, -1),  testDate.addYears(-1));
2015     QCOMPARE(calendar->addYears(testDate,  1),  testDate.addYears(1));
2016 
2017     delete calendar;
2018 }
2019 
2020 void KCalendarTest::testGregorianAddMonths()
2021 {
2022     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
2023 
2024     QDate testDate;
2025 
2026     calendar->setDate(testDate, 2000,  1,  1);
2027     QCOMPARE(calendar->addMonths(testDate, -1),  testDate.addMonths(-1));
2028     QCOMPARE(calendar->addMonths(testDate,  1),  testDate.addMonths(1));
2029 
2030     calendar->setDate(testDate, 2000,  3,  1);
2031     QCOMPARE(calendar->addMonths(testDate, -1),  testDate.addMonths(-1));
2032     QCOMPARE(calendar->addMonths(testDate,  1),  testDate.addMonths(1));
2033 
2034     calendar->setDate(testDate, 2000, 12,  1);
2035     QCOMPARE(calendar->addMonths(testDate, -1),  testDate.addMonths(-1));
2036     QCOMPARE(calendar->addMonths(testDate,  1),  testDate.addMonths(1));
2037 
2038     calendar->setDate(testDate, 2000,  1,  1);
2039     QCOMPARE(calendar->addMonths(testDate, -30),  testDate.addMonths(-30));
2040     QCOMPARE(calendar->addMonths(testDate,  30),  testDate.addMonths(30));
2041 
2042     calendar->setDate(testDate, -2000,  1,  1);
2043     QCOMPARE(calendar->addMonths(testDate, -1),  testDate.addMonths(-1));
2044     QCOMPARE(calendar->addMonths(testDate,  1),  testDate.addMonths(1));
2045 
2046     calendar->setDate(testDate, -2000,  3,  1);
2047     QCOMPARE(calendar->addMonths(testDate, -1),  testDate.addMonths(-1));
2048     QCOMPARE(calendar->addMonths(testDate,  1),  testDate.addMonths(1));
2049 
2050     calendar->setDate(testDate, -2000, 12,  1);
2051     QCOMPARE(calendar->addMonths(testDate, -1),  testDate.addMonths(-1));
2052     QCOMPARE(calendar->addMonths(testDate,  1),  testDate.addMonths(1));
2053 
2054     calendar->setDate(testDate, -2000,  1,  1);
2055     QCOMPARE(calendar->addMonths(testDate, -30),  testDate.addMonths(-30));
2056     QCOMPARE(calendar->addMonths(testDate,  30),  testDate.addMonths(30));
2057 
2058     delete calendar;
2059 }
2060 
2061 void KCalendarTest::testGregorianAddDays()
2062 {
2063     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
2064 
2065     QDate testDate;
2066 
2067     calendar->setDate(testDate, 2000,  1,  1);
2068     QCOMPARE(calendar->addDays(testDate, -1),  testDate.addDays(-1));
2069     QCOMPARE(calendar->addDays(testDate,  1),  testDate.addDays(1));
2070 
2071     calendar->setDate(testDate, -2000,  1,  1);
2072     QCOMPARE(calendar->addDays(testDate, -1),  testDate.addDays(-1));
2073     QCOMPARE(calendar->addDays(testDate,  1),  testDate.addDays(1));
2074 
2075     delete calendar;
2076 }
2077 
2078 void KCalendarTest::testGregorianDaysInYear()
2079 {
2080     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
2081 
2082     QDate testDate;
2083 
2084     calendar->setDate(testDate, 1900, 1, 1);
2085     QCOMPARE(calendar->daysInYear(testDate),  testDate.daysInYear());
2086     calendar->setDate(testDate, 1999, 1, 1);
2087     QCOMPARE(calendar->daysInYear(testDate),  testDate.daysInYear());
2088     calendar->setDate(testDate, 2000, 1, 1);
2089     QCOMPARE(calendar->daysInYear(testDate),  testDate.daysInYear());
2090     calendar->setDate(testDate, 2001, 1, 1);
2091     QCOMPARE(calendar->daysInYear(testDate),  testDate.daysInYear());
2092     calendar->setDate(testDate, 2002, 1, 1);
2093     QCOMPARE(calendar->daysInYear(testDate),  testDate.daysInYear());
2094     calendar->setDate(testDate, 2003, 1, 1);
2095     QCOMPARE(calendar->daysInYear(testDate),  testDate.daysInYear());
2096     calendar->setDate(testDate, 2004, 1, 1);
2097     QCOMPARE(calendar->daysInYear(testDate),  testDate.daysInYear());
2098     calendar->setDate(testDate, 2005, 1, 1);
2099     QCOMPARE(calendar->daysInYear(testDate),  testDate.daysInYear());
2100 
2101     calendar->setDate(testDate, -4700, 1, 1);
2102 #if QT_VERSION < 0x040601
2103     QEXPECT_FAIL("", "Returns 365 instead of 366", Continue); // Fix submitted to Qt
2104 #endif
2105     QCOMPARE(calendar->daysInYear(testDate),  testDate.daysInYear());
2106     calendar->setDate(testDate, -4000, 1, 1);
2107 #if QT_VERSION < 0x040601
2108     QEXPECT_FAIL("", "Returns 365 instead of 366", Continue); // Fix submitted to Qt
2109 #endif
2110     QCOMPARE(calendar->daysInYear(testDate),  testDate.daysInYear());
2111 
2112     calendar->setDate(testDate, 1, 1, 1);
2113     QCOMPARE(calendar->daysInYear(testDate),  testDate.daysInYear());
2114 
2115     calendar->setDate(testDate, 9996, 1, 1);
2116     QCOMPARE(calendar->daysInYear(testDate),  testDate.daysInYear());
2117     calendar->setDate(testDate, 9999, 1, 1);
2118     QCOMPARE(calendar->daysInYear(testDate),  testDate.daysInYear());
2119 
2120     delete calendar;
2121 }
2122 
2123 void KCalendarTest::testGregorianDaysInMonth()
2124 {
2125     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
2126 
2127     QDate testDate;
2128 
2129     // Test all months
2130     calendar->setDate(testDate, 2000,  1, 1);
2131     QCOMPARE(calendar->daysInMonth(testDate), testDate.daysInMonth());
2132     calendar->setDate(testDate, 2000,  2, 1);
2133     QCOMPARE(calendar->daysInMonth(testDate), testDate.daysInMonth());
2134     calendar->setDate(testDate, 2000,  3, 1);
2135     QCOMPARE(calendar->daysInMonth(testDate), testDate.daysInMonth());
2136     calendar->setDate(testDate, 2000,  4, 1);
2137     QCOMPARE(calendar->daysInMonth(testDate), testDate.daysInMonth());
2138     calendar->setDate(testDate, 2000,  5, 1);
2139     QCOMPARE(calendar->daysInMonth(testDate), testDate.daysInMonth());
2140     calendar->setDate(testDate, 2000,  6, 1);
2141     QCOMPARE(calendar->daysInMonth(testDate), testDate.daysInMonth());
2142     calendar->setDate(testDate, 2000,  7, 1);
2143     QCOMPARE(calendar->daysInMonth(testDate), testDate.daysInMonth());
2144     calendar->setDate(testDate, 2000,  8, 1);
2145     QCOMPARE(calendar->daysInMonth(testDate), testDate.daysInMonth());
2146     calendar->setDate(testDate, 2000,  9, 1);
2147     QCOMPARE(calendar->daysInMonth(testDate), testDate.daysInMonth());
2148     calendar->setDate(testDate, 2000, 10, 1);
2149     QCOMPARE(calendar->daysInMonth(testDate), testDate.daysInMonth());
2150     calendar->setDate(testDate, 2000, 11, 1);
2151     QCOMPARE(calendar->daysInMonth(testDate), testDate.daysInMonth());
2152     calendar->setDate(testDate, 2000, 12, 1);
2153     QCOMPARE(calendar->daysInMonth(testDate), testDate.daysInMonth());
2154 
2155     // Test Feb in leap and normal years
2156     calendar->setDate(testDate, 2000, 2, 1);
2157     QCOMPARE(calendar->daysInMonth(testDate), testDate.daysInMonth());
2158     calendar->setDate(testDate, 2001, 2, 1);
2159     QCOMPARE(calendar->daysInMonth(testDate), testDate.daysInMonth());
2160 
2161     // Test max date
2162     calendar->setDate(testDate, 9999, 12, 1);
2163     QCOMPARE(calendar->daysInMonth(testDate), testDate.daysInMonth());
2164 
2165     delete calendar;
2166 }
2167 
2168 void KCalendarTest::testGregorianDayOfYear()
2169 {
2170     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
2171 
2172     QDate testDate;
2173 
2174     calendar->setDate(testDate, 2000,  1,  1);
2175     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2176     calendar->setDate(testDate, 2000,  2, 29);
2177     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2178     calendar->setDate(testDate, 2000,  6,  1);
2179     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2180     calendar->setDate(testDate, 2000, 12,  1);
2181     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2182     calendar->setDate(testDate, 2000, 12, 31);
2183     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2184 
2185     calendar->setDate(testDate, -2000,  1,  1);
2186     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2187     calendar->setDate(testDate, -2000,  2, 28);
2188     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2189     calendar->setDate(testDate, -2000,  6,  1);
2190     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2191     calendar->setDate(testDate, -2000, 12,  1);
2192     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2193     calendar->setDate(testDate, -2000, 12, 31);
2194     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2195 
2196     calendar->setDate(testDate, -4712,  1,  2);
2197     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2198     calendar->setDate(testDate, -4712,  2, 28);
2199     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2200     calendar->setDate(testDate, -4712,  6,  1);
2201     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2202     calendar->setDate(testDate, -4712, 12,  1);
2203     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2204     calendar->setDate(testDate, -4712, 12, 31);
2205     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2206 
2207     calendar->setDate(testDate, 9999,  1,  1);
2208     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2209     //QDate::dayOfYear doesn't check if invalid!!!
2210     //calendar->setDate( testDate, 9999,  2, 29 );
2211     //QCOMPARE( calendar->dayOfYear( testDate ), testDate.dayOfYear() );
2212     calendar->setDate(testDate, 9999,  6,  1);
2213     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2214     calendar->setDate(testDate, 9999, 12,  1);
2215     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2216     calendar->setDate(testDate, 9999, 12, 31);
2217     QCOMPARE(calendar->dayOfYear(testDate), testDate.dayOfYear());
2218 
2219     delete calendar;
2220 }
2221 
2222 void KCalendarTest::testGregorianDayOfWeek()
2223 {
2224     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
2225 
2226     QDate testDate;
2227 
2228     calendar->setDate(testDate, 2000,  1,  1);
2229     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2230     calendar->setDate(testDate, 2000,  1,  2);
2231     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2232     calendar->setDate(testDate, 2000,  1,  3);
2233     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2234     calendar->setDate(testDate, 2000,  1,  4);
2235     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2236     calendar->setDate(testDate, 2000,  1,  5);
2237     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2238     calendar->setDate(testDate, 2000,  1,  6);
2239     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2240     calendar->setDate(testDate, 2000,  1,  7);
2241     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2242 
2243     calendar->setDate(testDate, -4712,  1,  2);
2244     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2245     calendar->setDate(testDate, -4712,  1,  3);
2246     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2247     calendar->setDate(testDate, -4712,  1,  4);
2248     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2249     calendar->setDate(testDate, -4712,  1,  5);
2250     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2251     calendar->setDate(testDate, -4712,  1,  6);
2252     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2253     calendar->setDate(testDate, -4712,  1,  7);
2254     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2255     calendar->setDate(testDate, -4712,  1,  8);
2256     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2257 
2258     calendar->setDate(testDate, 9999,  1,  1);
2259     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2260     calendar->setDate(testDate, 9999,  1,  2);
2261     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2262     calendar->setDate(testDate, 9999,  1,  3);
2263     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2264     calendar->setDate(testDate, 9999,  1,  4);
2265     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2266     calendar->setDate(testDate, 9999,  1,  5);
2267     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2268     calendar->setDate(testDate, 9999,  1,  6);
2269     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2270     calendar->setDate(testDate, 9999,  1,  7);
2271     QCOMPARE(calendar->dayOfWeek(testDate), testDate.dayOfWeek());
2272 
2273     delete calendar;
2274 }
2275 
2276 // Don't really need this as Gregorian currently uses QDate directly
2277 void KCalendarTest::testGregorianIsLeapYear()
2278 {
2279     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar);
2280 
2281     QDate testDate;
2282 
2283     calendar->setDate(testDate, 2000,  1,  1);
2284     QCOMPARE(calendar->isLeapYear(2000),     testDate.isLeapYear(2000));
2285     QCOMPARE(calendar->isLeapYear(testDate), testDate.isLeapYear(2000));
2286     calendar->setDate(testDate, 2001,  1,  1);
2287     QCOMPARE(calendar->isLeapYear(2001),     testDate.isLeapYear(2001));
2288     QCOMPARE(calendar->isLeapYear(testDate), testDate.isLeapYear(2001));
2289     calendar->setDate(testDate, 2004,  1,  1);
2290     QCOMPARE(calendar->isLeapYear(2004),     testDate.isLeapYear(2004));
2291     QCOMPARE(calendar->isLeapYear(testDate), testDate.isLeapYear(2004));
2292 
2293     calendar->setDate(testDate, 1900,  1,  1);
2294     QCOMPARE(calendar->isLeapYear(1900),     testDate.isLeapYear(1900));
2295     QCOMPARE(calendar->isLeapYear(testDate), testDate.isLeapYear(1900));
2296     calendar->setDate(testDate, 1901,  1,  1);
2297     QCOMPARE(calendar->isLeapYear(1901),     testDate.isLeapYear(1901));
2298     QCOMPARE(calendar->isLeapYear(testDate), testDate.isLeapYear(1901));
2299     calendar->setDate(testDate, 1904,  1,  1);
2300     QCOMPARE(calendar->isLeapYear(1904),     testDate.isLeapYear(1904));
2301     QCOMPARE(calendar->isLeapYear(testDate), testDate.isLeapYear(1904));
2302 
2303     calendar->setDate(testDate, -2000,  1,  1);
2304     QCOMPARE(calendar->isLeapYear(-2000),    testDate.isLeapYear(-2000));
2305     QCOMPARE(calendar->isLeapYear(testDate), testDate.isLeapYear(-2000));
2306     calendar->setDate(testDate, -2001,  1,  1);
2307     QCOMPARE(calendar->isLeapYear(-2001),    testDate.isLeapYear(-2001));
2308     QCOMPARE(calendar->isLeapYear(testDate), testDate.isLeapYear(-2001));
2309     calendar->setDate(testDate, -2004,  1,  1);
2310     QCOMPARE(calendar->isLeapYear(-2004),    testDate.isLeapYear(-2004));
2311     QCOMPARE(calendar->isLeapYear(testDate), testDate.isLeapYear(-2004));
2312 
2313     delete calendar;
2314 }
2315 
2316 void KCalendarTest::testKLocalizedDate()
2317 {
2318     KLocale::global()->setDateFormatShort("%y-%m-%d");
2319     const KCalendarSystem *calendar = KLocale::global()->calendar();
2320 
2321     // Set up a bunch of KLocalizedDates and QDates that should be equal
2322     QDate testQDate(2010, 1, 1);
2323     KLocalizedDate testKLocalizedDate(testQDate);
2324 
2325     // Test KLocalizedDate output calls not in KCalendarSystem
2326     QCOMPARE(KLocalizedDate().isNull(),        true);
2327     QCOMPARE(KLocalizedDate().isValid(),       false);
2328     QCOMPARE(testKLocalizedDate.isNull(),      testQDate.isNull());
2329     QCOMPARE(testKLocalizedDate.date(),        testQDate);
2330     QCOMPARE((qint64)testKLocalizedDate.toJulianDay(), (qint64)testQDate.toJulianDay());
2331     testKLocalizedDate.setDate(QDate(2010, 6, 1));
2332     testQDate.setDate(2010, 6, 1);
2333     QCOMPARE(testKLocalizedDate.date(),        testQDate);
2334 
2335     // Test static KLocalizedDate methods
2336     QCOMPARE(KLocalizedDate::currentDate().date(),           QDate::currentDate());
2337     QCOMPARE(KLocalizedDate::fromDate(testQDate).date(),   testQDate);
2338     QCOMPARE(KLocalizedDate::fromJulianDay(100000).date(), QDate::fromJulianDay(100000));
2339 
2340     // Compare KLocalizedDate output to KCalendarSystem output calls
2341     QCOMPARE(testKLocalizedDate.isValid(),                     calendar->isValid(testQDate));
2342     QCOMPARE(testKLocalizedDate.year(),                        calendar->year(testQDate));
2343     QCOMPARE(testKLocalizedDate.month(),                       calendar->month(testQDate));
2344     QCOMPARE(testKLocalizedDate.day(),                         calendar->day(testQDate));
2345     QCOMPARE(testKLocalizedDate.eraName(),                     calendar->eraName(testQDate));
2346     QCOMPARE(testKLocalizedDate.eraYear(),                     calendar->eraYear(testQDate));
2347     QCOMPARE(testKLocalizedDate.yearInEra(),                   calendar->yearInEra(testQDate));
2348     QCOMPARE(testKLocalizedDate.addYears(5).date(),          calendar->addYears(testQDate, 5));
2349     QCOMPARE(testKLocalizedDate.addMonths(5).date(),         calendar->addMonths(testQDate, 5));
2350     QCOMPARE(testKLocalizedDate.addDays(5).date(),           calendar->addDays(testQDate, 5));
2351     QCOMPARE(testKLocalizedDate.monthsInYear(),                calendar->monthsInYear(testQDate));
2352     QCOMPARE(testKLocalizedDate.weeksInYear(),                 calendar->weeksInYear(testQDate));
2353     QCOMPARE(testKLocalizedDate.daysInYear(),                  calendar->daysInYear(testQDate));
2354     QCOMPARE(testKLocalizedDate.daysInMonth(),                 calendar->daysInMonth(testQDate));
2355     QCOMPARE(testKLocalizedDate.daysInWeek(),                  calendar->daysInWeek(testQDate));
2356     QCOMPARE(testKLocalizedDate.dayOfYear(),                   calendar->dayOfYear(testQDate));
2357     QCOMPARE(testKLocalizedDate.dayOfWeek(),                   calendar->dayOfWeek(testQDate));
2358     QCOMPARE(testKLocalizedDate.week(),                        calendar->week(testQDate));
2359     QCOMPARE(testKLocalizedDate.isLeapYear(),                  calendar->isLeapYear(testQDate));
2360     QCOMPARE(testKLocalizedDate.firstDayOfYear().date(),       calendar->firstDayOfYear(testQDate));
2361     QCOMPARE(testKLocalizedDate.lastDayOfYear().date(),        calendar->lastDayOfYear(testQDate));
2362     QCOMPARE(testKLocalizedDate.firstDayOfMonth().date(),      calendar->firstDayOfMonth(testQDate));
2363     QCOMPARE(testKLocalizedDate.lastDayOfMonth().date(),       calendar->lastDayOfMonth(testQDate));
2364 
2365     QCOMPARE(testKLocalizedDate.week(KLocale::IsoWeekNumber),        calendar->week(testQDate, KLocale::IsoWeekNumber));
2366     QCOMPARE(testKLocalizedDate.weeksInYear(KLocale::IsoWeekNumber), calendar->weeksInYear(testQDate, KLocale::IsoWeekNumber));
2367 
2368     // Compare KLocalizedDate setDate to KCalendarSystem setDate
2369     testKLocalizedDate.setDate(2010, 3, 1);
2370     calendar->setDate(testQDate, 2010, 3, 1);
2371     QCOMPARE(testKLocalizedDate.date(), testQDate);
2372     testKLocalizedDate.setDate(2010, 100);
2373     calendar->setDate(testQDate, 2010, 100);
2374     QCOMPARE(testKLocalizedDate.date(), testQDate);
2375     testKLocalizedDate.setDate("AD", 2010, 9, 1);
2376     calendar->setDate(testQDate, "AD", 2010, 9, 1);
2377     QCOMPARE(testKLocalizedDate.date(), testQDate);
2378     testKLocalizedDate.setDate(KLocale::IsoWeekNumber, 2010, 15, 1);
2379     calendar->setDateIsoWeek(testQDate, 2010, 15, 1);
2380     QCOMPARE(testKLocalizedDate.date(), testQDate);
2381     testKLocalizedDate.setCurrentDate();
2382     testQDate = QDate::currentDate();
2383     QCOMPARE(testKLocalizedDate.date(), testQDate);
2384 
2385     // Compare KLocalizedDate getDate to KCalendarSystem getDate
2386     int ky, km, kd, qy, qm, qd;
2387     testKLocalizedDate.getDate(&ky, &km, &kd);
2388     calendar->getDate(testQDate, &qy, &qm, &qd);
2389     QCOMPARE(ky, qy);
2390     QCOMPARE(km, qm);
2391     QCOMPARE(kd, qd);
2392 
2393     // Compare KLocalizedDate format/read to KCalendarSystem format
2394     QCOMPARE(testKLocalizedDate.formatDate(),           calendar->formatDate(testQDate));
2395     QCOMPARE(testKLocalizedDate.formatDate("%d-%m-%y"), calendar->formatDate(testQDate, "%d-%m-%y"));
2396     QCOMPARE(testKLocalizedDate.formatDate(KLocale::Year),              calendar->yearString(testQDate));
2397     QCOMPARE(testKLocalizedDate.formatDate(KLocale::YearName),          calendar->yearString(testQDate));
2398     QCOMPARE(testKLocalizedDate.formatDate(KLocale::Month),             calendar->monthString(testQDate, KCalendarSystem::ShortFormat));
2399     QCOMPARE(testKLocalizedDate.formatDate(KLocale::MonthName),         calendar->monthName(testQDate));
2400     QCOMPARE(testKLocalizedDate.formatDate(KLocale::Day),               calendar->dayString(testQDate, KCalendarSystem::ShortFormat));
2401     QCOMPARE(testKLocalizedDate.formatDate(KLocale::DayName),           calendar->dayString(testQDate, KCalendarSystem::ShortFormat));
2402     QCOMPARE(testKLocalizedDate.formatDate(KLocale::JulianDay),         QString::number(testQDate.toJulianDay()));
2403     QCOMPARE(testKLocalizedDate.formatDate(KLocale::EraName),           calendar->eraName(testQDate));
2404     QCOMPARE(testKLocalizedDate.formatDate(KLocale::EraYear),           calendar->eraYear(testQDate));
2405     QCOMPARE(testKLocalizedDate.formatDate(KLocale::YearInEra),         calendar->yearInEraString(testQDate));
2406     QCOMPARE(testKLocalizedDate.formatDate(KLocale::DayOfYear),         calendar->dayOfYearString(testQDate, KCalendarSystem::ShortFormat));
2407     QCOMPARE(testKLocalizedDate.formatDate(KLocale::DayOfYearName),     calendar->dayOfYearString(testQDate, KCalendarSystem::ShortFormat));
2408     QCOMPARE(testKLocalizedDate.formatDate(KLocale::DayOfWeek),         calendar->dayOfWeekString(testQDate));
2409     QCOMPARE(testKLocalizedDate.formatDate(KLocale::DayOfWeekName),     calendar->weekDayName(testQDate));
2410     QCOMPARE(testKLocalizedDate.formatDate(KLocale::Week),              calendar->weekNumberString(testQDate, KCalendarSystem::ShortFormat));
2411     int weekYear;
2412     QDate weekDate;
2413     calendar->weekNumber(testQDate, &weekYear);
2414     calendar->setDate(weekDate, weekYear, 1, 1);
2415     QCOMPARE(testKLocalizedDate.formatDate(KLocale::WeekYear),          calendar->yearString(weekDate));
2416     QCOMPARE(testKLocalizedDate.formatDate(KLocale::MonthsInYear),      calendar->monthsInYearString(testQDate));
2417     QCOMPARE(testKLocalizedDate.formatDate(KLocale::WeeksInYear),       calendar->weeksInYearString(testQDate));
2418     QCOMPARE(testKLocalizedDate.formatDate(KLocale::DaysInYear),        calendar->daysInYearString(testQDate));
2419     QCOMPARE(testKLocalizedDate.formatDate(KLocale::DaysInMonth),       calendar->daysInMonthString(testQDate));
2420     QCOMPARE(testKLocalizedDate.formatDate(KLocale::DaysInWeek),        calendar->daysInWeekString(testQDate));
2421 
2422     // Compare KLocalizedDate format/read to KCalendarSystem read
2423     QCOMPARE(testKLocalizedDate.readDate("2010-01-01").date(),                        calendar->readDate("2010-01-01"));
2424     QCOMPARE(testKLocalizedDate.readDate("2010-01-01", KLocale::NormalFormat).date(), calendar->readDate("2010-01-01", KLocale::NormalFormat));
2425     QCOMPARE(testKLocalizedDate.readDate("2010-01-01", "%y-%m-%d").date(),            calendar->readDate("2010-01-01", "%y-%m-%d"));
2426 
2427     // Test new addOn methods
2428     testKLocalizedDate.addYearsTo(5);
2429     testQDate = calendar->addYears(testQDate, 5);
2430     QCOMPARE(testKLocalizedDate.date(), testQDate);
2431     testKLocalizedDate.addMonthsTo(5);
2432     testQDate = calendar->addMonths(testQDate, 5);
2433     QCOMPARE(testKLocalizedDate.date(), testQDate);
2434     testKLocalizedDate.addDaysTo(5);
2435     testQDate = calendar->addDays(testQDate, 5);
2436     QCOMPARE(testKLocalizedDate.date(), testQDate);
2437 
2438     // Set up some dates for comparisons
2439     QDate lowQDate(2000, 1, 1);
2440     KLocalizedDate lowKLocalizedDate(lowQDate);
2441     QDate highQDate(2010, 1, 1);
2442     KLocalizedDate highKLocalizedDate(highQDate);
2443 
2444     // Compare the difference methods
2445     int kdy, kdm, kdd, kdir, qdy, qdm, qdd, qdir;
2446     lowKLocalizedDate.dateDifference(highKLocalizedDate, &kdy, &kdm, &kdd, &kdir);
2447     calendar->dateDifference(lowKLocalizedDate.date(), highKLocalizedDate.date(), &qdy, &qdm, &qdd, &qdir);
2448     QCOMPARE(kdy, qdy);
2449     QCOMPARE(kdm, qdm);
2450     QCOMPARE(kdd, qdd);
2451     QCOMPARE(kdir, qdir);
2452     lowKLocalizedDate.dateDifference(highQDate, &kdy, &kdm, &kdd, &kdir);
2453     calendar->dateDifference(lowKLocalizedDate.date(), highQDate, &qdy, &qdm, &qdd, &qdir);
2454     QCOMPARE(kdy, qdy);
2455     QCOMPARE(kdm, qdm);
2456     QCOMPARE(kdd, qdd);
2457     QCOMPARE(kdir, qdir);
2458     QCOMPARE(lowKLocalizedDate.yearsDifference(highKLocalizedDate),  calendar->yearsDifference(lowQDate, highQDate));
2459     QCOMPARE(lowKLocalizedDate.yearsDifference(highQDate),  calendar->yearsDifference(lowQDate, highQDate));
2460     QCOMPARE(lowKLocalizedDate.monthsDifference(highKLocalizedDate), calendar->monthsDifference(lowQDate, highQDate));
2461     QCOMPARE(lowKLocalizedDate.monthsDifference(highQDate), calendar->monthsDifference(lowQDate, highQDate));
2462     QCOMPARE(lowKLocalizedDate.daysDifference(highKLocalizedDate),   calendar->daysDifference(lowQDate, highQDate));
2463     QCOMPARE(lowKLocalizedDate.daysDifference(highQDate),   calendar->daysDifference(lowQDate, highQDate));
2464 
2465     // Test new operators
2466     QCOMPARE(lowKLocalizedDate == lowKLocalizedDate,   true);
2467     QCOMPARE(lowKLocalizedDate == highKLocalizedDate,  false);
2468     QCOMPARE(lowKLocalizedDate == lowQDate,   true);
2469     QCOMPARE(lowKLocalizedDate == highQDate,  false);
2470     QCOMPARE(lowKLocalizedDate != lowKLocalizedDate,   false);
2471     QCOMPARE(lowKLocalizedDate != highKLocalizedDate,  true);
2472     QCOMPARE(lowKLocalizedDate != lowQDate,   false);
2473     QCOMPARE(lowKLocalizedDate != highQDate,  true);
2474     QCOMPARE(highKLocalizedDate < lowKLocalizedDate,   false);
2475     QCOMPARE(lowKLocalizedDate  < highKLocalizedDate,  true);
2476     QCOMPARE(highKLocalizedDate < lowQDate,   false);
2477     QCOMPARE(lowKLocalizedDate  < highQDate,  true);
2478     QCOMPARE(highKLocalizedDate <= lowKLocalizedDate,  false);
2479     QCOMPARE(lowKLocalizedDate  <= lowKLocalizedDate,  true);
2480     QCOMPARE(lowKLocalizedDate  <= highKLocalizedDate, true);
2481     QCOMPARE(highKLocalizedDate <= lowQDate,  false);
2482     QCOMPARE(lowKLocalizedDate  <= lowQDate,  true);
2483     QCOMPARE(lowKLocalizedDate  <= highQDate, true);
2484     QCOMPARE(highKLocalizedDate > lowKLocalizedDate,   true);
2485     QCOMPARE(lowKLocalizedDate  > highKLocalizedDate,  false);
2486     QCOMPARE(highKLocalizedDate > lowQDate,   true);
2487     QCOMPARE(lowKLocalizedDate  > highQDate,  false);
2488     QCOMPARE(highKLocalizedDate >= lowKLocalizedDate,  true);
2489     QCOMPARE(lowKLocalizedDate  >= lowKLocalizedDate,  true);
2490     QCOMPARE(lowKLocalizedDate  >= highKLocalizedDate, false);
2491     QCOMPARE(highKLocalizedDate >= lowQDate,  true);
2492     QCOMPARE(lowKLocalizedDate  >= lowQDate,  true);
2493     QCOMPARE(lowKLocalizedDate  >= highQDate, false);
2494 
2495     // Test changing Calendar System
2496     KCalendarSystem *hijri = KCalendarSystem::create(KLocale::IslamicCivilCalendar);
2497     testKLocalizedDate.setCalendarSystem(KLocale::IslamicCivilCalendar);
2498     QCOMPARE(testKLocalizedDate.year(), hijri->year(testQDate));
2499 
2500     // Test copy of calendar object
2501     if (true) {
2502         KLocalizedDate test2 = testKLocalizedDate;
2503         test2.day();
2504     }
2505     testKLocalizedDate.day();
2506 
2507     KLocalizedDate newOne = testKLocalizedDate.currentDate();
2508     qDebug() << newOne;
2509     newOne = KLocalizedDate::currentDate();
2510     qDebug() << newOne;
2511 
2512     KLocalizedDate calendarDate(testQDate, calendar);
2513     QCOMPARE(calendarDate.date(), testQDate);
2514     QCOMPARE(calendarDate.day(), calendar->day(testQDate));
2515 
2516     KCalendarSystem *hijriCalendar =  KCalendarSystem::create(KLocale::IslamicCivilCalendar);
2517     KLocalizedDate hijriDate(testQDate, hijriCalendar);
2518     QCOMPARE(hijriDate.date(), testQDate);
2519     QCOMPARE(hijriDate.day(), hijriCalendar->day(testQDate));
2520     delete hijriCalendar;
2521 }
2522 
2523 void KCalendarTest::testWeekNumberSystem()
2524 {
2525     KLocale *locale = new KLocale(*KLocale::global());
2526     const KCalendarSystem *calendar = KCalendarSystem::create(KLocale::GregorianCalendar, locale);
2527     int weekYear = 0;
2528 
2529     // Test the 7 days the year can start on:
2530     // Monday    == 2018
2531     // Tuesday   == 2013
2532     // Wednesday == 2014
2533     // Thursday  == 2015
2534     // Friday    == 2016
2535     // Saturday  == 2011
2536     // Sunday    == 2012
2537 
2538     // ISO Week
2539     // Monday    == 2018 == Week 1 2018
2540     // Tuesday   == 2013 == Week 1 2013
2541     // Wednesday == 2014 == Week 1 2014
2542     // Thursday  == 2015 == Week 1 2015
2543     // Friday    == 2016 == Week 53 2015
2544     // Saturday  == 2011 == Week 52 2010
2545     // Sunday    == 2012 == Week 52 2011
2546 
2547     locale->setWeekStartDay(1);
2548     locale->setWeekNumberSystem(KLocale::IsoWeekNumber);
2549 
2550     QCOMPARE(calendar->week(QDate(2018, 1, 1), &weekYear), 1);
2551     QCOMPARE(weekYear, 2018);
2552     QCOMPARE(calendar->week(QDate(2013, 1, 1), &weekYear), 1);
2553     QCOMPARE(weekYear, 2013);
2554     QCOMPARE(calendar->week(QDate(2014, 1, 1), &weekYear), 1);
2555     QCOMPARE(weekYear, 2014);
2556     QCOMPARE(calendar->week(QDate(2015, 1, 1), &weekYear), 1);
2557     QCOMPARE(weekYear, 2015);
2558     QCOMPARE(calendar->week(QDate(2016, 1, 1), &weekYear), 53);
2559     QCOMPARE(weekYear, 2015);
2560     QCOMPARE(calendar->week(QDate(2011, 1, 1), &weekYear), 52);
2561     QCOMPARE(weekYear, 2010);
2562     QCOMPARE(calendar->week(QDate(2012, 1, 1), &weekYear), 52);
2563     QCOMPARE(weekYear, 2011);
2564 
2565     QCOMPARE(calendar->week(QDate(2014, 12, 31), &weekYear), 1);
2566     QCOMPARE(weekYear, 2015);
2567     QCOMPARE(calendar->week(QDate(2015, 12, 31), &weekYear), 53);
2568     QCOMPARE(weekYear, 2015);
2569 
2570     QCOMPARE(calendar->weeksInYear(2010), 52);
2571     QCOMPARE(calendar->weeksInYear(2015), 53);
2572 
2573     // First Full Week starting Sunday, aka US Week
2574     locale->setWeekStartDay(7);
2575     locale->setWeekNumberSystem(KLocale::FirstFullWeek);
2576 
2577     // Mon = Jan 1 2018 = Week 52 2017, Sun = Jan 7 = Week 1
2578     QCOMPARE(calendar->week(QDate(2018, 1, 1), &weekYear), 53);        //First Day
2579     QCOMPARE(weekYear, 2017);
2580     QCOMPARE(calendar->week(QDate(2018, 1, 6), &weekYear), 53);        //First Saturday
2581     QCOMPARE(weekYear, 2017);
2582     QCOMPARE(calendar->week(QDate(2018, 1, 7), &weekYear), 1);         //First Sunday
2583     QCOMPARE(weekYear, 2018);
2584     QCOMPARE(calendar->week(QDate(2018, 1, 13), &weekYear), 1);        //Second Saturday
2585     QCOMPARE(weekYear, 2018);
2586     QCOMPARE(calendar->week(QDate(2018, 1, 14), &weekYear), 2);        //Second Sunday
2587     QCOMPARE(weekYear, 2018);
2588     QCOMPARE(calendar->week(QDate(2018, 12, 31), &weekYear), 52);       //Last Day
2589     QCOMPARE(weekYear, 2018);
2590 
2591     // Tue = Jan 1 2013 = Week 52 2012, Sun = Jan 6 = Week 1
2592     QCOMPARE(calendar->week(QDate(2013, 1, 1), &weekYear), 53);        //First Day
2593     QCOMPARE(weekYear, 2012);
2594     QCOMPARE(calendar->week(QDate(2013, 1, 5), &weekYear), 53);        //First Saturday
2595     QCOMPARE(weekYear, 2012);
2596     QCOMPARE(calendar->week(QDate(2013, 1, 6), &weekYear), 1);         //First Sunday
2597     QCOMPARE(weekYear, 2013);
2598     QCOMPARE(calendar->week(QDate(2013, 1, 12), &weekYear), 1);        //Second Saturday
2599     QCOMPARE(weekYear, 2013);
2600     QCOMPARE(calendar->week(QDate(2013, 1, 13), &weekYear), 2);        //Second Sunday
2601     QCOMPARE(weekYear, 2013);
2602     QCOMPARE(calendar->week(QDate(2013, 12, 31), &weekYear), 52);       //Last Day
2603     QCOMPARE(weekYear, 2013);
2604 
2605     // Wed = Jan 1 2014 = Week 52 2013, Sun = Jan 5 = Week 1
2606     QCOMPARE(calendar->week(QDate(2014, 1, 1), &weekYear), 52);        //First Day
2607     QCOMPARE(weekYear, 2013);
2608     QCOMPARE(calendar->week(QDate(2014, 1, 4), &weekYear), 52);        //First Saturday
2609     QCOMPARE(weekYear, 2013);
2610     QCOMPARE(calendar->week(QDate(2014, 1, 5), &weekYear), 1);         //First Sunday
2611     QCOMPARE(weekYear, 2014);
2612     QCOMPARE(calendar->week(QDate(2014, 1, 11), &weekYear), 1);        //Second Saturday
2613     QCOMPARE(weekYear, 2014);
2614     QCOMPARE(calendar->week(QDate(2014, 1, 12), &weekYear), 2);        //Second Sunday
2615     QCOMPARE(weekYear, 2014);
2616     QCOMPARE(calendar->week(QDate(2014, 12, 31), &weekYear), 52);       //Last Day
2617     QCOMPARE(weekYear, 2014);
2618 
2619     // Thu = Jan 1 2015 = Week 52 2014, Sun = Jan 4 = Week 1
2620     QCOMPARE(calendar->week(QDate(2015, 1, 1), &weekYear), 52);        //First Day
2621     QCOMPARE(weekYear, 2014);
2622     QCOMPARE(calendar->week(QDate(2015, 1, 3), &weekYear), 52);        //First Saturday
2623     QCOMPARE(weekYear, 2014);
2624     QCOMPARE(calendar->week(QDate(2015, 1, 4), &weekYear), 1);         //First Sunday
2625     QCOMPARE(weekYear, 2015);
2626     QCOMPARE(calendar->week(QDate(2015, 1, 10), &weekYear), 1);        //Second Saturday
2627     QCOMPARE(weekYear, 2015);
2628     QCOMPARE(calendar->week(QDate(2015, 1, 11), &weekYear), 2);        //Second Sunday
2629     QCOMPARE(weekYear, 2015);
2630     QCOMPARE(calendar->week(QDate(2015, 12, 31), &weekYear), 52);       //Last Day
2631     QCOMPARE(weekYear, 2015);
2632 
2633     // Fri = Jan 1 2016 = Week 52 2015, Sun = Jan 3 = Week 1
2634     QCOMPARE(calendar->week(QDate(2016, 1, 1), &weekYear), 52);        //First Day
2635     QCOMPARE(weekYear, 2015);
2636     QCOMPARE(calendar->week(QDate(2016, 1, 2), &weekYear), 52);        //First Saturday
2637     QCOMPARE(weekYear, 2015);
2638     QCOMPARE(calendar->week(QDate(2016, 1, 3), &weekYear), 1);         //First Sunday
2639     QCOMPARE(weekYear, 2016);
2640     QCOMPARE(calendar->week(QDate(2016, 1, 9), &weekYear), 1);         //Second Saturday
2641     QCOMPARE(weekYear, 2016);
2642     QCOMPARE(calendar->week(QDate(2016, 1, 10), &weekYear), 2);        //Second Sunday
2643     QCOMPARE(weekYear, 2016);
2644     QCOMPARE(calendar->week(QDate(2016, 12, 31), &weekYear), 52);       //Last Day
2645     QCOMPARE(weekYear, 2016);
2646 
2647     // Sat = Jan 1 2011 = Week 52 2010, Sun = Jan 2 = Week 1
2648     QCOMPARE(calendar->week(QDate(2011, 1, 1), &weekYear), 52);        //First Day, First Saturday
2649     QCOMPARE(weekYear, 2010);
2650     QCOMPARE(calendar->week(QDate(2011, 1, 2), &weekYear), 1);         //First Sunday
2651     QCOMPARE(weekYear, 2011);
2652     QCOMPARE(calendar->week(QDate(2011, 1, 8), &weekYear), 1);         //Second Saturday
2653     QCOMPARE(weekYear, 2011);
2654     QCOMPARE(calendar->week(QDate(2011, 1, 9), &weekYear), 2);         //Second Sunday
2655     QCOMPARE(weekYear, 2011);
2656     QCOMPARE(calendar->week(QDate(2011, 12, 31), &weekYear), 52);       //Last Day
2657     QCOMPARE(weekYear, 2011);
2658 
2659     // Sun = Jan 1 2012 = Week 1 2012, Sun = Jan 1 = Week 1
2660     QCOMPARE(calendar->week(QDate(2012, 1, 1), &weekYear), 1);         //First Day, First Sunday
2661     QCOMPARE(weekYear, 2012);
2662     QCOMPARE(calendar->week(QDate(2012, 1, 7), &weekYear), 1);         //First Saturday
2663     QCOMPARE(weekYear, 2012);
2664     QCOMPARE(calendar->week(QDate(2012, 1, 8), &weekYear), 2);         //Second Sunday
2665     QCOMPARE(weekYear, 2012);
2666     QCOMPARE(calendar->week(QDate(2012, 12, 31), &weekYear), 53);       //Last Day
2667     QCOMPARE(weekYear, 2012);
2668 
2669     // First Full Week starting Monday
2670     locale->setWeekStartDay(1);
2671     locale->setWeekNumberSystem(KLocale::FirstFullWeek);
2672 
2673     // Mon = Jan 1 2018 = Week 52 2017, Mon = Jan 1 = Week 1
2674     QCOMPARE(calendar->week(QDate(2018, 1, 1), &weekYear), 1);          //First Day, First Monday
2675     QCOMPARE(weekYear, 2018);
2676     QCOMPARE(calendar->week(QDate(2018, 1, 7), &weekYear), 1);          //First Sunday
2677     QCOMPARE(weekYear, 2018);
2678     QCOMPARE(calendar->week(QDate(2018, 1, 8), &weekYear), 2);          //Second Monday
2679     QCOMPARE(weekYear, 2018);
2680     QCOMPARE(calendar->week(QDate(2018, 12, 31), &weekYear), 53);       //Last Day
2681     QCOMPARE(weekYear, 2018);
2682 
2683     // Tue = Jan 1 2013 = Week 52 2012, Mon = Jan 7 = Week 1
2684     QCOMPARE(calendar->week(QDate(2013, 1, 1), &weekYear), 53);         //First Day
2685     QCOMPARE(weekYear, 2012);
2686     QCOMPARE(calendar->week(QDate(2013, 1, 6), &weekYear), 53);         //First Sunday
2687     QCOMPARE(weekYear, 2012);
2688     QCOMPARE(calendar->week(QDate(2013, 1, 7), &weekYear), 1);          //First Monday
2689     QCOMPARE(weekYear, 2013);
2690     QCOMPARE(calendar->week(QDate(2013, 1, 13), &weekYear), 1);         //Second Saturday
2691     QCOMPARE(weekYear, 2013);
2692     QCOMPARE(calendar->week(QDate(2013, 1, 14), &weekYear), 2);         //Second Monday
2693     QCOMPARE(weekYear, 2013);
2694     QCOMPARE(calendar->week(QDate(2013, 12, 31), &weekYear), 52);       //Last Day
2695     QCOMPARE(weekYear, 2013);
2696 
2697     // Wed = Jan 1 2014 = Week 52 2013, Mon = Jan 6 = Week 1
2698     QCOMPARE(calendar->week(QDate(2014, 1, 1), &weekYear), 52);         //First Day
2699     QCOMPARE(weekYear, 2013);
2700     QCOMPARE(calendar->week(QDate(2014, 1, 5), &weekYear), 52);         //First Sunday
2701     QCOMPARE(weekYear, 2013);
2702     QCOMPARE(calendar->week(QDate(2014, 1, 6), &weekYear), 1);          //First Monday
2703     QCOMPARE(weekYear, 2014);
2704     QCOMPARE(calendar->week(QDate(2014, 1, 12), &weekYear), 1);         //Second Saturday
2705     QCOMPARE(weekYear, 2014);
2706     QCOMPARE(calendar->week(QDate(2014, 1, 13), &weekYear), 2);         //Second Monday
2707     QCOMPARE(weekYear, 2014);
2708     QCOMPARE(calendar->week(QDate(2014, 12, 31), &weekYear), 52);       //Last Day
2709     QCOMPARE(weekYear, 2014);
2710 
2711     // Thu = Jan 1 2015 = Week 52 2014, Mon = Jan 5 = Week 1
2712     QCOMPARE(calendar->week(QDate(2015, 1, 1), &weekYear), 52);         //First Day
2713     QCOMPARE(weekYear, 2014);
2714     QCOMPARE(calendar->week(QDate(2015, 1, 4), &weekYear), 52);         //First Sunday
2715     QCOMPARE(weekYear, 2014);
2716     QCOMPARE(calendar->week(QDate(2015, 1, 5), &weekYear), 1);          //First Monday
2717     QCOMPARE(weekYear, 2015);
2718     QCOMPARE(calendar->week(QDate(2015, 1, 11), &weekYear), 1);         //Second Saturday
2719     QCOMPARE(weekYear, 2015);
2720     QCOMPARE(calendar->week(QDate(2015, 1, 12), &weekYear), 2);         //Second Monday
2721     QCOMPARE(weekYear, 2015);
2722     QCOMPARE(calendar->week(QDate(2015, 12, 31), &weekYear), 52);       //Last Day
2723     QCOMPARE(weekYear, 2015);
2724 
2725     // Fri = Jan 1 2016 = Week 52 2015, Mon = Jan 4 = Week 1
2726     QCOMPARE(calendar->week(QDate(2016, 1, 1), &weekYear), 52);         //First Day
2727     QCOMPARE(weekYear, 2015);
2728     QCOMPARE(calendar->week(QDate(2016, 1, 3), &weekYear), 52);         //First Sunday
2729     QCOMPARE(weekYear, 2015);
2730     QCOMPARE(calendar->week(QDate(2016, 1, 4), &weekYear), 1);          //First Monday
2731     QCOMPARE(weekYear, 2016);
2732     QCOMPARE(calendar->week(QDate(2016, 1, 10), &weekYear), 1);         //Second Saturday
2733     QCOMPARE(weekYear, 2016);
2734     QCOMPARE(calendar->week(QDate(2016, 1, 11), &weekYear), 2);         //Second Monday
2735     QCOMPARE(weekYear, 2016);
2736     QCOMPARE(calendar->week(QDate(2016, 12, 31), &weekYear), 52);       //Last Day
2737     QCOMPARE(weekYear, 2016);
2738 
2739     // Sat = Jan 1 2011 = Week 52 2010, Mon = Jan 3 = Week 1
2740     QCOMPARE(calendar->week(QDate(2011, 1, 1), &weekYear), 52);        //First Day
2741     QCOMPARE(weekYear, 2010);
2742     QCOMPARE(calendar->week(QDate(2011, 1, 2), &weekYear), 52);        //First Sunday
2743     QCOMPARE(weekYear, 2010);
2744     QCOMPARE(calendar->week(QDate(2011, 1, 3), &weekYear), 1);         //First Monday
2745     QCOMPARE(weekYear, 2011);
2746     QCOMPARE(calendar->week(QDate(2011, 1, 9), &weekYear), 1);         //Second Sunday
2747     QCOMPARE(weekYear, 2011);
2748     QCOMPARE(calendar->week(QDate(2011, 1, 10), &weekYear), 2);        //Second Monday
2749     QCOMPARE(weekYear, 2011);
2750     QCOMPARE(calendar->week(QDate(2011, 12, 31), &weekYear), 52);       //Last Day
2751     QCOMPARE(weekYear, 2011);
2752 
2753     // Sun = Jan 1 2012 = Week 52 2011, Mon = Jan 2 = Week 1
2754     QCOMPARE(calendar->week(QDate(2012, 1, 1), &weekYear), 52);         //First Day, First Sunday
2755     QCOMPARE(weekYear, 2011);
2756     QCOMPARE(calendar->week(QDate(2012, 1, 2), &weekYear), 1);          //First Monday
2757     QCOMPARE(weekYear, 2012);
2758     QCOMPARE(calendar->week(QDate(2012, 1, 8), &weekYear), 1);          //Second Sunday
2759     QCOMPARE(weekYear, 2012);
2760     QCOMPARE(calendar->week(QDate(2012, 1, 9), &weekYear), 2);          //Second Monday
2761     QCOMPARE(weekYear, 2012);
2762     QCOMPARE(calendar->week(QDate(2012, 12, 31), &weekYear), 53);       //Last Day
2763     QCOMPARE(weekYear, 2012);
2764 
2765     // First Partial Week, second week starting Sunday
2766     locale->setWeekStartDay(7);
2767     locale->setWeekNumberSystem(KLocale::FirstPartialWeek);
2768 
2769     // Mon = Jan 1 2018 = Week 1, Sun = Jan 7 = Week 2
2770     QCOMPARE(calendar->week(QDate(2018, 1, 1), &weekYear), 1);         //First Day
2771     QCOMPARE(weekYear, 2018);
2772     QCOMPARE(calendar->week(QDate(2018, 1, 6), &weekYear), 1);         //First Saturday
2773     QCOMPARE(weekYear, 2018);
2774     QCOMPARE(calendar->week(QDate(2018, 1, 7), &weekYear), 2);         //First Sunday
2775     QCOMPARE(weekYear, 2018);
2776     QCOMPARE(calendar->week(QDate(2018, 1, 13), &weekYear), 2);        //Second Saturday
2777     QCOMPARE(weekYear, 2018);
2778     QCOMPARE(calendar->week(QDate(2018, 1, 14), &weekYear), 3);        //Second Sunday
2779     QCOMPARE(weekYear, 2018);
2780     QCOMPARE(calendar->week(QDate(2018, 12, 31), &weekYear), 53);       //Last Day
2781     QCOMPARE(weekYear, 2018);
2782 
2783     // Tue = Jan 1 2013 = Week 1, Sun = Jan 6 = Week 2
2784     QCOMPARE(calendar->week(QDate(2013, 1, 1), &weekYear), 1);        //First Day
2785     QCOMPARE(weekYear, 2013);
2786     QCOMPARE(calendar->week(QDate(2013, 1, 5), &weekYear), 1);        //First Saturday
2787     QCOMPARE(weekYear, 2013);
2788     QCOMPARE(calendar->week(QDate(2013, 1, 6), &weekYear), 2);         //First Sunday
2789     QCOMPARE(weekYear, 2013);
2790     QCOMPARE(calendar->week(QDate(2013, 1, 12), &weekYear), 2);        //Second Saturday
2791     QCOMPARE(weekYear, 2013);
2792     QCOMPARE(calendar->week(QDate(2013, 1, 13), &weekYear), 3);        //Second Sunday
2793     QCOMPARE(weekYear, 2013);
2794     QCOMPARE(calendar->week(QDate(2013, 12, 31), &weekYear), 53);       //Last Day
2795     QCOMPARE(weekYear, 2013);
2796 
2797     // Wed = Jan 1 2014 = Week 1, Sun = Jan 5 = Week 2
2798     QCOMPARE(calendar->week(QDate(2014, 1, 1), &weekYear), 1);        //First Day
2799     QCOMPARE(weekYear, 2014);
2800     QCOMPARE(calendar->week(QDate(2014, 1, 4), &weekYear), 1);        //First Saturday
2801     QCOMPARE(weekYear, 2014);
2802     QCOMPARE(calendar->week(QDate(2014, 1, 5), &weekYear), 2);         //First Sunday
2803     QCOMPARE(weekYear, 2014);
2804     QCOMPARE(calendar->week(QDate(2014, 1, 11), &weekYear), 2);        //Second Saturday
2805     QCOMPARE(weekYear, 2014);
2806     QCOMPARE(calendar->week(QDate(2014, 1, 12), &weekYear), 3);        //Second Sunday
2807     QCOMPARE(weekYear, 2014);
2808     QCOMPARE(calendar->week(QDate(2014, 12, 31), &weekYear), 53);       //Last Day
2809     QCOMPARE(weekYear, 2014);
2810 
2811     // Thu = Jan 1 2015 = Week 1, Sun = Jan 4 = Week 2
2812     QCOMPARE(calendar->week(QDate(2015, 1, 1), &weekYear), 1);        //First Day
2813     QCOMPARE(weekYear, 2015);
2814     QCOMPARE(calendar->week(QDate(2015, 1, 3), &weekYear), 1);        //First Saturday
2815     QCOMPARE(weekYear, 2015);
2816     QCOMPARE(calendar->week(QDate(2015, 1, 4), &weekYear), 2);         //First Sunday
2817     QCOMPARE(weekYear, 2015);
2818     QCOMPARE(calendar->week(QDate(2015, 1, 10), &weekYear), 2);        //Second Saturday
2819     QCOMPARE(weekYear, 2015);
2820     QCOMPARE(calendar->week(QDate(2015, 1, 11), &weekYear), 3);        //Second Sunday
2821     QCOMPARE(weekYear, 2015);
2822     QCOMPARE(calendar->week(QDate(2015, 12, 31), &weekYear), 53);       //Last Day
2823     QCOMPARE(weekYear, 2015);
2824 
2825     // Fri = Jan 1 2016 = Week 1, Sun = Jan 3 = Week 2
2826     QCOMPARE(calendar->week(QDate(2016, 1, 1), &weekYear), 1);        //First Day
2827     QCOMPARE(weekYear, 2016);
2828     QCOMPARE(calendar->week(QDate(2016, 1, 2), &weekYear), 1);        //First Saturday
2829     QCOMPARE(weekYear, 2016);
2830     QCOMPARE(calendar->week(QDate(2016, 1, 3), &weekYear), 2);         //First Sunday
2831     QCOMPARE(weekYear, 2016);
2832     QCOMPARE(calendar->week(QDate(2016, 1, 9), &weekYear), 2);         //Second Saturday
2833     QCOMPARE(weekYear, 2016);
2834     QCOMPARE(calendar->week(QDate(2016, 1, 10), &weekYear), 3);        //Second Sunday
2835     QCOMPARE(weekYear, 2016);
2836     QCOMPARE(calendar->week(QDate(2016, 12, 31), &weekYear), 53);       //Last Day
2837     QCOMPARE(weekYear, 2016);
2838 
2839     // Sat = Jan 1 2011 = Week 1, Sun = Jan 2 = Week 2
2840     QCOMPARE(calendar->week(QDate(2011, 1, 1), &weekYear), 1);        //First Day, First Saturday
2841     QCOMPARE(weekYear, 2011);
2842     QCOMPARE(calendar->week(QDate(2011, 1, 2), &weekYear), 2);         //First Sunday
2843     QCOMPARE(weekYear, 2011);
2844     QCOMPARE(calendar->week(QDate(2011, 1, 8), &weekYear), 2);         //Second Saturday
2845     QCOMPARE(weekYear, 2011);
2846     QCOMPARE(calendar->week(QDate(2011, 1, 9), &weekYear), 3);         //Second Sunday
2847     QCOMPARE(weekYear, 2011);
2848     QCOMPARE(calendar->week(QDate(2011, 12, 31), &weekYear), 53);       //Last Day
2849     QCOMPARE(weekYear, 2011);
2850 
2851     // Sun = Jan 1 2012 = Week 1, Sun = Jan 1 = Week 1
2852     QCOMPARE(calendar->week(QDate(2012, 1, 1), &weekYear), 1);         //First Day, First Sunday
2853     QCOMPARE(weekYear, 2012);
2854     QCOMPARE(calendar->week(QDate(2012, 1, 7), &weekYear), 1);         //First Saturday
2855     QCOMPARE(weekYear, 2012);
2856     QCOMPARE(calendar->week(QDate(2012, 1, 8), &weekYear), 2);         //Second Sunday
2857     QCOMPARE(weekYear, 2012);
2858     QCOMPARE(calendar->week(QDate(2012, 12, 31), &weekYear), 53);       //Last Day
2859     QCOMPARE(weekYear, 2012);
2860 
2861     // First Partial Week starting Monday
2862     locale->setWeekStartDay(1);
2863     locale->setWeekNumberSystem(KLocale::FirstPartialWeek);
2864 
2865     // Mon = Jan 1 2018 = Week 1, Mon = Jan 1 = Week 2
2866     QCOMPARE(calendar->week(QDate(2018, 1, 1), &weekYear), 1);          //First Day, First Monday
2867     QCOMPARE(weekYear, 2018);
2868     QCOMPARE(calendar->week(QDate(2018, 1, 7), &weekYear), 1);          //First Sunday
2869     QCOMPARE(weekYear, 2018);
2870     QCOMPARE(calendar->week(QDate(2018, 1, 8), &weekYear), 2);          //Second Monday
2871     QCOMPARE(weekYear, 2018);
2872     QCOMPARE(calendar->week(QDate(2018, 12, 31), &weekYear), 53);       //Last Day
2873     QCOMPARE(weekYear, 2018);
2874 
2875     // Tue = Jan 1 2013 = Week 1, Mon = Jan 7 = Week 2
2876     QCOMPARE(calendar->week(QDate(2013, 1, 1), &weekYear), 1);          //First Day
2877     QCOMPARE(weekYear, 2013);
2878     QCOMPARE(calendar->week(QDate(2013, 1, 6), &weekYear), 1);          //First Sunday
2879     QCOMPARE(weekYear, 2013);
2880     QCOMPARE(calendar->week(QDate(2013, 1, 7), &weekYear), 2);          //First Monday
2881     QCOMPARE(weekYear, 2013);
2882     QCOMPARE(calendar->week(QDate(2013, 1, 13), &weekYear), 2);         //Second Saturday
2883     QCOMPARE(weekYear, 2013);
2884     QCOMPARE(calendar->week(QDate(2013, 1, 14), &weekYear), 3);         //Second Monday
2885     QCOMPARE(weekYear, 2013);
2886     QCOMPARE(calendar->week(QDate(2013, 12, 31), &weekYear), 53);       //Last Day
2887     QCOMPARE(weekYear, 2013);
2888 
2889     // Wed = Jan 1 2014 = Week 1, Mon = Jan 6 = Week 2
2890     QCOMPARE(calendar->week(QDate(2014, 1, 1), &weekYear), 1);          //First Day
2891     QCOMPARE(weekYear, 2014);
2892     QCOMPARE(calendar->week(QDate(2014, 1, 5), &weekYear), 1);          //First Sunday
2893     QCOMPARE(weekYear, 2014);
2894     QCOMPARE(calendar->week(QDate(2014, 1, 6), &weekYear), 2);          //First Monday
2895     QCOMPARE(weekYear, 2014);
2896     QCOMPARE(calendar->week(QDate(2014, 1, 12), &weekYear), 2);         //Second Saturday
2897     QCOMPARE(weekYear, 2014);
2898     QCOMPARE(calendar->week(QDate(2014, 1, 13), &weekYear), 3);         //Second Monday
2899     QCOMPARE(weekYear, 2014);
2900     QCOMPARE(calendar->week(QDate(2014, 12, 31), &weekYear), 53);       //Last Day
2901     QCOMPARE(weekYear, 2014);
2902 
2903     // Thu = Jan 1 2015 = Week 1, Mon = Jan 5 = Week 2
2904     QCOMPARE(calendar->week(QDate(2015, 1, 1), &weekYear), 1);          //First Day
2905     QCOMPARE(weekYear, 2015);
2906     QCOMPARE(calendar->week(QDate(2015, 1, 4), &weekYear), 1);          //First Sunday
2907     QCOMPARE(weekYear, 2015);
2908     QCOMPARE(calendar->week(QDate(2015, 1, 5), &weekYear), 2);          //First Monday
2909     QCOMPARE(weekYear, 2015);
2910     QCOMPARE(calendar->week(QDate(2015, 1, 11), &weekYear), 2);         //Second Saturday
2911     QCOMPARE(weekYear, 2015);
2912     QCOMPARE(calendar->week(QDate(2015, 1, 12), &weekYear), 3);         //Second Monday
2913     QCOMPARE(weekYear, 2015);
2914     QCOMPARE(calendar->week(QDate(2015, 12, 31), &weekYear), 53);       //Last Day
2915     QCOMPARE(weekYear, 2015);
2916 
2917     // Fri = Jan 1 2016 = Week 1, Mon = Jan 4 = Week 2
2918     QCOMPARE(calendar->week(QDate(2016, 1, 1), &weekYear), 1);          //First Day
2919     QCOMPARE(weekYear, 2016);
2920     QCOMPARE(calendar->week(QDate(2016, 1, 3), &weekYear), 1);          //First Sunday
2921     QCOMPARE(weekYear, 2016);
2922     QCOMPARE(calendar->week(QDate(2016, 1, 4), &weekYear), 2);          //First Monday
2923     QCOMPARE(weekYear, 2016);
2924     QCOMPARE(calendar->week(QDate(2016, 1, 10), &weekYear), 2);         //Second Saturday
2925     QCOMPARE(weekYear, 2016);
2926     QCOMPARE(calendar->week(QDate(2016, 1, 11), &weekYear), 3);         //Second Monday
2927     QCOMPARE(weekYear, 2016);
2928     QCOMPARE(calendar->week(QDate(2016, 12, 31), &weekYear), 53);       //Last Day
2929     QCOMPARE(weekYear, 2016);
2930 
2931     // Sat = Jan 1 2011 = Week 1, Mon = Jan 3 = Week 2
2932     QCOMPARE(calendar->week(QDate(2011, 1, 1), &weekYear), 1);         //First Day
2933     QCOMPARE(weekYear, 2011);
2934     QCOMPARE(calendar->week(QDate(2011, 1, 2), &weekYear), 1);         //First Sunday
2935     QCOMPARE(weekYear, 2011);
2936     QCOMPARE(calendar->week(QDate(2011, 1, 3), &weekYear), 2);         //First Monday
2937     QCOMPARE(weekYear, 2011);
2938     QCOMPARE(calendar->week(QDate(2011, 1, 9), &weekYear), 2);         //Second Sunday
2939     QCOMPARE(weekYear, 2011);
2940     QCOMPARE(calendar->week(QDate(2011, 1, 10), &weekYear), 3);        //Second Monday
2941     QCOMPARE(weekYear, 2011);
2942     QCOMPARE(calendar->week(QDate(2011, 12, 31), &weekYear), 53);       //Last Day
2943     QCOMPARE(weekYear, 2011);
2944 
2945     // Sun = Jan 1 2012 = Week 1, Mon = Jan 2 = Week 2
2946     QCOMPARE(calendar->week(QDate(2012, 1, 1), &weekYear), 1);         //First Day, First Sunday
2947     QCOMPARE(weekYear, 2012);
2948     QCOMPARE(calendar->week(QDate(2012, 1, 2), &weekYear), 2);          //First Monday
2949     QCOMPARE(weekYear, 2012);
2950     QCOMPARE(calendar->week(QDate(2012, 1, 8), &weekYear), 2);          //Second Sunday
2951     QCOMPARE(weekYear, 2012);
2952     QCOMPARE(calendar->week(QDate(2012, 1, 9), &weekYear), 3);          //Second Monday
2953     QCOMPARE(weekYear, 2012);
2954     QCOMPARE(calendar->week(QDate(2012, 12, 31), &weekYear), 54);       //Last Day
2955     QCOMPARE(weekYear, 2012);
2956 
2957     // Simple weeks starting Jan 1
2958     locale->setWeekStartDay(1);
2959     locale->setWeekNumberSystem(KLocale::SimpleWeek);
2960 
2961     // Mon = Jan 1 2018
2962     QCOMPARE(calendar->week(QDate(2018, 1, 1), &weekYear), 1);         //First Day, Monday
2963     QCOMPARE(weekYear, 2018);
2964     QCOMPARE(calendar->week(QDate(2018, 1, 7), &weekYear), 1);
2965     QCOMPARE(weekYear, 2018);
2966     QCOMPARE(calendar->week(QDate(2018, 1, 8), &weekYear), 2);         //Second Week
2967     QCOMPARE(weekYear, 2018);
2968     QCOMPARE(calendar->week(QDate(2018, 1, 14), &weekYear), 2);
2969     QCOMPARE(weekYear, 2018);
2970     QCOMPARE(calendar->week(QDate(2018, 1, 15), &weekYear), 3);        //Third Week
2971     QCOMPARE(weekYear, 2018);
2972 
2973     // Tue = Jan 1 2013
2974     QCOMPARE(calendar->week(QDate(2013, 1, 1), &weekYear), 1);         //First Day, Tuesday
2975     QCOMPARE(weekYear, 2013);
2976     QCOMPARE(calendar->week(QDate(2013, 1, 7), &weekYear), 1);
2977     QCOMPARE(weekYear, 2013);
2978     QCOMPARE(calendar->week(QDate(2013, 1, 8), &weekYear), 2);         //Second Week
2979     QCOMPARE(weekYear, 2013);
2980     QCOMPARE(calendar->week(QDate(2013, 1, 14), &weekYear), 2);
2981     QCOMPARE(weekYear, 2013);
2982     QCOMPARE(calendar->week(QDate(2013, 1, 15), &weekYear), 3);        //Third Week
2983     QCOMPARE(weekYear, 2013);
2984 
2985     // Wed = Jan 1 2014
2986     QCOMPARE(calendar->week(QDate(2014, 1, 1), &weekYear), 1);         //First Day, Wednesday
2987     QCOMPARE(weekYear, 2014);
2988     QCOMPARE(calendar->week(QDate(2014, 1, 7), &weekYear), 1);
2989     QCOMPARE(weekYear, 2014);
2990     QCOMPARE(calendar->week(QDate(2014, 1, 8), &weekYear), 2);         //Second Week
2991     QCOMPARE(weekYear, 2014);
2992     QCOMPARE(calendar->week(QDate(2014, 1, 14), &weekYear), 2);
2993     QCOMPARE(weekYear, 2014);
2994     QCOMPARE(calendar->week(QDate(2014, 1, 15), &weekYear), 3);        //Third Week
2995     QCOMPARE(weekYear, 2014);
2996 
2997     // Thu = Jan 1 2015
2998     QCOMPARE(calendar->week(QDate(2015, 1, 1), &weekYear), 1);         //First Day, Thursday
2999     QCOMPARE(weekYear, 2015);
3000     QCOMPARE(calendar->week(QDate(2015, 1, 7), &weekYear), 1);
3001     QCOMPARE(weekYear, 2015);
3002     QCOMPARE(calendar->week(QDate(2015, 1, 8), &weekYear), 2);         //Second Week
3003     QCOMPARE(weekYear, 2015);
3004     QCOMPARE(calendar->week(QDate(2015, 1, 14), &weekYear), 2);
3005     QCOMPARE(weekYear, 2015);
3006     QCOMPARE(calendar->week(QDate(2015, 1, 15), &weekYear), 3);        //Third Week
3007     QCOMPARE(weekYear, 2015);
3008 
3009     // Fri = Jan 1 2016
3010     QCOMPARE(calendar->week(QDate(2016, 1, 1), &weekYear), 1);         //First Day, Monday
3011     QCOMPARE(weekYear, 2016);
3012     QCOMPARE(calendar->week(QDate(2016, 1, 7), &weekYear), 1);
3013     QCOMPARE(weekYear, 2016);
3014     QCOMPARE(calendar->week(QDate(2016, 1, 8), &weekYear), 2);         //Second Week
3015     QCOMPARE(weekYear, 2016);
3016     QCOMPARE(calendar->week(QDate(2016, 1, 14), &weekYear), 2);
3017     QCOMPARE(weekYear, 2016);
3018     QCOMPARE(calendar->week(QDate(2016, 1, 15), &weekYear), 3);        //Third Week
3019     QCOMPARE(weekYear, 2016);
3020 
3021     // Sat = Jan 1 2011
3022     QCOMPARE(calendar->week(QDate(2011, 1, 1), &weekYear), 1);         //First Day, Monday
3023     QCOMPARE(weekYear, 2011);
3024     QCOMPARE(calendar->week(QDate(2011, 1, 7), &weekYear), 1);
3025     QCOMPARE(weekYear, 2011);
3026     QCOMPARE(calendar->week(QDate(2011, 1, 8), &weekYear), 2);         //Second Week
3027     QCOMPARE(weekYear, 2011);
3028     QCOMPARE(calendar->week(QDate(2011, 1, 14), &weekYear), 2);
3029     QCOMPARE(weekYear, 2011);
3030     QCOMPARE(calendar->week(QDate(2011, 1, 15), &weekYear), 3);        //Third Week
3031     QCOMPARE(weekYear, 2011);
3032 
3033     // Sun = Jan 1 2012
3034     QCOMPARE(calendar->week(QDate(2012, 1, 1), &weekYear), 1);         //First Day, Monday
3035     QCOMPARE(weekYear, 2012);
3036     QCOMPARE(calendar->week(QDate(2012, 1, 7), &weekYear), 1);
3037     QCOMPARE(weekYear, 2012);
3038     QCOMPARE(calendar->week(QDate(2012, 1, 8), &weekYear), 2);         //Second Week
3039     QCOMPARE(weekYear, 2012);
3040     QCOMPARE(calendar->week(QDate(2012, 1, 14), &weekYear), 2);
3041     QCOMPARE(weekYear, 2012);
3042     QCOMPARE(calendar->week(QDate(2012, 1, 15), &weekYear), 3);        //Third Week
3043     QCOMPARE(weekYear, 2012);
3044 
3045     delete calendar;
3046     delete locale;
3047 }