File indexing completed on 2022-11-29 18:25:15

0001 /* This file is part of the KDE libraries
0002     Copyright (c) 2005 Thomas Braxton <brax108@cox.net>
0003     Copyright (c) 2007 David Jarvie <djarvie@kde.org>
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 version 2 as published by the Free Software Foundation.
0008 
0009     This library is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012     Library General Public License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to
0016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017     Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "klocaletest.h"
0021 #include <QTest>
0022 
0023 #include "klocale.h"
0024 #include "klocale_p.h"
0025 #include "kconfiggroup.h"
0026 #include "kcalendarsystem.h"
0027 #include "kdayperiod_p.h"
0028 #include "kdatetime.h"
0029 #include "ksystemtimezone.h"
0030 #include <QString>
0031 #include <QDate>
0032 #include <QDBusConnection>
0033 #include <QDBusConnectionInterface>
0034 
0035 void setupLocale()
0036 {
0037     qputenv("LC_ALL", "C");
0038 }
0039 Q_CONSTRUCTOR_FUNCTION(setupLocale)
0040 
0041 void
0042 KLocaleTest::initTestCase()
0043 {
0044     QStandardPaths::setTestModeEnabled(true);
0045 
0046     KLocale::global()->setCountry(QLatin1String("C"), nullptr);
0047     KLocale::global()->setThousandsSeparator(QLatin1String(","));
0048 }
0049 
0050 void KLocaleTest::languages()
0051 {
0052     QVERIFY(KLocale::global()->installedLanguages().contains("en_US"));
0053 }
0054 
0055 void
0056 KLocaleTest::formatNumbers()
0057 {
0058     KLocale locale(*KLocale::global());
0059 
0060     KLocale::global()->setDecimalPlaces(2);
0061     QCOMPARE(locale.formatNumber(70), QString("70.00"));
0062     QCOMPARE(locale.formatNumber(70, 0), QString("70"));
0063     QCOMPARE(locale.formatNumber(70.2), QString("70.20"));
0064     QCOMPARE(locale.formatNumber(70.24), QString("70.24"));
0065     QCOMPARE(locale.formatNumber(70.245), QString("70.25"));
0066     QCOMPARE(locale.formatNumber(1234567.89123456789, 8),
0067              QString("1,234,567.89123457"));
0068     QCOMPARE(locale.formatNumber(1234567.89123456789, 0), QString("1,234,568"));
0069 
0070     // Test Grouping
0071     locale.d->setNumericDigitGrouping(QList<int>());
0072     QCOMPARE(locale.formatNumber(123456789, 0), QString("123456789"));
0073     QCOMPARE(locale.formatNumber(123456789.01), QString("123456789.01"));
0074     locale.d->setNumericDigitGrouping(QList<int>() << 3 << 2);
0075     QCOMPARE(locale.formatNumber(123456789, 0), QString("12,34,56,789"));
0076     QCOMPARE(locale.formatNumber(123456789.01), QString("12,34,56,789.01"));
0077     locale.d->setNumericDigitGrouping(QList<int>() << 3 << -1);
0078     QCOMPARE(locale.formatNumber(123456789, 0), QString("123456,789"));
0079     QCOMPARE(locale.formatNumber(123456789.01), QString("123456,789.01"));
0080 
0081     //Test it formats correctly with an empty and space separator.
0082     locale.d->setNumericDigitGrouping(QList<int>() << 3);
0083     QCOMPARE(locale.formatNumber(123456789, 0), QString("123,456,789"));
0084     QCOMPARE(locale.formatNumber(123456789.01), QString("123,456,789.01"));
0085     locale.setThousandsSeparator(QString());
0086     QCOMPARE(locale.formatNumber(123456789, 0), QString("123456789"));
0087     QCOMPARE(locale.formatNumber(123456789.01), QString("123456789.01"));
0088     locale.setThousandsSeparator(" ");
0089     QCOMPARE(locale.formatNumber(123456789, 0), QString("123 456 789"));
0090     QCOMPARE(locale.formatNumber(123456789.01), QString("123 456 789.01"));
0091 }
0092 
0093 void
0094 KLocaleTest::formatNumberStrings()
0095 {
0096     KLocale locale(*KLocale::global());
0097 
0098     KLocale::global()->setDecimalPlaces(2);
0099     QCOMPARE(locale.formatNumber("70"), QString("70.00"));
0100     QCOMPARE(locale.formatNumber("70", true, 2), QString("70.00"));
0101     QCOMPARE(locale.formatNumber("70", true, 0), QString("70"));
0102     QCOMPARE(locale.formatNumber("0.2", true, 1), QString("0.2"));
0103     QCOMPARE(locale.formatNumber("70.9123", true, 0), QString("71"));
0104     QCOMPARE(locale.formatNumber("-70.2", true, 2), QString("-70.20"));
0105     QCOMPARE(locale.formatNumber("+70.24", true, 2), QString("70.24"));
0106     QCOMPARE(locale.formatNumber("70.245", true, 2), QString("70.25"));
0107     QCOMPARE(locale.formatNumber("99.996", true, 2), QString("100.00"));
0108     QCOMPARE(locale.formatNumber("12345678901234567.89123456789", false, 0),
0109              QString("12,345,678,901,234,567.89123456789"));
0110     QCOMPARE(locale.formatNumber("99.9e96", true, 2), QString("99.90e96"));
0111     QCOMPARE(locale.formatNumber("99e", true, 1), QString("99.0"));
0112     QCOMPARE(locale.formatNumber("e2", true, 0), QString("0e2"));
0113     QCOMPARE(locale.formatNumber("1.", true, 1), QString("1.0"));
0114     QCOMPARE(locale.formatNumber("1.", true, 0), QString("1"));
0115     QCOMPARE(locale.formatNumber("1.", false, 1), QString("1.")); // no rounding -> 1. unchanged
0116     QCOMPARE(locale.formatNumber("1.", false, 0), QString("1."));
0117     QCOMPARE(locale.formatNumber("abcd", true, 2), QString("0.00")); // invalid number
0118 
0119     // Test Grouping
0120     locale.d->setNumericDigitGrouping(QList<int>());
0121     QCOMPARE(locale.formatNumber("123456789", true, 0), QString("123456789"));
0122     QCOMPARE(locale.formatNumber("123456789.01"),       QString("123456789.01"));
0123     locale.d->setNumericDigitGrouping(QList<int>() << 3 << 2);
0124     QCOMPARE(locale.formatNumber("123456789", true, 0), QString("12,34,56,789"));
0125     QCOMPARE(locale.formatNumber("123456789.01"),       QString("12,34,56,789.01"));
0126     locale.d->setNumericDigitGrouping(QList<int>() << 3 << -1);
0127     QCOMPARE(locale.formatNumber("123456789", true, 0), QString("123456,789"));
0128     QCOMPARE(locale.formatNumber("123456789.01"),       QString("123456,789.01"));
0129 
0130     //Test it formats correctly with an empty and space separator.
0131     locale.d->setNumericDigitGrouping(QList<int>() << 3);
0132     QCOMPARE(locale.formatNumber("123456789", true, 0), QString("123,456,789"));
0133     QCOMPARE(locale.formatNumber("123456789.01"),       QString("123,456,789.01"));
0134     locale.setThousandsSeparator(QString());
0135     QCOMPARE(locale.formatNumber("123456789", true, 0), QString("123456789"));
0136     QCOMPARE(locale.formatNumber("123456789.01"),       QString("123456789.01"));
0137     locale.setThousandsSeparator(" ");
0138     QCOMPARE(locale.formatNumber("123456789", true, 0), QString("123 456 789"));
0139     QCOMPARE(locale.formatNumber("123456789.01"),       QString("123 456 789.01"));
0140 }
0141 
0142 void
0143 KLocaleTest::readNumber()
0144 {
0145     KLocale locale(*KLocale::global());
0146     bool ok = false;
0147 
0148     locale.readNumber("12,1", &ok);
0149     QVERIFY(!ok);
0150     QCOMPARE(locale.readNumber("12,100", &ok), 12100.0);
0151     QVERIFY(ok);
0152     locale.readNumber("12,100000,00", &ok);
0153     QVERIFY(!ok);
0154     locale.readNumber("12,100000000", &ok);
0155     QVERIFY(!ok);
0156     locale.readNumber("12,,100,000", &ok);
0157     QVERIFY(!ok);
0158     locale.readNumber("12,1000,000", &ok);
0159     QVERIFY(!ok);
0160     locale.readNumber("12,0000000,000", &ok);
0161     QVERIFY(!ok);
0162     locale.readNumber("12,0000000", &ok);
0163     QVERIFY(!ok);
0164     QCOMPARE(locale.readNumber("12,146,131.12", &ok), 12146131.12);
0165     QVERIFY(ok);
0166     QCOMPARE(locale.readNumber("1.12345678912", &ok), 1.12345678912);
0167     QVERIFY(ok);
0168 
0169     // Test Grouping
0170     locale.d->setNumericDigitGrouping(QList<int>());
0171     QCOMPARE(locale.readNumber(QString("123456789"), &ok), 123456789.0);
0172     QVERIFY(ok);
0173     QCOMPARE(locale.readNumber(QString("123456789.01"), &ok), 123456789.01);
0174     QVERIFY(ok);
0175     QCOMPARE(locale.readNumber(QString("123,456,789"), &ok), 0.0);
0176     QVERIFY(!ok);
0177     QCOMPARE(locale.readNumber(QString("123,456,789.01"), &ok), 0.0);
0178     QVERIFY(!ok);
0179 
0180     locale.d->setNumericDigitGrouping(QList<int>() << 3 << 2);
0181     QCOMPARE(locale.readNumber(QString("12,34,56,789"), &ok), 123456789.0);
0182     QVERIFY(ok);
0183     QCOMPARE(locale.readNumber(QString("12,34,56,789.01"), &ok), 123456789.01);
0184     QVERIFY(ok);
0185     QCOMPARE(locale.readNumber(QString("123,456,789"), &ok), 0.0);
0186     QVERIFY(!ok);
0187     QCOMPARE(locale.readNumber(QString("123,456,789.01"), &ok), 0.0);
0188     QVERIFY(!ok);
0189     QCOMPARE(locale.readNumber(QString("123456789"), &ok), 123456789.0);
0190     QVERIFY(ok);
0191     QCOMPARE(locale.readNumber(QString("123456789.01"), &ok), 123456789.01);
0192     QVERIFY(ok);
0193     QCOMPARE(locale.readNumber(QString("123456,789"), &ok), 0.0);
0194     QVERIFY(!ok);
0195     QCOMPARE(locale.readNumber(QString("123456,789.01"), &ok), 0.0);
0196     QVERIFY(!ok);
0197 
0198     //Test it parses correctly with an empty separator.
0199     locale.d->setNumericDigitGrouping(QList<int>() << 3);
0200     locale.setThousandsSeparator(QString());
0201     QCOMPARE(locale.readNumber(QString("123456789"), &ok), 123456789.0);
0202     QVERIFY(ok);
0203     QCOMPARE(locale.readNumber(QString("123456789.01"), &ok), 123456789.01);
0204     QVERIFY(ok);
0205     QCOMPARE(locale.readNumber(QString("123,456,789"), &ok), 0.0);
0206     QVERIFY(!ok);
0207     QCOMPARE(locale.readNumber(QString("123,456,789.01"), &ok), 0.0);
0208     QVERIFY(!ok);
0209 
0210     //Test it parses correctly with an space separator.
0211     locale.d->setNumericDigitGrouping(QList<int>() << 3);
0212     locale.setThousandsSeparator(" ");
0213     QCOMPARE(locale.readNumber(QString("123 456 789"), &ok), 123456789.0);
0214     QVERIFY(ok);
0215     QCOMPARE(locale.readNumber(QString("123 456 789.01"), &ok), 123456789.01);
0216     QVERIFY(ok);
0217     QCOMPARE(locale.readNumber(QString("123456789"), &ok), 123456789.0);
0218     QVERIFY(ok);
0219     QCOMPARE(locale.readNumber(QString("123456789.01"), &ok), 123456789.01);
0220     QVERIFY(ok);
0221     QCOMPARE(locale.readNumber(QString("123456 789"), &ok), 0.0);
0222     QVERIFY(!ok);
0223     QCOMPARE(locale.readNumber(QString("123456 789.01"), &ok), 0.0);
0224     QVERIFY(!ok);
0225     QCOMPARE(locale.readNumber(QString("123,456,789"), &ok), 0.0);
0226     QVERIFY(!ok);
0227     QCOMPARE(locale.readNumber(QString("123,456,789.01"), &ok), 0.0);
0228     QVERIFY(!ok);
0229 
0230     // Test signs
0231     locale.setPositiveSign("@");
0232     locale.setNegativeSign("&");
0233     locale.setThousandsSeparator(",");
0234     locale.d->setNumericDigitGrouping(QList<int>() << 3);
0235 
0236     QCOMPARE(locale.readNumber(QString("@123,456,789.12"), &ok), 123456789.12);
0237     QVERIFY(ok);
0238     QCOMPARE(locale.readNumber(QString("&123,456,789.12"), &ok), -123456789.12);
0239     QVERIFY(ok);
0240     QCOMPARE(locale.readNumber(QString("123,456,789.12"), &ok), 123456789.12);
0241     QVERIFY(ok);
0242     QCOMPARE(locale.readNumber(QString("+123,456,789.12"), &ok), 0.0);
0243     QVERIFY(!ok);
0244     QCOMPARE(locale.readNumber(QString("-123,456,789.12"), &ok), 0.0);
0245     QVERIFY(!ok);
0246     locale.setNegativeSign(QString());
0247     QCOMPARE(locale.readNumber(QString("123,456,789.12"), &ok), -123456789.12);
0248     QVERIFY(ok);
0249     QCOMPARE(locale.readNumber(QString("-123,456,789.12"), &ok), 0.0);
0250     QVERIFY(!ok);
0251 }
0252 
0253 void KLocaleTest::formatMoney()
0254 {
0255     KLocale locale(*KLocale::global());
0256     locale.setPositiveSign(QString());
0257     locale.setNegativeSign("-");
0258     locale.setPositivePrefixCurrencySymbol(true);
0259     locale.setNegativePrefixCurrencySymbol(true);
0260     locale.setMonetaryDecimalPlaces(2);
0261     locale.setMonetaryThousandsSeparator(",");
0262     locale.setMonetaryDecimalSymbol(".");
0263     locale.setCurrencySymbol("$");
0264     locale.setPositiveMonetarySignPosition(KLocale::BeforeQuantityMoney);
0265     locale.setNegativeMonetarySignPosition(KLocale::BeforeQuantityMoney);
0266     locale.d->setMonetaryDigitGrouping(QList<int>() << 3);
0267 
0268     // Basic grouping, decimal place and rounding tests
0269     QCOMPARE(locale.formatMoney(1), QString("$ 1.00"));
0270     QCOMPARE(locale.formatMoney(21), QString("$ 21.00"));
0271     QCOMPARE(locale.formatMoney(321), QString("$ 321.00"));
0272     QCOMPARE(locale.formatMoney(4321), QString("$ 4,321.00"));
0273     QCOMPARE(locale.formatMoney(54321), QString("$ 54,321.00"));
0274     QCOMPARE(locale.formatMoney(654321), QString("$ 654,321.00"));
0275     QCOMPARE(locale.formatMoney(7654321), QString("$ 7,654,321.00"));
0276     QCOMPARE(locale.formatMoney(87654321), QString("$ 87,654,321.00"));
0277     QCOMPARE(locale.formatMoney(987654321), QString("$ 987,654,321.00"));
0278 
0279     QCOMPARE(locale.formatMoney(1.1), QString("$ 1.10"));
0280     QCOMPARE(locale.formatMoney(21.1), QString("$ 21.10"));
0281     QCOMPARE(locale.formatMoney(321.1), QString("$ 321.10"));
0282     QCOMPARE(locale.formatMoney(4321.1), QString("$ 4,321.10"));
0283     QCOMPARE(locale.formatMoney(54321.1), QString("$ 54,321.10"));
0284     QCOMPARE(locale.formatMoney(654321.1), QString("$ 654,321.10"));
0285     QCOMPARE(locale.formatMoney(7654321.1), QString("$ 7,654,321.10"));
0286     QCOMPARE(locale.formatMoney(87654321.1), QString("$ 87,654,321.10"));
0287     QCOMPARE(locale.formatMoney(987654321.1), QString("$ 987,654,321.10"));
0288 
0289     QCOMPARE(locale.formatMoney(1.12), QString("$ 1.12"));
0290     QCOMPARE(locale.formatMoney(21.12), QString("$ 21.12"));
0291     QCOMPARE(locale.formatMoney(321.12), QString("$ 321.12"));
0292     QCOMPARE(locale.formatMoney(4321.12), QString("$ 4,321.12"));
0293     QCOMPARE(locale.formatMoney(54321.12), QString("$ 54,321.12"));
0294     QCOMPARE(locale.formatMoney(654321.12), QString("$ 654,321.12"));
0295     QCOMPARE(locale.formatMoney(7654321.12), QString("$ 7,654,321.12"));
0296     QCOMPARE(locale.formatMoney(87654321.12), QString("$ 87,654,321.12"));
0297     QCOMPARE(locale.formatMoney(987654321.12), QString("$ 987,654,321.12"));
0298 
0299     QCOMPARE(locale.formatMoney(1.12), QString("$ 1.12"));
0300     QCOMPARE(locale.formatMoney(21.12), QString("$ 21.12"));
0301     QCOMPARE(locale.formatMoney(321.12), QString("$ 321.12"));
0302     QCOMPARE(locale.formatMoney(4321.12), QString("$ 4,321.12"));
0303     QCOMPARE(locale.formatMoney(54321.12), QString("$ 54,321.12"));
0304     QCOMPARE(locale.formatMoney(654321.12), QString("$ 654,321.12"));
0305     QCOMPARE(locale.formatMoney(7654321.12), QString("$ 7,654,321.12"));
0306     QCOMPARE(locale.formatMoney(87654321.12), QString("$ 87,654,321.12"));
0307     QCOMPARE(locale.formatMoney(987654321.12), QString("$ 987,654,321.12"));
0308 
0309     QCOMPARE(locale.formatMoney(1.123), QString("$ 1.12"));
0310     QCOMPARE(locale.formatMoney(21.123), QString("$ 21.12"));
0311     QCOMPARE(locale.formatMoney(321.123), QString("$ 321.12"));
0312     QCOMPARE(locale.formatMoney(4321.123), QString("$ 4,321.12"));
0313     QCOMPARE(locale.formatMoney(54321.123), QString("$ 54,321.12"));
0314     QCOMPARE(locale.formatMoney(654321.123), QString("$ 654,321.12"));
0315     QCOMPARE(locale.formatMoney(7654321.123), QString("$ 7,654,321.12"));
0316     QCOMPARE(locale.formatMoney(87654321.123), QString("$ 87,654,321.12"));
0317     QCOMPARE(locale.formatMoney(987654321.123), QString("$ 987,654,321.12"));
0318 
0319     QCOMPARE(locale.formatMoney(1.129), QString("$ 1.13"));
0320     QCOMPARE(locale.formatMoney(21.129), QString("$ 21.13"));
0321     QCOMPARE(locale.formatMoney(321.129), QString("$ 321.13"));
0322     QCOMPARE(locale.formatMoney(4321.129), QString("$ 4,321.13"));
0323     QCOMPARE(locale.formatMoney(54321.129), QString("$ 54,321.13"));
0324     QCOMPARE(locale.formatMoney(654321.129), QString("$ 654,321.13"));
0325     QCOMPARE(locale.formatMoney(7654321.129), QString("$ 7,654,321.13"));
0326     QCOMPARE(locale.formatMoney(87654321.129), QString("$ 87,654,321.13"));
0327     QCOMPARE(locale.formatMoney(987654321.129), QString("$ 987,654,321.13"));
0328 
0329     QCOMPARE(locale.formatMoney(-1), QString("$ -1.00"));
0330     QCOMPARE(locale.formatMoney(-21), QString("$ -21.00"));
0331     QCOMPARE(locale.formatMoney(-321), QString("$ -321.00"));
0332     QCOMPARE(locale.formatMoney(-4321), QString("$ -4,321.00"));
0333     QCOMPARE(locale.formatMoney(-54321), QString("$ -54,321.00"));
0334     QCOMPARE(locale.formatMoney(-654321), QString("$ -654,321.00"));
0335     QCOMPARE(locale.formatMoney(-7654321), QString("$ -7,654,321.00"));
0336     QCOMPARE(locale.formatMoney(-87654321), QString("$ -87,654,321.00"));
0337     QCOMPARE(locale.formatMoney(-987654321), QString("$ -987,654,321.00"));
0338 
0339     QCOMPARE(locale.formatMoney(-1.1), QString("$ -1.10"));
0340     QCOMPARE(locale.formatMoney(-21.1), QString("$ -21.10"));
0341     QCOMPARE(locale.formatMoney(-321.1), QString("$ -321.10"));
0342     QCOMPARE(locale.formatMoney(-4321.1), QString("$ -4,321.10"));
0343     QCOMPARE(locale.formatMoney(-54321.1), QString("$ -54,321.10"));
0344     QCOMPARE(locale.formatMoney(-654321.1), QString("$ -654,321.10"));
0345     QCOMPARE(locale.formatMoney(-7654321.1), QString("$ -7,654,321.10"));
0346     QCOMPARE(locale.formatMoney(-87654321.1), QString("$ -87,654,321.10"));
0347     QCOMPARE(locale.formatMoney(-987654321.1), QString("$ -987,654,321.10"));
0348 
0349     QCOMPARE(locale.formatMoney(-1.12), QString("$ -1.12"));
0350     QCOMPARE(locale.formatMoney(-21.12), QString("$ -21.12"));
0351     QCOMPARE(locale.formatMoney(-321.12), QString("$ -321.12"));
0352     QCOMPARE(locale.formatMoney(-4321.12), QString("$ -4,321.12"));
0353     QCOMPARE(locale.formatMoney(-54321.12), QString("$ -54,321.12"));
0354     QCOMPARE(locale.formatMoney(-654321.12), QString("$ -654,321.12"));
0355     QCOMPARE(locale.formatMoney(-7654321.12), QString("$ -7,654,321.12"));
0356     QCOMPARE(locale.formatMoney(-87654321.12), QString("$ -87,654,321.12"));
0357     QCOMPARE(locale.formatMoney(-987654321.12), QString("$ -987,654,321.12"));
0358 
0359     QCOMPARE(locale.formatMoney(-1.12), QString("$ -1.12"));
0360     QCOMPARE(locale.formatMoney(-21.12), QString("$ -21.12"));
0361     QCOMPARE(locale.formatMoney(-321.12), QString("$ -321.12"));
0362     QCOMPARE(locale.formatMoney(-4321.12), QString("$ -4,321.12"));
0363     QCOMPARE(locale.formatMoney(-54321.12), QString("$ -54,321.12"));
0364     QCOMPARE(locale.formatMoney(-654321.12), QString("$ -654,321.12"));
0365     QCOMPARE(locale.formatMoney(-7654321.12), QString("$ -7,654,321.12"));
0366     QCOMPARE(locale.formatMoney(-87654321.12), QString("$ -87,654,321.12"));
0367     QCOMPARE(locale.formatMoney(-987654321.12), QString("$ -987,654,321.12"));
0368 
0369     QCOMPARE(locale.formatMoney(-1.123), QString("$ -1.12"));
0370     QCOMPARE(locale.formatMoney(-21.123), QString("$ -21.12"));
0371     QCOMPARE(locale.formatMoney(-321.123), QString("$ -321.12"));
0372     QCOMPARE(locale.formatMoney(-4321.123), QString("$ -4,321.12"));
0373     QCOMPARE(locale.formatMoney(-54321.123), QString("$ -54,321.12"));
0374     QCOMPARE(locale.formatMoney(-654321.123), QString("$ -654,321.12"));
0375     QCOMPARE(locale.formatMoney(-7654321.123), QString("$ -7,654,321.12"));
0376     QCOMPARE(locale.formatMoney(-87654321.123), QString("$ -87,654,321.12"));
0377     QCOMPARE(locale.formatMoney(-987654321.123), QString("$ -987,654,321.12"));
0378 
0379     QCOMPARE(locale.formatMoney(-1.129), QString("$ -1.13"));
0380     QCOMPARE(locale.formatMoney(-21.129), QString("$ -21.13"));
0381     QCOMPARE(locale.formatMoney(-321.129), QString("$ -321.13"));
0382     QCOMPARE(locale.formatMoney(-4321.129), QString("$ -4,321.13"));
0383     QCOMPARE(locale.formatMoney(-54321.129), QString("$ -54,321.13"));
0384     QCOMPARE(locale.formatMoney(-654321.129), QString("$ -654,321.13"));
0385     QCOMPARE(locale.formatMoney(-7654321.129), QString("$ -7,654,321.13"));
0386     QCOMPARE(locale.formatMoney(-87654321.129), QString("$ -87,654,321.13"));
0387     QCOMPARE(locale.formatMoney(-987654321.129), QString("$ -987,654,321.13"));
0388 
0389     // Test override defaults
0390     QCOMPARE(locale.formatMoney(987654321.12, "£",   0), QString("£ 987,654,321"));
0391     QCOMPARE(locale.formatMoney(987654321.12, "USD", 4), QString("USD 987,654,321.1200"));
0392 
0393     // Test Grouping
0394     locale.d->setMonetaryDigitGrouping(QList<int>());
0395     QCOMPARE(locale.formatMoney(987654321.12), QString("$ 987654321.12"));
0396     QCOMPARE(locale.formatMoney(-987654321.12), QString("$ -987654321.12"));
0397     locale.d->setMonetaryDigitGrouping(QList<int>() << 3 << 2);
0398     QCOMPARE(locale.formatMoney(987654321.12), QString("$ 98,76,54,321.12"));
0399     QCOMPARE(locale.formatMoney(-987654321.12), QString("$ -98,76,54,321.12"));
0400     locale.d->setMonetaryDigitGrouping(QList<int>() << 3 << -1);
0401     QCOMPARE(locale.formatMoney(987654321.12), QString("$ 987654,321.12"));
0402     QCOMPARE(locale.formatMoney(-987654321.12), QString("$ -987654,321.12"));
0403 
0404     // Test symbol and sign position options
0405     locale.setPositiveSign("+");
0406     locale.setNegativeSign("-");
0407     locale.d->setMonetaryDigitGrouping(QList<int>() << 3);
0408 
0409     locale.setPositiveMonetarySignPosition(KLocale::ParensAround);
0410     locale.setNegativeMonetarySignPosition(KLocale::ParensAround);
0411     locale.setPositivePrefixCurrencySymbol(true);
0412     locale.setNegativePrefixCurrencySymbol(true);
0413     QCOMPARE(locale.formatMoney(987654321.12), QString("$ (987,654,321.12)"));
0414     QCOMPARE(locale.formatMoney(-987654321.12), QString("$ (987,654,321.12)"));
0415     locale.setPositivePrefixCurrencySymbol(false);
0416     locale.setNegativePrefixCurrencySymbol(false);
0417     QCOMPARE(locale.formatMoney(987654321.12), QString("(987,654,321.12) $"));
0418     QCOMPARE(locale.formatMoney(-987654321.12), QString("(987,654,321.12) $"));
0419 
0420     locale.setPositiveMonetarySignPosition(KLocale::BeforeQuantityMoney);
0421     locale.setNegativeMonetarySignPosition(KLocale::BeforeQuantityMoney);
0422     locale.setPositivePrefixCurrencySymbol(true);
0423     locale.setNegativePrefixCurrencySymbol(true);
0424     QCOMPARE(locale.formatMoney(987654321.12), QString("$ +987,654,321.12"));
0425     QCOMPARE(locale.formatMoney(-987654321.12), QString("$ -987,654,321.12"));
0426     locale.setPositivePrefixCurrencySymbol(false);
0427     locale.setNegativePrefixCurrencySymbol(false);
0428     QCOMPARE(locale.formatMoney(987654321.12), QString("+987,654,321.12 $"));
0429     QCOMPARE(locale.formatMoney(-987654321.12), QString("-987,654,321.12 $"));
0430 
0431     locale.setPositiveMonetarySignPosition(KLocale::AfterQuantityMoney);
0432     locale.setNegativeMonetarySignPosition(KLocale::AfterQuantityMoney);
0433     locale.setPositivePrefixCurrencySymbol(true);
0434     locale.setNegativePrefixCurrencySymbol(true);
0435     QCOMPARE(locale.formatMoney(987654321.12), QString("$ 987,654,321.12+"));
0436     QCOMPARE(locale.formatMoney(-987654321.12), QString("$ 987,654,321.12-"));
0437     locale.setPositivePrefixCurrencySymbol(false);
0438     locale.setNegativePrefixCurrencySymbol(false);
0439     QCOMPARE(locale.formatMoney(987654321.12), QString("987,654,321.12+ $"));
0440     QCOMPARE(locale.formatMoney(-987654321.12), QString("987,654,321.12- $"));
0441 
0442     locale.setPositiveMonetarySignPosition(KLocale::BeforeMoney);
0443     locale.setNegativeMonetarySignPosition(KLocale::BeforeMoney);
0444     locale.setPositivePrefixCurrencySymbol(true);
0445     locale.setNegativePrefixCurrencySymbol(true);
0446     QCOMPARE(locale.formatMoney(987654321.12), QString("+$ 987,654,321.12"));
0447     QCOMPARE(locale.formatMoney(-987654321.12), QString("-$ 987,654,321.12"));
0448     locale.setPositivePrefixCurrencySymbol(false);
0449     locale.setNegativePrefixCurrencySymbol(false);
0450     QCOMPARE(locale.formatMoney(987654321.12), QString("987,654,321.12 +$"));
0451     QCOMPARE(locale.formatMoney(-987654321.12), QString("987,654,321.12 -$"));
0452 
0453     locale.setPositiveMonetarySignPosition(KLocale::AfterMoney);
0454     locale.setNegativeMonetarySignPosition(KLocale::AfterMoney);
0455     locale.setPositivePrefixCurrencySymbol(true);
0456     locale.setNegativePrefixCurrencySymbol(true);
0457     QCOMPARE(locale.formatMoney(987654321.12), QString("$+ 987,654,321.12"));
0458     QCOMPARE(locale.formatMoney(-987654321.12), QString("$- 987,654,321.12"));
0459     locale.setPositivePrefixCurrencySymbol(false);
0460     locale.setNegativePrefixCurrencySymbol(false);
0461     QCOMPARE(locale.formatMoney(987654321.12), QString("987,654,321.12 $+"));
0462     QCOMPARE(locale.formatMoney(-987654321.12), QString("987,654,321.12 $-"));
0463 }
0464 
0465 void KLocaleTest::readMoney()
0466 {
0467     bool ok = false;
0468     KLocale locale(*KLocale::global());
0469     locale.setPositiveSign(QString());
0470     locale.setNegativeSign("-");
0471     locale.setPositivePrefixCurrencySymbol(true);
0472     locale.setNegativePrefixCurrencySymbol(true);
0473     locale.setMonetaryDecimalPlaces(2);
0474     locale.setMonetaryThousandsSeparator(",");
0475     locale.setMonetaryDecimalSymbol(".");
0476     locale.setCurrencySymbol("$");
0477     locale.setPositiveMonetarySignPosition(KLocale::BeforeQuantityMoney);
0478     locale.setNegativeMonetarySignPosition(KLocale::BeforeQuantityMoney);
0479     locale.d->setMonetaryDigitGrouping(QList<int>() << 3);
0480 
0481     // Basic grouping, decimal place and rounding tests
0482     QCOMPARE(locale.readMoney("$ 1.12", &ok),         1.12);
0483     QVERIFY(ok);
0484     QCOMPARE(locale.readMoney("$ 21.12", &ok),        21.12);
0485     QVERIFY(ok);
0486     QCOMPARE(locale.readMoney("$ 321.12", &ok),       321.12);
0487     QVERIFY(ok);
0488     QCOMPARE(locale.readMoney("$ 4,321.12", &ok),      4321.12);
0489     QVERIFY(ok);
0490     QCOMPARE(locale.readMoney("$ 54,321.12", &ok),     54321.12);
0491     QVERIFY(ok);
0492     QCOMPARE(locale.readMoney("$ 654,321.12", &ok),    654321.12);
0493     QVERIFY(ok);
0494     QCOMPARE(locale.readMoney("$ 7,654,321.12", &ok),   7654321.12);
0495     QVERIFY(ok);
0496     QCOMPARE(locale.readMoney("$ 87,654,321.12", &ok),  87654321.12);
0497     QVERIFY(ok);
0498     QCOMPARE(locale.readMoney("$ 987,654,321.12", &ok), 987654321.12);
0499     QVERIFY(ok);
0500     QCOMPARE(locale.readMoney("$ 987654321.12", &ok), 987654321.12);
0501     QVERIFY(ok);
0502     QCOMPARE(locale.readMoney("$ 987654,321.12", &ok), 0.0);
0503     QVERIFY(!ok);
0504 
0505     QCOMPARE(locale.readMoney("$ -1.12", &ok),         -1.12);
0506     QVERIFY(ok);
0507     QCOMPARE(locale.readMoney("$ -21.12", &ok),        -21.12);
0508     QVERIFY(ok);
0509     QCOMPARE(locale.readMoney("$ -321.12", &ok),       -321.12);
0510     QVERIFY(ok);
0511     QCOMPARE(locale.readMoney("$ -4,321.12", &ok),      -4321.12);
0512     QVERIFY(ok);
0513     QCOMPARE(locale.readMoney("$ -54,321.12", &ok),     -54321.12);
0514     QVERIFY(ok);
0515     QCOMPARE(locale.readMoney("$ -654,321.12", &ok),    -654321.12);
0516     QVERIFY(ok);
0517     QCOMPARE(locale.readMoney("$ -7,654,321.12", &ok),   -7654321.12);
0518     QVERIFY(ok);
0519     QCOMPARE(locale.readMoney("$ -87,654,321.12", &ok),  -87654321.12);
0520     QVERIFY(ok);
0521     QCOMPARE(locale.readMoney("$ -987,654,321.12", &ok), -987654321.12);
0522     QVERIFY(ok);
0523     QCOMPARE(locale.readMoney("$ -987654321.12", &ok), -987654321.12);
0524     QVERIFY(ok);
0525     QCOMPARE(locale.readMoney("$ -987654,321.12", &ok), 0.0);
0526     QVERIFY(!ok);
0527 
0528     // Test incomplete formats
0529     QCOMPARE(locale.readMoney("$ 1", &ok),         1.00);
0530     QVERIFY(ok);
0531     QCOMPARE(locale.readMoney("$ 21", &ok),        21.00);
0532     QVERIFY(ok);
0533     QCOMPARE(locale.readMoney("$ 321", &ok),       321.00);
0534     QVERIFY(ok);
0535     QCOMPARE(locale.readMoney("$ 4,321", &ok),      4321.00);
0536     QVERIFY(ok);
0537     QCOMPARE(locale.readMoney("$ 54,321", &ok),     54321.00);
0538     QVERIFY(ok);
0539     QCOMPARE(locale.readMoney("$ 654,321", &ok),    654321.00);
0540     QVERIFY(ok);
0541     QCOMPARE(locale.readMoney("$ 7,654,321", &ok),   7654321.00);
0542     QVERIFY(ok);
0543     QCOMPARE(locale.readMoney("$ 87,654,321", &ok),  87654321.00);
0544     QVERIFY(ok);
0545     QCOMPARE(locale.readMoney("$ 987,654,321", &ok), 987654321.00);
0546     QVERIFY(ok);
0547 
0548     // Test Grouping
0549     locale.d->setMonetaryDigitGrouping(QList<int>());
0550     QCOMPARE(locale.readMoney("$ 987654321.12", &ok),  987654321.12);
0551     QVERIFY(ok);
0552     QCOMPARE(locale.readMoney("$ -987654321.12", &ok), -987654321.12);
0553     QVERIFY(ok);
0554     locale.d->setMonetaryDigitGrouping(QList<int>() << 3 << 2);
0555     QCOMPARE(locale.readMoney("$ 98,76,54,321.12", &ok),  987654321.12);
0556     QVERIFY(ok);
0557     QCOMPARE(locale.readMoney("$ -98,76,54,321.12", &ok), -987654321.12);
0558     QVERIFY(ok);
0559     QCOMPARE(locale.readMoney("$ 987654321.12", &ok),  987654321.12);
0560     QVERIFY(ok);
0561     QCOMPARE(locale.readMoney("$ -987654321.12", &ok), -987654321.12);
0562     QVERIFY(ok);
0563     locale.d->setMonetaryDigitGrouping(QList<int>() << 3 << -1);
0564     QCOMPARE(locale.readMoney("$ 987654,321.12", &ok),  987654321.12);
0565     QVERIFY(ok);
0566     QCOMPARE(locale.readMoney("$ -987654,321.12", &ok), -987654321.12);
0567     QVERIFY(ok);
0568 
0569     // Test signs
0570     locale.setPositiveSign("@");
0571     locale.setNegativeSign("&");
0572     locale.d->setMonetaryDigitGrouping(QList<int>() << 3);
0573 
0574     QCOMPARE(locale.readMoney(QString("$ @123,456,789.12"),  &ok),  123456789.12);
0575     QVERIFY(ok);
0576     QCOMPARE(locale.readMoney(QString("$ 123,456,789.12@"), &ok),  123456789.12);
0577     QVERIFY(ok);
0578     QCOMPARE(locale.readMoney(QString("$ &123,456,789.12"),  &ok), -123456789.12);
0579     QVERIFY(ok);
0580     QCOMPARE(locale.readMoney(QString("$ 123,456,789.12&"), &ok), -123456789.12);
0581     QVERIFY(ok);
0582     QCOMPARE(locale.readMoney(QString("$ 123,456,789.12"),  &ok),  123456789.12);
0583     QVERIFY(ok);
0584     QCOMPARE(locale.readMoney(QString("$ +123,456,789.12"),  &ok), 0.0);
0585     QVERIFY(!ok);
0586     QCOMPARE(locale.readMoney(QString("$ 123,456,789.12+"), &ok), 0.0);
0587     QVERIFY(!ok);
0588     QCOMPARE(locale.readMoney(QString("$ -123,456,789.12"),  &ok), 0.0);
0589     QVERIFY(!ok);
0590     QCOMPARE(locale.readMoney(QString("$ 123,456,789.12-"), &ok), 0.0);
0591     QVERIFY(!ok);
0592     locale.setNegativeSign(QString());
0593     QCOMPARE(locale.readMoney(QString("$ 123,456,789.12"),  &ok), -123456789.12);
0594     QVERIFY(ok);
0595     QCOMPARE(locale.readMoney(QString("$ -123,456,789.12"),  &ok), 0.0);
0596     QVERIFY(!ok);
0597     QCOMPARE(locale.readMoney(QString("$ 123,456,789.12-"), &ok), 0.0);
0598     QVERIFY(!ok);
0599 
0600     // Test symbol and sign position options
0601     locale.setPositiveSign("+");
0602     locale.setNegativeSign("-");
0603 
0604     locale.setPositiveMonetarySignPosition(KLocale::ParensAround);
0605     locale.setNegativeMonetarySignPosition(KLocale::BeforeQuantityMoney);
0606     locale.setPositivePrefixCurrencySymbol(true);
0607     QCOMPARE(locale.readMoney("$ (987,654,321.12)", &ok),  987654321.12);
0608     QVERIFY(ok);
0609     locale.setPositivePrefixCurrencySymbol(false);
0610     QCOMPARE(locale.readMoney("(987,654,321.12) $", &ok),  987654321.12);
0611     QVERIFY(ok);
0612 
0613     locale.setPositiveMonetarySignPosition(KLocale::BeforeQuantityMoney);
0614     locale.setNegativeMonetarySignPosition(KLocale::ParensAround);
0615     locale.setNegativePrefixCurrencySymbol(true);
0616     QCOMPARE(locale.readMoney("$ (987,654,321.12)", &ok), -987654321.12);
0617     QVERIFY(ok);
0618     locale.setNegativePrefixCurrencySymbol(false);
0619     QCOMPARE(locale.readMoney("(987,654,321.12) $", &ok), -987654321.12);
0620     QVERIFY(ok);
0621 
0622     locale.setPositiveMonetarySignPosition(KLocale::BeforeQuantityMoney);
0623     locale.setNegativeMonetarySignPosition(KLocale::BeforeQuantityMoney);
0624     locale.setPositivePrefixCurrencySymbol(true);
0625     locale.setNegativePrefixCurrencySymbol(true);
0626     QCOMPARE(locale.readMoney("$ +987,654,321.12", &ok),  987654321.12);
0627     QVERIFY(ok);
0628     QCOMPARE(locale.readMoney("$ -987,654,321.12", &ok), -987654321.12);
0629     QVERIFY(ok);
0630     locale.setPositivePrefixCurrencySymbol(false);
0631     locale.setNegativePrefixCurrencySymbol(false);
0632     QCOMPARE(locale.readMoney("+987,654,321.12 $", &ok),  987654321.12);
0633     QVERIFY(ok);
0634     QCOMPARE(locale.readMoney("-987,654,321.12 $", &ok), -987654321.12);
0635     QVERIFY(ok);
0636 
0637     locale.setPositiveMonetarySignPosition(KLocale::AfterQuantityMoney);
0638     locale.setNegativeMonetarySignPosition(KLocale::AfterQuantityMoney);
0639     locale.setPositivePrefixCurrencySymbol(true);
0640     locale.setNegativePrefixCurrencySymbol(true);
0641     QCOMPARE(locale.readMoney("$ 987,654,321.12+", &ok),  987654321.12);
0642     QVERIFY(ok);
0643     QCOMPARE(locale.readMoney("$ 987,654,321.12-", &ok), -987654321.12);
0644     QVERIFY(ok);
0645     locale.setPositivePrefixCurrencySymbol(false);
0646     locale.setNegativePrefixCurrencySymbol(false);
0647     QCOMPARE(locale.readMoney("987,654,321.12+ $", &ok),  987654321.12);
0648     QVERIFY(ok);
0649     QCOMPARE(locale.readMoney("987,654,321.12- $", &ok), -987654321.12);
0650     QVERIFY(ok);
0651 
0652     locale.setPositiveMonetarySignPosition(KLocale::BeforeMoney);
0653     locale.setNegativeMonetarySignPosition(KLocale::BeforeMoney);
0654     locale.setPositivePrefixCurrencySymbol(true);
0655     locale.setNegativePrefixCurrencySymbol(true);
0656     QCOMPARE(locale.readMoney("+$ 987,654,321.12", &ok),  987654321.12);
0657     QVERIFY(ok);
0658     QCOMPARE(locale.readMoney("-$ 987,654,321.12", &ok), -987654321.12);
0659     QVERIFY(ok);
0660     locale.setPositivePrefixCurrencySymbol(false);
0661     locale.setNegativePrefixCurrencySymbol(false);
0662     QCOMPARE(locale.readMoney("987,654,321.12 +$", &ok),  987654321.12);
0663     QVERIFY(ok);
0664     QCOMPARE(locale.readMoney("987,654,321.12 -$", &ok), -987654321.12);
0665     QVERIFY(ok);
0666 
0667     locale.setPositiveMonetarySignPosition(KLocale::AfterMoney);
0668     locale.setNegativeMonetarySignPosition(KLocale::AfterMoney);
0669     locale.setPositivePrefixCurrencySymbol(true);
0670     locale.setNegativePrefixCurrencySymbol(true);
0671     QCOMPARE(locale.readMoney("$+ 987,654,321.12", &ok),  987654321.12);
0672     QVERIFY(ok);
0673     QCOMPARE(locale.readMoney("$- 987,654,321.12", &ok), -987654321.12);
0674     QVERIFY(ok);
0675     locale.setPositivePrefixCurrencySymbol(false);
0676     locale.setNegativePrefixCurrencySymbol(false);
0677     QCOMPARE(locale.readMoney("987,654,321.12 $+", &ok),  987654321.12);
0678     QVERIFY(ok);
0679     QCOMPARE(locale.readMoney("987,654,321.12 $-", &ok), -987654321.12);
0680     QVERIFY(ok);
0681 }
0682 
0683 void
0684 KLocaleTest::formatDate()
0685 {
0686     KLocale locale(*KLocale::global());
0687     QString small("yyyy-MM-dd");
0688     QString full("dddd dd MMMM yyyy");
0689     QDate date;
0690 
0691     // Ensure that user configuration isn't messing with us;
0692     // shouldn't happen though, due to QStandardPaths::setTestModeEnabled(true)
0693     QCOMPARE(locale.dateFormat(), QString("%A %d %B %Y"));
0694 
0695     date.setDate(2002, 5, 3);
0696     QCOMPARE(locale.formatDate(date), date.toString(full));
0697     QCOMPARE(locale.formatDate(date, KLocale::ShortDate), date.toString(small));
0698 
0699     date = QDate::currentDate();
0700     QCOMPARE(locale.formatDate(date), date.toString(full));
0701     QCOMPARE(locale.formatDate(date, KLocale::ShortDate), date.toString(small));
0702 
0703     QCOMPARE(locale.formatDate(date, KLocale::FancyLongDate), QString("Today"));
0704     QCOMPARE(locale.formatDate(date.addDays(-1), KLocale::FancyLongDate), QString("Yesterday"));
0705     QDate dat = date.addDays(-2);
0706     QCOMPARE(locale.formatDate(dat, KLocale::FancyLongDate), locale.calendar()->weekDayName(dat));
0707     dat = date.addDays(-3);
0708     QCOMPARE(locale.formatDate(dat, KLocale::FancyLongDate), locale.calendar()->weekDayName(dat));
0709     dat = date.addDays(-4);
0710     QCOMPARE(locale.formatDate(dat, KLocale::FancyLongDate), locale.calendar()->weekDayName(dat));
0711     dat = date.addDays(-5);
0712     QCOMPARE(locale.formatDate(dat, KLocale::FancyLongDate), locale.calendar()->weekDayName(dat));
0713     dat = date.addDays(-6);
0714     QCOMPARE(locale.formatDate(dat, KLocale::FancyLongDate), locale.calendar()->weekDayName(dat));
0715     dat = date.addDays(-7);
0716     QCOMPARE(locale.formatDate(dat, KLocale::FancyLongDate), dat.toString(full));
0717     dat = date.addDays(1);
0718     QCOMPARE(locale.formatDate(dat, KLocale::FancyLongDate), dat.toString(full));
0719 }
0720 
0721 void
0722 KLocaleTest::readDate()
0723 {
0724     KLocale locale(*KLocale::global());
0725     QString small("yyyy-MM-dd");
0726     QString full("dddd dd MMMM yyyy");
0727     bool ok = false;
0728     QDate date;
0729     date.setDate(2002, 5, 3);
0730     QCOMPARE(locale.readDate(date.toString(small)), date);
0731     QCOMPARE(locale.readDate(date.toString(full)), date);
0732     locale.readDate(date.toString(small), KLocale::NormalFormat, &ok);
0733     QVERIFY(!ok);
0734     locale.readDate(date.toString(full), KLocale::ShortFormat, &ok);
0735     QVERIFY(!ok);
0736 
0737     QString twoDigitYear("dd-MM-yy");
0738     QCOMPARE(date.toString(twoDigitYear), QString("03-05-02"));
0739     QCOMPARE(locale.readDate("03-05-02", "%d-%m-%y"), date);
0740 
0741     date = QDate::currentDate();
0742     QCOMPARE(locale.readDate(date.toString(small)), date);
0743     QCOMPARE(locale.readDate(date.toString(full)), date);
0744     locale.readDate(date.toString(small), KLocale::NormalFormat, &ok);
0745     QVERIFY(!ok);
0746     locale.readDate(date.toString(full), KLocale::ShortFormat, &ok);
0747     QVERIFY(!ok);
0748 
0749     locale.readDate("2005-02-31", &ok);
0750     QVERIFY(!ok);
0751 }
0752 
0753 void
0754 KLocaleTest::formatTime()
0755 {
0756     KLocale locale(*KLocale::global());
0757     QTime time(0, 22, 33);
0758 
0759     locale.setTimeFormat("%H:%M %p");
0760     QCOMPARE(locale.formatLocaleTime(time, KLocale::TimeWithoutSeconds), QString("00:22 AM"));
0761     QCOMPARE(locale.formatLocaleTime(time, KLocale::TimeWithoutSeconds | KLocale::TimeDuration),
0762              QString("00:22"));
0763 
0764     locale.setTimeFormat("%H:%M:%S %p");
0765     QCOMPARE(locale.formatLocaleTime(time, KLocale::TimeDefault), QString("00:22:33 AM"));
0766     QCOMPARE(locale.formatLocaleTime(time, KLocale::TimeDuration), QString("00:22:33"));
0767 
0768     locale.setTimeFormat("%l : %M : %S %p"); // #164813
0769     QCOMPARE(locale.formatLocaleTime(time, KLocale::TimeDefault), QString("12 : 22 : 33 AM"));
0770     QCOMPARE(locale.formatLocaleTime(time, KLocale::TimeWithoutSeconds), QString("12 : 22 AM"));
0771 }
0772 
0773 void
0774 KLocaleTest::formatDateTime()
0775 {
0776     KLocale locale(*KLocale::global());
0777     QString small("yyyy-MM-dd hh:mm");
0778     QString smallsecs("yyyy-MM-dd hh:mm:ss");
0779     QString full("dddd dd MMMM yyyy hh:mm");
0780     QString fullsecs("dddd dd MMMM yyyy hh:mm:ss");
0781     QString tfmt(" hh:mm");
0782     QDateTime qdt;
0783 
0784     // Ensure that user configuration isn't messing with us;
0785     // shouldn't happen though, due to QStandardPaths::setTestModeEnabled(true)
0786     QCOMPARE(locale.dateFormat(), QString("%A %d %B %Y"));
0787 
0788     qdt = QDateTime(QDate(2002, 5, 3), QTime(10, 20, 30));
0789     QCOMPARE(locale.formatDateTime(qdt), qdt.toString(small));
0790     QCOMPARE(locale.formatDateTime(qdt, KLocale::LongDate), qdt.toString(full));
0791 
0792     qdt = QDateTime::currentDateTime();
0793     QDate today = qdt.date();
0794     QTime nowt = qdt.time();
0795     QCOMPARE(locale.formatDateTime(qdt), qdt.toString(small));
0796     QCOMPARE(locale.formatDateTime(qdt, KLocale::LongDate), qdt.toString(full));
0797 
0798     QCOMPARE(locale.formatDateTime(qdt, KLocale::FancyLongDate), QString("Today" + qdt.time().toString(tfmt)));
0799     qdt = qdt.addSecs(3605);  // more than 1 hour from now
0800     QCOMPARE(locale.formatDateTime(qdt, KLocale::FancyLongDate), qdt.toString(full));
0801     qdt.setDate(today);
0802     qdt.setTime(QTime(0, 0, 0));
0803     QCOMPARE(locale.formatDateTime(qdt, KLocale::FancyLongDate), QString("Today" + qdt.time().toString(tfmt)));
0804     qdt = qdt.addSecs(-1);
0805     QCOMPARE(locale.formatDateTime(qdt, KLocale::FancyLongDate), QString("Yesterday" + qdt.time().toString(tfmt)));
0806     qdt = qdt.addDays(2);   // tomorrow
0807     qdt.setTime(nowt);
0808     QCOMPARE(locale.formatDateTime(qdt, KLocale::FancyLongDate), qdt.toString(full));
0809     qdt = qdt.addDays(-2);   // yesterday
0810     QCOMPARE(locale.formatDateTime(qdt, KLocale::FancyLongDate), QString("Yesterday" + qdt.time().toString(tfmt)));
0811     qdt = qdt.addDays(-1);
0812     QCOMPARE(locale.formatDateTime(qdt, KLocale::FancyLongDate), QString(locale.calendar()->weekDayName(qdt.date()) + qdt.time().toString(tfmt)));
0813     qdt = qdt.addDays(-1);
0814     QCOMPARE(locale.formatDateTime(qdt, KLocale::FancyLongDate), QString(locale.calendar()->weekDayName(qdt.date()) + qdt.time().toString(tfmt)));
0815     qdt = qdt.addDays(-1);
0816     QCOMPARE(locale.formatDateTime(qdt, KLocale::FancyLongDate), QString(locale.calendar()->weekDayName(qdt.date()) + qdt.time().toString(tfmt)));
0817     qdt = qdt.addDays(-1);
0818     QCOMPARE(locale.formatDateTime(qdt, KLocale::FancyLongDate), QString(locale.calendar()->weekDayName(qdt.date()) + qdt.time().toString(tfmt)));
0819     qdt = qdt.addDays(-1);
0820     QCOMPARE(locale.formatDateTime(qdt, KLocale::FancyLongDate), QString(locale.calendar()->weekDayName(qdt.date()) + qdt.time().toString(tfmt)));
0821     qdt = qdt.addDays(-1);
0822     QCOMPARE(locale.formatDateTime(qdt, KLocale::FancyLongDate), qdt.toString(full));
0823 
0824     // The use of KSystemTimeZones requires kded to be running
0825     if (!QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.kded5")) {
0826         QSKIP("kded not running");
0827     }
0828 
0829     small = "%Y-%m-%d %H:%M";
0830     smallsecs = "%Y-%m-%d %H:%M:%S";
0831     full = "%A %d %B %Y %H:%M";
0832     fullsecs = "%A %d %B %Y %H:%M:%S";
0833     KDateTime kdt;
0834     const KTimeZone tz = KSystemTimeZones::zone("Pacific/Fiji");
0835     if (!tz.isValid()) {
0836         QSKIP("Pacific/Fiji timezone not available");
0837     }
0838     kdt = KDateTime::currentDateTime(tz);
0839     today = kdt.date();
0840     nowt = kdt.time();
0841     QCOMPARE(locale.formatDateTime(kdt), kdt.toString(small));
0842 //  QCOMPARE(locale.formatDateTime(kdt, KLocale::ShortDate, KLocale::Seconds), kdt.toString(smallsecs));
0843     QCOMPARE(locale.formatDateTime(kdt, KLocale::LongDate), kdt.toString(full));
0844 //  QCOMPARE(locale.formatDateTime(kdt, KLocale::LongDate, KLocale::Seconds), kdt.toString(fullsecs));
0845 
0846 //  QCOMPARE(locale.formatDateTime(kdt, KLocale::FancyLongDate), QString("Today") + kdt.time().toString(tfmt));
0847     kdt = kdt.addSecs(3605);  // more than 1 hour from now
0848     QCOMPARE(locale.formatDateTime(kdt, KLocale::FancyLongDate), kdt.toString(full));
0849     kdt.setDate(today);
0850     kdt.setTime(QTime(0, 0, 0));
0851     QCOMPARE(locale.formatDateTime(kdt, KLocale::FancyLongDate), QString("Today" + kdt.time().toString(tfmt)));
0852     kdt = kdt.addSecs(-1);
0853     QCOMPARE(locale.formatDateTime(kdt, KLocale::FancyLongDate), QString("Yesterday" + kdt.time().toString(tfmt)));
0854     kdt = kdt.addDays(2);   // tomorrow
0855     kdt.setTime(nowt);
0856     QCOMPARE(locale.formatDateTime(kdt, KLocale::FancyLongDate), kdt.toString(full));
0857     kdt = kdt.addDays(-2);   // yesterday
0858     QCOMPARE(locale.formatDateTime(kdt, KLocale::FancyLongDate), QString("Yesterday" + kdt.time().toString(tfmt)));
0859     kdt = kdt.addDays(-1);
0860     QCOMPARE(locale.formatDateTime(kdt, KLocale::FancyLongDate), QString(locale.calendar()->weekDayName(kdt.date()) + kdt.time().toString(tfmt)));
0861     kdt = kdt.addDays(-1);
0862     QCOMPARE(locale.formatDateTime(kdt, KLocale::FancyLongDate), QString(locale.calendar()->weekDayName(kdt.date()) + kdt.time().toString(tfmt)));
0863     kdt = kdt.addDays(-1);
0864     QCOMPARE(locale.formatDateTime(kdt, KLocale::FancyLongDate), QString(locale.calendar()->weekDayName(kdt.date()) + kdt.time().toString(tfmt)));
0865     kdt = kdt.addDays(-1);
0866     QCOMPARE(locale.formatDateTime(kdt, KLocale::FancyLongDate), QString(locale.calendar()->weekDayName(kdt.date()) + kdt.time().toString(tfmt)));
0867     kdt = kdt.addDays(-1);
0868     QCOMPARE(locale.formatDateTime(kdt, KLocale::FancyLongDate), QString(locale.calendar()->weekDayName(kdt.date()) + kdt.time().toString(tfmt)));
0869     kdt = kdt.addDays(-1);
0870     QCOMPARE(locale.formatDateTime(kdt, KLocale::FancyLongDate), kdt.toString(full));
0871 }
0872 
0873 void
0874 KLocaleTest::readTime()
0875 {
0876     KLocale locale(*KLocale::global());
0877     bool ok = false;
0878 
0879     QCOMPARE(locale.readTime("11:22:33", &ok), QTime(11, 22, 33));
0880     QVERIFY(ok);
0881 
0882     QCOMPARE(locale.readTime("11:22", &ok), QTime(11, 22, 0));
0883     QVERIFY(ok);
0884 
0885     locale.readLocaleTime("11:22:33", &ok, KLocale::TimeWithoutSeconds);
0886     QVERIFY(!ok);
0887 
0888     QCOMPARE(locale.readLocaleTime("11:22", &ok, KLocale::TimeWithoutSeconds), QTime(11, 22, 0));
0889     QVERIFY(ok);
0890 
0891     // strict processing of a time string with a missing space
0892     locale.setTimeFormat("%I:%M:%S %p");
0893     QString timeString = QString("11:13:55%1").arg(locale.d->dayPeriodForTime(QTime(11, 13, 55)).periodName());
0894     locale.readLocaleTime(timeString, &ok, KLocale::TimeDefault, KLocale::ProcessStrict);
0895     QVERIFY(!ok);
0896 
0897     // lax processing of the same time
0898     QCOMPARE(locale.readLocaleTime(timeString, &ok, KLocale::TimeDefault), QTime(11, 13, 55));
0899     QVERIFY(ok);
0900 }
0901 
0902 void KLocaleTest::testDayPeriods()
0903 {
0904     KLocale locale(*KLocale::global());
0905     locale.setTimeFormat("%I:%M:%S %p");
0906 
0907     //Test default standard AM/PM Day Periods
0908     KDayPeriod testPeriod;
0909     QCOMPARE(testPeriod.isValid(), false);
0910     testPeriod = locale.d->dayPeriodForTime(QTime(03, 00, 00));
0911     QCOMPARE(testPeriod.isValid(), true);
0912     QCOMPARE(testPeriod.periodName(KLocale::LongName), QString("Ante Meridiem"));
0913     QCOMPARE(testPeriod.periodName(KLocale::ShortName), QString("AM"));
0914     QCOMPARE(testPeriod.periodName(KLocale::NarrowName), QString("A"));
0915     QCOMPARE(testPeriod.periodStart(), QTime(0, 0, 0));
0916     QCOMPARE(testPeriod.periodEnd(), QTime(11, 59, 59, 999));
0917     QCOMPARE(testPeriod.hourInPeriod(QTime(0, 0, 0)), 12);
0918     QCOMPARE(testPeriod.hourInPeriod(QTime(1, 0, 0)),  1);
0919     QCOMPARE(testPeriod.hourInPeriod(QTime(6, 0, 0)),  6);
0920     QCOMPARE(testPeriod.hourInPeriod(QTime(11, 59, 59, 999)), 11);
0921     QCOMPARE(testPeriod.hourInPeriod(QTime(12, 0, 0)), -1);
0922     QCOMPARE(testPeriod.time(12, 0, 0), QTime(0, 0, 0));
0923     QCOMPARE(testPeriod.time(1, 0, 0), QTime(1, 0, 0));
0924     QCOMPARE(testPeriod.time(6, 0, 0), QTime(6, 0, 0));
0925     QCOMPARE(testPeriod.time(11, 59, 59, 999), QTime(11, 59, 59, 999));
0926     QCOMPARE(testPeriod.time(13, 0, 0), QTime());
0927 
0928     testPeriod = locale.d->dayPeriodForTime(QTime(13, 00, 00));
0929     QCOMPARE(testPeriod.isValid(), true);
0930     QCOMPARE(testPeriod.periodName(KLocale::LongName), QString("Post Meridiem"));
0931     QCOMPARE(testPeriod.periodName(KLocale::ShortName), QString("PM"));
0932     QCOMPARE(testPeriod.periodName(KLocale::NarrowName), QString("P"));
0933     QCOMPARE(testPeriod.periodStart(), QTime(12, 0, 0));
0934     QCOMPARE(testPeriod.periodEnd(), QTime(23, 59, 59, 999));
0935     QCOMPARE(testPeriod.hourInPeriod(QTime(11, 59, 59, 999)), -1);
0936     QCOMPARE(testPeriod.hourInPeriod(QTime(12, 0, 0)), 12);
0937     QCOMPARE(testPeriod.hourInPeriod(QTime(13, 0, 0)),  1);
0938     QCOMPARE(testPeriod.hourInPeriod(QTime(18, 0, 0)),  6);
0939     QCOMPARE(testPeriod.hourInPeriod(QTime(23, 59, 59, 999)), 11);
0940     QCOMPARE(testPeriod.hourInPeriod(QTime(24, 0, 0)), -1);
0941     QCOMPARE(testPeriod.time(12, 0, 0), QTime(12, 0, 0));
0942     QCOMPARE(testPeriod.time(1, 0, 0), QTime(13, 0, 0));
0943     QCOMPARE(testPeriod.time(6, 0, 0), QTime(18, 0, 0));
0944     QCOMPARE(testPeriod.time(11, 59, 59, 999), QTime(23, 59, 59, 999));
0945     QCOMPARE(testPeriod.time(13, 0, 0), QTime());
0946 
0947     QCOMPARE(locale.dayPeriodText(QTime(11, 12, 13)), QString("AM"));
0948     QCOMPARE(locale.dayPeriodText(QTime(23, 12, 13)), QString("PM"));
0949     QCOMPARE(locale.formatLocaleTime(QTime(11, 12, 13)), QString("11:12:13 AM"));
0950     QCOMPARE(locale.formatLocaleTime(QTime(23, 12, 13)), QString("11:12:13 PM"));
0951     QCOMPARE(locale.readLocaleTime(QString("11:12:13 AM")), QTime(11, 12, 13));
0952     QCOMPARE(locale.readLocaleTime(QString("11:12:13 PM")), QTime(23, 12, 13));
0953     QCOMPARE(locale.readLocaleTime(QString("11:12:13 am")), QTime(11, 12, 13));
0954     QCOMPARE(locale.readLocaleTime(QString("11:12:13 pm")), QTime(23, 12, 13));
0955 
0956     // Test setting own periods
0957     // Test alternative periods, e.g. thirds of day with absolute hours in each period numbered 1..8
0958     QList<KDayPeriod> dayPeriods;
0959     dayPeriods.append(KDayPeriod("p1", "Early Day", "Early", "E", QTime(0, 0, 0), QTime(7, 59, 59, 999), 1, 0));
0960     dayPeriods.append(KDayPeriod("p2", "Middle Day", "Middle", "M", QTime(8, 0, 0), QTime(15, 59, 59, 999), 1, 0));
0961     dayPeriods.append(KDayPeriod("p3", "Late Day", "Late", "L", QTime(16, 0, 0), QTime(23, 59, 59, 999), 1, 0));
0962     locale.d->setDayPeriods(dayPeriods);
0963     testPeriod = locale.d->dayPeriodForTime(QTime(0, 0, 0));
0964     QCOMPARE(testPeriod.periodName(KLocale::ShortName), QString("Early"));
0965     QCOMPARE(testPeriod.hourInPeriod(QTime(0, 0, 0)), 1);
0966     QCOMPARE(testPeriod.hourInPeriod(QTime(4, 0, 0)), 5);
0967     QCOMPARE(testPeriod.hourInPeriod(QTime(7, 0, 0)), 8);
0968     QCOMPARE(testPeriod.time(0, 0, 0), QTime());
0969     QCOMPARE(testPeriod.time(1, 0, 0), QTime(0, 0, 0));
0970     QCOMPARE(testPeriod.time(5, 0, 0), QTime(4, 0, 0));
0971     QCOMPARE(testPeriod.time(8, 0, 0), QTime(7, 0, 0));
0972     QCOMPARE(testPeriod.time(9, 0, 0), QTime());
0973     testPeriod = locale.d->dayPeriodForTime(QTime(8, 0, 0));
0974     QCOMPARE(testPeriod.periodName(KLocale::ShortName), QString("Middle"));
0975     QCOMPARE(testPeriod.hourInPeriod(QTime(8, 0, 0)), 1);
0976     QCOMPARE(testPeriod.hourInPeriod(QTime(12, 0, 0)), 5);
0977     QCOMPARE(testPeriod.hourInPeriod(QTime(15, 0, 0)), 8);
0978     QCOMPARE(testPeriod.time(0, 0, 0), QTime());
0979     QCOMPARE(testPeriod.time(1, 0, 0), QTime(8, 0, 0));
0980     QCOMPARE(testPeriod.time(5, 0, 0), QTime(12, 0, 0));
0981     QCOMPARE(testPeriod.time(8, 0, 0), QTime(15, 0, 0));
0982     QCOMPARE(testPeriod.time(9, 0, 0), QTime());
0983     testPeriod = locale.d->dayPeriodForTime(QTime(16, 0, 0));
0984     QCOMPARE(testPeriod.periodName(KLocale::ShortName), QString("Late"));
0985     QCOMPARE(testPeriod.hourInPeriod(QTime(16, 0, 0)), 1);
0986     QCOMPARE(testPeriod.hourInPeriod(QTime(20, 0, 0)), 5);
0987     QCOMPARE(testPeriod.hourInPeriod(QTime(23, 0, 0)), 8);
0988     QCOMPARE(testPeriod.time(0, 0, 0), QTime());
0989     QCOMPARE(testPeriod.time(1, 0, 0), QTime(16, 0, 0));
0990     QCOMPARE(testPeriod.time(5, 0, 0), QTime(20, 0, 0));
0991     QCOMPARE(testPeriod.time(8, 0, 0), QTime(23, 0, 0));
0992     QCOMPARE(testPeriod.time(9, 0, 0), QTime());
0993     QCOMPARE(locale.formatLocaleTime(QTime(4, 0, 0)), QString("05:00:00 Early"));
0994     QCOMPARE(locale.formatLocaleTime(QTime(12, 0, 0)), QString("05:00:00 Middle"));
0995     QCOMPARE(locale.formatLocaleTime(QTime(20, 0, 0)), QString("05:00:00 Late"));
0996     QCOMPARE(locale.readLocaleTime(QString("05:00:00 Early")), QTime(4, 0, 0));
0997     QCOMPARE(locale.readLocaleTime(QString("05:00:00 Middle")), QTime(12, 0, 0));
0998     QCOMPARE(locale.readLocaleTime(QString("05:00:00 Late")), QTime(20, 0, 0));
0999 
1000     // Test more conventional alternative periods, e.g. informal periods of day with 12 hour clock
1001     dayPeriods.clear();
1002     dayPeriods.append(KDayPeriod("morning", "Morning", "in the morning", "M", QTime(6, 0, 0), QTime(11, 59, 59, 999), 6, 12));
1003     dayPeriods.append(KDayPeriod("noon", "Noon", "noon", "O", QTime(12, 0, 0), QTime(12, 0, 59, 999), 12, 0));
1004     dayPeriods.append(KDayPeriod("afternoon", "Afternoon", "in the afternoon", "A", QTime(12, 1, 0), QTime(17, 59, 59, 999), 0, 12));
1005     dayPeriods.append(KDayPeriod("evening", "Evening", "in the evening", "E", QTime(18, 0, 0), QTime(21, 59, 59, 999), 6, 12));
1006     dayPeriods.append(KDayPeriod("night", "Night", "at night", "N", QTime(22, 0, 0), QTime(5, 59, 59, 999), 10, 12));
1007     locale.d->setDayPeriods(dayPeriods);
1008     testPeriod = locale.d->dayPeriodForTime(QTime(8, 0, 0));
1009     QCOMPARE(testPeriod.periodName(KLocale::ShortName), QString("in the morning"));
1010     QCOMPARE(testPeriod.hourInPeriod(QTime(8, 0, 0)), 8);
1011     QCOMPARE(testPeriod.time(8, 0, 0), QTime(8, 0, 0));
1012     testPeriod = locale.d->dayPeriodForTime(QTime(12, 0, 0));
1013     QCOMPARE(testPeriod.periodName(KLocale::ShortName), QString("noon"));
1014     QCOMPARE(testPeriod.hourInPeriod(QTime(12, 0, 0)), 12);
1015     QCOMPARE(testPeriod.time(12, 0, 0), QTime(12, 0, 0));
1016     testPeriod = locale.d->dayPeriodForTime(QTime(12, 1, 0));
1017     QCOMPARE(testPeriod.periodName(KLocale::ShortName), QString("in the afternoon"));
1018     QCOMPARE(testPeriod.hourInPeriod(QTime(12, 1, 0)), 12);
1019     QCOMPARE(testPeriod.time(12, 1, 0), QTime(12, 1, 0));
1020     testPeriod = locale.d->dayPeriodForTime(QTime(23, 0, 0));
1021     QCOMPARE(testPeriod.periodName(KLocale::ShortName), QString("at night"));
1022     QCOMPARE(testPeriod.hourInPeriod(QTime(23, 0, 0)), 11);
1023     QCOMPARE(testPeriod.hourInPeriod(QTime(0, 0, 0)), 12);
1024     QCOMPARE(testPeriod.hourInPeriod(QTime(1, 0, 0)), 1);
1025     QCOMPARE(testPeriod.time(11, 0, 0), QTime(23, 0, 0));
1026     QCOMPARE(testPeriod.time(12, 0, 0), QTime(0, 0, 0));
1027     QCOMPARE(testPeriod.time(1, 0, 0), QTime(1, 0, 0));
1028     testPeriod = locale.d->dayPeriodForTime(QTime(0, 0, 0));
1029     QCOMPARE(testPeriod.periodName(KLocale::ShortName), QString("at night"));
1030     QCOMPARE(locale.readLocaleTime(QString("8:00:00 in the morning")), QTime(8, 0, 0));
1031     QCOMPARE(locale.readLocaleTime(QString("12:00:00 noon")), QTime(12, 0, 0));
1032     QCOMPARE(locale.readLocaleTime(QString("5:00:00 in the afternoon")), QTime(17, 0, 0));
1033     QCOMPARE(locale.readLocaleTime(QString("11:00:00 at night")), QTime(23, 0, 0));
1034     QCOMPARE(locale.readLocaleTime(QString("1:00:00 at night")), QTime(1, 0, 0));
1035     locale.setTimeFormat("%l %p");
1036     QCOMPARE(locale.formatLocaleTime(QTime(8, 0, 0)), QString("8 in the morning"));
1037     QCOMPARE(locale.formatLocaleTime(QTime(12, 0, 0)), QString("12 noon"));
1038     QCOMPARE(locale.formatLocaleTime(QTime(17, 0, 0)), QString("5 in the afternoon"));
1039     QCOMPARE(locale.formatLocaleTime(QTime(23, 0, 0)), QString("11 at night"));
1040     QCOMPARE(locale.formatLocaleTime(QTime(1, 0, 0)), QString("1 at night"));
1041 
1042     // Test load config
1043     KConfig *testConfig = new KConfig(QString(), KConfig::SimpleConfig);
1044     KConfigGroup testGroup(testConfig, "Locale");
1045     testGroup.writeEntry("DayPeriod1",
1046                          QStringList() << "third1" << "First Third" << "T1" << "T" <<
1047                          QTime(0, 0, 0).toString("HH:mm:ss.zzz") << QTime(7, 59, 59, 999).toString("HH:mm:ss.zzz") <<
1048                          QString::number(0) << QString::number(12));
1049     testGroup.writeEntry("DayPeriod2",
1050                          QStringList() << "third2" << "Second Third" << "T2" << "S" <<
1051                          QTime(8, 0, 0).toString("HH:mm:ss.zzz") << QTime(15, 59, 59, 999).toString("HH:mm:ss.zzz") <<
1052                          QString::number(8) << QString::number(12));
1053     testGroup.writeEntry("DayPeriod3",
1054                          QStringList() << "third3" << "First Third" << "T3" << "R" <<
1055                          QTime(16, 0, 0).toString("HH:mm:ss.zzz") << QTime(23, 59, 59, 999).toString("HH:mm:ss.zzz") <<
1056                          QString::number(4) << QString::number(12));
1057     locale = KLocale("en_us", "us", testConfig);
1058     QCOMPARE(locale.d->dayPeriodForTime(QTime(1, 0, 0)).periodName(KLocale::ShortName), QString("T1"));
1059     QCOMPARE(locale.d->dayPeriodForTime(QTime(11, 0, 0)).periodName(KLocale::ShortName), QString("T2"));
1060     QCOMPARE(locale.d->dayPeriodForTime(QTime(21, 0, 0)).periodName(KLocale::ShortName), QString("T3"));
1061 }
1062 
1063 void KLocaleTest::testCalendarSystemType()
1064 {
1065     KLocale locale(*KLocale::global());
1066 
1067     locale.setCalendar("gregorian");
1068     QCOMPARE(locale.calendarSystem(), KLocale::GregorianCalendar);
1069     QCOMPARE(locale.calendarType(),   QString("gregorian"));
1070 
1071     locale.setCalendar("coptic");
1072     QCOMPARE(locale.calendarSystem(), KLocale::CopticCalendar);
1073     QCOMPARE(locale.calendarType(),   QString("coptic"));
1074 
1075     locale.setCalendar("ethiopian");
1076     QCOMPARE(locale.calendarSystem(), KLocale::EthiopianCalendar);
1077     QCOMPARE(locale.calendarType(),   QString("ethiopian"));
1078 
1079     locale.setCalendar("hebrew");
1080     QCOMPARE(locale.calendarSystem(), KLocale::HebrewCalendar);
1081     QCOMPARE(locale.calendarType(),   QString("hebrew"));
1082 
1083     locale.setCalendar("hijri");
1084     QCOMPARE(locale.calendarSystem(), KLocale::IslamicCivilCalendar);
1085     QCOMPARE(locale.calendarType(),   QString("hijri"));
1086 
1087     locale.setCalendar("indian-national");
1088     QCOMPARE(locale.calendarSystem(), KLocale::IndianNationalCalendar);
1089     QCOMPARE(locale.calendarType(),   QString("indian-national"));
1090 
1091     locale.setCalendar("jalali");
1092     QCOMPARE(locale.calendarSystem(), KLocale::JalaliCalendar);
1093     QCOMPARE(locale.calendarType(),   QString("jalali"));
1094 
1095     locale.setCalendar("japanese");
1096     QCOMPARE(locale.calendarSystem(), KLocale::JapaneseCalendar);
1097     QCOMPARE(locale.calendarType(),   QString("japanese"));
1098 
1099     locale.setCalendar("julian");
1100     QCOMPARE(locale.calendarSystem(), KLocale::JulianCalendar);
1101     QCOMPARE(locale.calendarType(),   QString("julian"));
1102 
1103     locale.setCalendar("minguo");
1104     QCOMPARE(locale.calendarSystem(), KLocale::MinguoCalendar);
1105     QCOMPARE(locale.calendarType(),   QString("minguo"));
1106 
1107     locale.setCalendar("thai");
1108     QCOMPARE(locale.calendarSystem(), KLocale::ThaiCalendar);
1109     QCOMPARE(locale.calendarType(),   QString("thai"));
1110 
1111     locale.setCalendarSystem(KLocale::GregorianCalendar);
1112     QCOMPARE(locale.calendarSystem(), KLocale::GregorianCalendar);
1113     QCOMPARE(locale.calendarType(),   QString("gregorian"));
1114 
1115     locale.setCalendarSystem(KLocale::CopticCalendar);
1116     QCOMPARE(locale.calendarSystem(), KLocale::CopticCalendar);
1117     QCOMPARE(locale.calendarType(),   QString("coptic"));
1118 
1119     locale.setCalendarSystem(KLocale::EthiopianCalendar);
1120     QCOMPARE(locale.calendarSystem(), KLocale::EthiopianCalendar);
1121     QCOMPARE(locale.calendarType(),   QString("ethiopian"));
1122 
1123     locale.setCalendarSystem(KLocale::HebrewCalendar);
1124     QCOMPARE(locale.calendarSystem(), KLocale::HebrewCalendar);
1125     QCOMPARE(locale.calendarType(),   QString("hebrew"));
1126 
1127     locale.setCalendarSystem(KLocale::IslamicCivilCalendar);
1128     QCOMPARE(locale.calendarSystem(), KLocale::IslamicCivilCalendar);
1129     QCOMPARE(locale.calendarType(),   QString("hijri"));
1130 
1131     locale.setCalendarSystem(KLocale::IndianNationalCalendar);
1132     QCOMPARE(locale.calendarSystem(), KLocale::IndianNationalCalendar);
1133     QCOMPARE(locale.calendarType(),   QString("indian-national"));
1134 
1135     locale.setCalendarSystem(KLocale::JalaliCalendar);
1136     QCOMPARE(locale.calendarSystem(), KLocale::JalaliCalendar);
1137     QCOMPARE(locale.calendarType(),   QString("jalali"));
1138 
1139     locale.setCalendarSystem(KLocale::JapaneseCalendar);
1140     QCOMPARE(locale.calendarSystem(), KLocale::JapaneseCalendar);
1141     QCOMPARE(locale.calendarType(),   QString("japanese"));
1142 
1143     locale.setCalendarSystem(KLocale::JulianCalendar);
1144     QCOMPARE(locale.calendarSystem(), KLocale::JulianCalendar);
1145     QCOMPARE(locale.calendarType(),   QString("julian"));
1146 
1147     locale.setCalendarSystem(KLocale::MinguoCalendar);
1148     QCOMPARE(locale.calendarSystem(), KLocale::MinguoCalendar);
1149     QCOMPARE(locale.calendarType(),   QString("minguo"));
1150 
1151     locale.setCalendarSystem(KLocale::ThaiCalendar);
1152     QCOMPARE(locale.calendarSystem(), KLocale::ThaiCalendar);
1153     QCOMPARE(locale.calendarType(),   QString("thai"));
1154 
1155     locale.setCalendarSystem(KLocale::QDateCalendar);
1156     QCOMPARE(locale.calendarSystem(), KLocale::GregorianCalendar);
1157     QCOMPARE(locale.calendarType(),   QString("gregorian"));
1158 }
1159 
1160 void KLocaleTest::testCountryDivision()
1161 {
1162     KLocale locale(*KLocale::global());
1163     QCOMPARE(locale.countryDivisionCode(), locale.country().toUpper());
1164     QVERIFY(locale.setCountryDivisionCode("US-NY"));
1165     QCOMPARE(locale.countryDivisionCode(), QString("US-NY"));
1166 }
1167 
1168 void
1169 KLocaleTest::prettyFormatDuration()
1170 {
1171     KLocale locale(*KLocale::global());
1172 
1173     QCOMPARE(locale.prettyFormatDuration(1000), QString("1 second"));
1174     QCOMPARE(locale.prettyFormatDuration(5000), QString("5 seconds"));
1175     QCOMPARE(locale.prettyFormatDuration(60000), QString("1 minute"));
1176     QCOMPARE(locale.prettyFormatDuration(300000), QString("5 minutes"));
1177     QCOMPARE(locale.prettyFormatDuration(3600000), QString("1 hour"));
1178     QCOMPARE(locale.prettyFormatDuration(18000000), QString("5 hours"));
1179     QCOMPARE(locale.prettyFormatDuration(75000), QString("1 minute and 15 seconds"));
1180     // Problematic case #1 (there is a reference to this case on klocale.cpp)
1181     QCOMPARE(locale.prettyFormatDuration(119999), QString("2 minutes"));
1182     // This case is strictly 2 hours, 15 minutes and 59 seconds. However, since the range is
1183     // pretty high between hours and seconds, prettyFormatDuration always omits seconds when there
1184     // are hours in scene.
1185     QCOMPARE(locale.prettyFormatDuration(8159000), QString("2 hours and 15 minutes"));
1186     // This case is strictly 1 hour and 10 seconds. For the same reason, prettyFormatDuration
1187     // detects that 10 seconds is just garbage compared to 1 hour, and omits it on the result.
1188     QCOMPARE(locale.prettyFormatDuration(3610000), QString("1 hour"));
1189 }
1190 
1191 void
1192 KLocaleTest::formatByteSize()
1193 {
1194     KLocale locale(*KLocale::global());
1195 
1196     QCOMPARE(locale.formatByteSize(0), QString("0 B"));
1197     QCOMPARE(locale.formatByteSize(50), QString("50 B"));
1198     QCOMPARE(locale.formatByteSize(500), QString("500 B"));
1199     QCOMPARE(locale.formatByteSize(5000), QString("4.9 KiB"));
1200     QCOMPARE(locale.formatByteSize(50000), QString("48.8 KiB"));
1201     QCOMPARE(locale.formatByteSize(500000), QString("488.3 KiB"));
1202     QCOMPARE(locale.formatByteSize(5000000), QString("4.8 MiB"));
1203     QCOMPARE(locale.formatByteSize(50000000), QString("47.7 MiB"));
1204     QCOMPARE(locale.formatByteSize(500000000), QString("476.8 MiB"));
1205 #if (__WORDSIZE == 64) || defined (_LP64) || defined(__LP64__) || defined(__ILP64__)
1206     QCOMPARE(locale.formatByteSize(5000000000), QString("4.7 GiB"));
1207     QCOMPARE(locale.formatByteSize(50000000000), QString("46.6 GiB"));
1208     QCOMPARE(locale.formatByteSize(500000000000), QString("465.7 GiB"));
1209     QCOMPARE(locale.formatByteSize(5000000000000), QString("4.5 TiB"));
1210     QCOMPARE(locale.formatByteSize(50000000000000), QString("45.5 TiB"));
1211     QCOMPARE(locale.formatByteSize(500000000000000), QString("454.7 TiB"));
1212 #endif
1213 }
1214 
1215 void
1216 KLocaleTest::bug95511()
1217 {
1218     KLocale locale(*KLocale::global());
1219     bool ok = false;
1220 
1221     locale.setCurrencySymbol("$$");
1222     QCOMPARE(locale.readMoney("1,234,567,890.12$$", &ok), 1234567890.12);
1223     QVERIFY(ok);
1224     QCOMPARE(locale.readMoney("-1,234,567,890.12$$", &ok), -1234567890.12);
1225     QVERIFY(ok);
1226 
1227     locale.setCurrencySymbol("dollars");
1228     QCOMPARE(locale.readMoney("12,345,678,901,234,567 dollars", &ok),
1229              12345678901234567.00);
1230     QVERIFY(ok);
1231     QCOMPARE(locale.readMoney("-12,345,678,901.00 dollars", &ok),
1232              -12345678901.00);
1233     QVERIFY(ok);
1234 }
1235 
1236 void  KLocaleTest::weekDays()
1237 {
1238     KLocale locale(*KLocale::global());
1239 
1240     //Test setting of weekStartDay
1241     locale.setWeekStartDay(3);
1242     QCOMPARE(locale.weekStartDay(), 3);
1243     locale.setWeekStartDay(0);
1244     QCOMPARE(locale.weekStartDay(), 3);
1245     locale.setWeekStartDay(8);
1246     QCOMPARE(locale.weekStartDay(), 3);
1247 
1248     //Test setting of workingWeekStartDay
1249     locale.setWorkingWeekStartDay(3);
1250     locale.setWorkingWeekEndDay(6);
1251     QCOMPARE(locale.workingWeekStartDay(), 3);
1252     locale.setWorkingWeekStartDay(0);
1253     QCOMPARE(locale.workingWeekStartDay(), 3);
1254     locale.setWorkingWeekStartDay(8);
1255     QCOMPARE(locale.workingWeekStartDay(), 3);
1256 
1257     //Test setting of workingWeekEndDay
1258     locale.setWorkingWeekStartDay(3);
1259     locale.setWorkingWeekEndDay(6);
1260     QCOMPARE(locale.workingWeekEndDay(), 6);
1261     locale.setWorkingWeekEndDay(0);
1262     QCOMPARE(locale.workingWeekEndDay(), 6);
1263     locale.setWorkingWeekEndDay(8);
1264     QCOMPARE(locale.workingWeekEndDay(), 6);
1265 
1266     //Test setting of weekStartDay
1267     locale.setWeekDayOfPray(3);
1268     QCOMPARE(locale.weekDayOfPray(), 3);
1269     locale.setWeekDayOfPray(-1);
1270     QCOMPARE(locale.weekDayOfPray(), 3);
1271     locale.setWeekDayOfPray(8);
1272     QCOMPARE(locale.weekDayOfPray(), 3);
1273     locale.setWeekDayOfPray(0);
1274     QCOMPARE(locale.weekDayOfPray(), 0);
1275 }
1276 
1277 void
1278 KLocaleTest::formatByteSize2()
1279 {
1280     KLocale locale(*KLocale::global());
1281 
1282     // IEC is also default
1283     locale.setBinaryUnitDialect(KLocale::IECBinaryDialect);
1284     QCOMPARE(locale.binaryUnitDialect(), KLocale::IECBinaryDialect);
1285 
1286     QCOMPARE(locale.formatByteSize(1024.0), QString("1.0 KiB"));
1287     QCOMPARE(locale.formatByteSize(1023.0), QString("1,023 B"));
1288     QCOMPARE(locale.formatByteSize(1163000.0), QString("1.1 MiB")); // 1.2 metric
1289 
1290     QCOMPARE(locale.formatByteSize(-1024.0), QString("-1.0 KiB"));
1291     QCOMPARE(locale.formatByteSize(-1023.0), QString("-1,023 B"));
1292     QCOMPARE(locale.formatByteSize(-1163000.0), QString("-1.1 MiB")); // 1.2 metric
1293 
1294     locale.setBinaryUnitDialect(KLocale::JEDECBinaryDialect);
1295     QCOMPARE(locale.binaryUnitDialect(), KLocale::JEDECBinaryDialect);
1296 
1297     QCOMPARE(locale.formatByteSize(1024.0), QString("1.0 KB"));
1298     QCOMPARE(locale.formatByteSize(1023.0), QString("1,023 B"));
1299     QCOMPARE(locale.formatByteSize(1163000.0), QString("1.1 MB"));
1300 
1301     QCOMPARE(locale.formatByteSize(-1024.0), QString("-1.0 KB"));
1302     QCOMPARE(locale.formatByteSize(-1023.0), QString("-1,023 B"));
1303     QCOMPARE(locale.formatByteSize(-1163000.0), QString("-1.1 MB"));
1304 
1305     locale.setBinaryUnitDialect(KLocale::MetricBinaryDialect);
1306     QCOMPARE(locale.binaryUnitDialect(), KLocale::MetricBinaryDialect);
1307 
1308     QCOMPARE(locale.formatByteSize(1024.0), QString("1.0 kB"));
1309     QCOMPARE(locale.formatByteSize(1023.0), QString("1.0 kB"));
1310     QCOMPARE(locale.formatByteSize(1163000.0), QString("1.2 MB"));
1311 
1312     QCOMPARE(locale.formatByteSize(-1024.0), QString("-1.0 kB"));
1313     QCOMPARE(locale.formatByteSize(-1023.0), QString("-1.0 kB"));
1314     QCOMPARE(locale.formatByteSize(-1163000.0), QString("-1.2 MB"));
1315 
1316     // Ensure specific dialects work, still on metric by default
1317     QCOMPARE(locale.formatByteSize(1024.0, 1, KLocale::JEDECBinaryDialect), QString("1.0 KB"));
1318     QCOMPARE(locale.formatByteSize(2097152.0, 1, KLocale::IECBinaryDialect), QString("2.0 MiB"));
1319 
1320     locale.setBinaryUnitDialect(KLocale::JEDECBinaryDialect);
1321     QCOMPARE(locale.binaryUnitDialect(), KLocale::JEDECBinaryDialect);
1322     QCOMPARE(locale.formatByteSize(2097152.0, 1, KLocale::MetricBinaryDialect), QString("2.1 MB"));
1323 
1324     // Ensure all units are represented (still in JEDEC mode)
1325     QCOMPARE(locale.formatByteSize(2.0e9, 1, KLocale::MetricBinaryDialect), QString("2.0 GB"));
1326     QCOMPARE(locale.formatByteSize(3.2e12, 1, KLocale::MetricBinaryDialect), QString("3.2 TB"));
1327     QCOMPARE(locale.formatByteSize(4.1e15, 1, KLocale::MetricBinaryDialect), QString("4.1 PB"));
1328     QCOMPARE(locale.formatByteSize(6.7e18, 2, KLocale::MetricBinaryDialect), QString("6.70 EB"));
1329     QCOMPARE(locale.formatByteSize(5.6e20, 2, KLocale::MetricBinaryDialect), QString("560.00 EB"));
1330     QCOMPARE(locale.formatByteSize(2.3e22, 2, KLocale::MetricBinaryDialect), QString("23.00 ZB"));
1331     QCOMPARE(locale.formatByteSize(1.0e27, 1, KLocale::MetricBinaryDialect), QString("1,000.0 YB"));
1332 
1333     // Spattering of specific units
1334     QCOMPARE(locale.formatByteSize(823000, 3, KLocale::IECBinaryDialect, KLocale::UnitMegaByte), QString("0.785 MiB"));
1335     QCOMPARE(locale.formatByteSize(1234034.0, 4, KLocale::JEDECBinaryDialect, KLocale::UnitByte), QString("1,234,034 B"));
1336 }
1337 
1338 QTEST_MAIN(KLocaleTest)