File indexing completed on 2024-07-21 04:35:14

0001 /*
0002     SPDX-FileCopyrightText: 2004 Ace Jones acejones @users.sourceforge.net
0003     SPDX-FileCopyrightText: 2018-2019 Thomas Baumgart tbaumgart @kde.org
0004 
0005     This file is part of libalkimia.
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "alkdateformattest.h"
0011 
0012 #include "alkdateformat.h"
0013 #include "test.h"
0014 
0015 QTEST_GUILESS_MAIN(AlkDateFormatTest)
0016 
0017 void AlkDateFormatTest::testDateFormatKMyMoney()
0018 {
0019     AlkDateFormat format("%mm-%dd-%yyyy");
0020 
0021     QCOMPARE(format.convertString("1-5-2005"), QDate(2005, 1, 5));
0022     QCOMPARE(format.convertString("jan-15-2005"), QDate(2005, 1, 15));
0023     QCOMPARE(format.convertString("august-25-2005"), QDate(2005, 8, 25));
0024 
0025     format = AlkDateFormat("%mm/%dd/%yy");
0026 
0027     QCOMPARE(format.convertString("1/5/05"), QDate(2005, 1, 5));
0028     QCOMPARE(format.convertString("jan/15/05"), QDate(2005, 1, 15));
0029     QCOMPARE(format.convertString("august/25/05"), QDate(2005, 8, 25));
0030 
0031     format = AlkDateFormat("%d\\.%m\\.%yy");
0032 
0033     QCOMPARE(format.convertString("1.5.05"), QDate(2005, 5, 1));
0034     QCOMPARE(format.convertString("15.jan.05"), QDate(2005, 1, 15));
0035     QCOMPARE(format.convertString("25.august.05"), QDate(2005, 8, 25));
0036 
0037     format = AlkDateFormat("%yyyy\\\\%dddd\\\\%mmmmmmmmmmm");
0038 
0039     QCOMPARE(format.convertString("2005\\31\\12"), QDate(2005, 12, 31));
0040     QCOMPARE(format.convertString("2005\\15\\jan"), QDate(2005, 1, 15));
0041     QCOMPARE(format.convertString("2005\\25\\august"), QDate(2005, 8, 25));
0042 
0043     format = AlkDateFormat("%m %dd, %yyyy");
0044 
0045     QCOMPARE(format.convertString("jan 15, 2005"), QDate(2005, 1, 15));
0046     QCOMPARE(format.convertString("august 25, 2005"), QDate(2005, 8, 25));
0047     QCOMPARE(format.convertString("january 1st, 2005"), QDate(2005, 1, 1));
0048 
0049     format = AlkDateFormat("%m %d %y");
0050 
0051     QCOMPARE(format.convertString("12/31/50", false, 2000), QDate(1950, 12, 31));
0052     QCOMPARE(format.convertString("1/1/90", false, 2000), QDate(1990, 1, 1));
0053     QCOMPARE(format.convertString("december 31st, 5", false), QDate(2005, 12, 31));
0054 }
0055 
0056 void AlkDateFormatTest::testDateFormatSkrooge()
0057 {
0058     const QLocale defaultLocale = QLocale();
0059     QLocale::setDefault(QLocale("de_DE"));
0060 
0061     AlkDateFormat format1("M-d-yyyy");
0062     AlkDateFormat format2("MMM-dd-yyyy");
0063     AlkDateFormat format3("MMMM-dd-yyyy");
0064 
0065     QCOMPARE(format1.convertString("1-5-2005"), QDate(2005, 1, 5));
0066 
0067 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
0068     QCOMPARE(format2.convertString("Dez.-15-2005"), QDate(2005, 12, 15));
0069     QCOMPARE(format2.convertString("Dez-15-2005"), QDate(2005, 12, 15));
0070 #endif
0071     QCOMPARE(format2.convertString("Dec-15-2005"), QDate(2005, 12, 15));
0072     QCOMPARE(format3.convertString("august-25-2005"), QDate(2005, 8, 25));
0073 
0074     format1 = AlkDateFormat("M/d/yy");
0075     format2 = AlkDateFormat("MMM/dd/yy");
0076     format3 = AlkDateFormat("MMMM/dd/yy");
0077 
0078     QCOMPARE(format1.convertString("1/5/05"), QDate(2005, 1, 5));
0079     QCOMPARE(format2.convertString("jan/15/05"), QDate(2005, 1, 15));
0080     QCOMPARE(format3.convertString("august/25/05"), QDate(2005, 8, 25));
0081 
0082     format1 = AlkDateFormat("d.M.yy");
0083     format2 = AlkDateFormat("dd.MMM.yy");
0084     format3 = AlkDateFormat("dd.MMMM.yy");
0085 
0086     QCOMPARE(format1.convertString("1.5.05"), QDate(2005, 5, 1));
0087     QCOMPARE(format2.convertString("15.jan.05"), QDate(2005, 1, 15));
0088     QCOMPARE(format3.convertString("25.august.05"), QDate(2005, 8, 25));
0089 
0090     format1 = AlkDateFormat("yyyy\\dd\\MM");
0091     format2 = AlkDateFormat("yyyy\\dd\\MMM");
0092     format3 = AlkDateFormat("yyyy\\dd\\MMMM");
0093 
0094     QCOMPARE(format1.convertString("2005\\31\\12"), QDate(2005, 12, 31));
0095     QCOMPARE(format2.convertString("2005\\15\\jan"), QDate(2005, 1, 15));
0096     QCOMPARE(format3.convertString("2005\\25\\august"), QDate(2005, 8, 25));
0097 
0098     format1 = AlkDateFormat("MMM dd, yyyy");
0099     format2 = AlkDateFormat("MMMM dd, yyyy");
0100     format3 = AlkDateFormat("MMMM ddd, yyyy");
0101 
0102     QCOMPARE(format1.convertString("jan 15, 2005"), QDate(2005, 1, 15));
0103     QCOMPARE(format1.convertString("jan 15, 2005 21:02 GMT"), QDate(2005, 1, 15));
0104     QCOMPARE(format2.convertString("august 25, 2005"), QDate(2005, 8, 25));
0105     QCOMPARE(format2.convertString("august 25, 2005 21:02 GMT"), QDate(2005, 8, 25));
0106 
0107     // FinancialTimesFund
0108     format1 = AlkDateFormat("MMM dd yyyy");
0109     QCOMPARE(format1.convertString("jan 15 2005 21:02 GMT"), QDate(2005, 1, 15));
0110     QCOMPARE(format1.convertString("dec 15 2005 21:02 GMT"), QDate(2005, 12, 15));
0111 
0112     format1 = AlkDateFormat("UNIX");
0113     QCOMPARE(format1.convertString("1105747200"), QDate(2005, 1, 15));
0114 
0115     // not supported
0116 #if 0
0117     QCOMPARE(format3.convertString("january 1st, 2005"), QDate(2005, 1, 1));
0118     format = AlkDateFormat("%m %d %y");
0119 
0120     QCOMPARE(format.convertString("12/31/50", false, 2000), QDate(1950, 12, 31));
0121     QCOMPARE(format.convertString("1/1/90", false, 2000), QDate(1990, 1, 1));
0122     QCOMPARE(format.convertString("december 31st, 5", false), QDate(2005, 12, 31));
0123 #endif
0124 
0125     QLocale::setDefault(defaultLocale);
0126 }
0127 
0128 void AlkDateFormatTest::testValidFormatDetection()
0129 {
0130     AlkDateFormat format1("M-d-yyyy");
0131     QCOMPARE(format1.lastError(), AlkDateFormat::NoError);
0132     QCOMPARE(format1.lastErrorMessage(), QString());
0133 
0134     format1 = AlkDateFormat("bla");
0135     QCOMPARE(format1.lastError(), AlkDateFormat::NoError);
0136     QCOMPARE(format1.lastErrorMessage(), QString());
0137 
0138     QDate date;
0139     date = format1.convertString("1105747200");
0140     QCOMPARE(date.isValid(), false);
0141     QCOMPARE(format1.lastError(), AlkDateFormat::InvalidFormatString);
0142 }