diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2017-02-25 11:21:44 -0800 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2017-02-28 10:40:40 +0000 |
commit | 04b8db3d57970631351fc6330af9553e94f1b14d (patch) | |
tree | 87b19362b62bc19b37bc0bc18783a0af6f323094 | |
parent | 2ae8292d03beab5869a1319e3d18ea281a5d014c (diff) |
Fix parsing of day-of-week names that start with another name
Task-number: QTBUG-59159
Change-Id: I95c9e502ccc74af3bcf0fffd14a69e0cd27ce96b
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
-rw-r--r-- | src/corelib/tools/qdatetimeparser.cpp | 41 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp | 50 |
2 files changed, 59 insertions, 32 deletions
diff --git a/src/corelib/tools/qdatetimeparser.cpp b/src/corelib/tools/qdatetimeparser.cpp index d66416207b..c8aa4fbc89 100644 --- a/src/corelib/tools/qdatetimeparser.cpp +++ b/src/corelib/tools/qdatetimeparser.cpp @@ -49,7 +49,7 @@ //#define QDATETIMEPARSER_DEBUG #if defined (QDATETIMEPARSER_DEBUG) && !defined(QT_NO_DEBUG_STREAM) -# define QDTPDEBUG qDebug() << QString("%1:%2").arg(__FILE__).arg(__LINE__) +# define QDTPDEBUG qDebug() # define QDTPDEBUGN qDebug #else # define QDTPDEBUG if (false) qDebug() @@ -1325,39 +1325,16 @@ int QDateTimeParser::findDay(const QString &str1, int startDay, int sectionIndex } const QLocale l = locale(); for (int day=startDay; day<=7; ++day) { - const QString str2 = l.dayName(day, sn.count == 4 ? QLocale::LongFormat : QLocale::ShortFormat); - - if (str1.startsWith(str2.toLower())) { - if (used) - *used = str2.size(); - if (usedDay) { - *usedDay = str2; - } - return day; - } - if (context == FromString) - continue; + const QString dayName = l.dayName(day, sn.count == 4 ? QLocale::LongFormat : QLocale::ShortFormat); + const QString str2 = dayName.toLower(); const int limit = qMin(str1.size(), str2.size()); - bool found = true; - for (int i=0; i<limit; ++i) { - if (str1.at(i) != str2.at(i) && !str1.at(i).isSpace()) { - if (i > bestCount) { - bestCount = i; - bestMatch = day; - } - found = false; - break; - } - - } - if (found) { - if (used) - *used = limit; - if (usedDay) - *usedDay = str2; - - return day; + int i = 0; + while (i < limit && str1.at(i) == str2.at(i)) + ++i; + if (i > bestCount) { + bestCount = i; + bestMatch = day; } } if (usedDay && bestMatch != -1) { diff --git a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp index 33844feafe..b6c740998e 100644 --- a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp @@ -117,6 +117,8 @@ private slots: void fromStringDateFormat(); void fromStringStringFormat_data(); void fromStringStringFormat(); + void fromStringStringFormatLocale_data(); + void fromStringStringFormatLocale(); #ifdef Q_OS_WIN void fromString_LOCALE_ILDATE(); #endif @@ -2337,6 +2339,54 @@ void tst_QDateTime::fromStringStringFormat() QCOMPARE(dt, expected); } +void tst_QDateTime::fromStringStringFormatLocale_data() +{ + QTest::addColumn<QString>("string"); + QTest::addColumn<QString>("format"); + QTest::addColumn<QLocale>("locale"); + QTest::addColumn<QDateTime>("expected"); + + QLocale c = QLocale::c(); + QDateTime dt(QDate(2017, 02, 25), QTime(17, 21, 25)); + + // The formats correspond to the locale formats, with the timezone removed. + // We hardcode them in case an update to the locale DB changes them. + + QTest::newRow("C:long") << "Saturday, 25 February 2017 17:21:25" << "dddd, d MMMM yyyy HH:mm:ss" << c << dt; + QTest::newRow("C:short") << "25 Feb 2017 17:21:25" << "d MMM yyyy HH:mm:ss" << c << dt; + QTest::newRow("C:narrow") << "25 Feb 2017 17:21:25" << "d MMM yyyy HH:mm:ss" << c << dt; + + QLocale fr(QLocale::French); + QTest::newRow("fr:long") << "Samedi 25 février 2017 17:21:25" << "dddd d MMMM yyyy HH:mm:ss" << fr << dt; + QTest::newRow("fr:short") << "25/02/2017 17:21" << "dd/MM/yyyy HH:mm" << fr << dt.addSecs(-25); + + // In Turkish, the word for Friday ("Cuma") is a prefix for the word for + // Saturday ("Cumartesi") + QLocale tr(QLocale::Turkish); + QTest::newRow("tr:long") << "25 Şubat 2017 Cumartesi 17:21:25" << "d MMMM yyyy dddd HH:mm:ss" << tr << dt; + QTest::newRow("tr:long2") << "24 Şubat 2017 Cuma 17:21:25" << "d MMMM yyyy dddd HH:mm:ss" << tr << dt.addDays(-1); + QTest::newRow("tr:mashed") << "25 Şubat2017 Cumartesi17:21:25" << "d MMMMyyyy ddddHH:mm:ss" << tr << dt; + QTest::newRow("tr:mashed2") << "24 Şubat2017 Cuma17:21:25" << "d MMMMyyyy ddddHH:mm:ss" << tr << dt.addDays(-1); + QTest::newRow("tr:short") << "25.02.2017 17:21" << "d.MM.yyyy HH:mm" << tr << dt.addSecs(-25); +} + +void tst_QDateTime::fromStringStringFormatLocale() +{ + QFETCH(QString, string); + QFETCH(QString, format); + QFETCH(QLocale, locale); + QFETCH(QDateTime, expected); + + QDateTime parsed = locale.toDateTime(string, format); + QCOMPARE(parsed, expected); + + parsed = locale.toDateTime(string.toLower(), format); + QCOMPARE(parsed, expected); + + parsed = locale.toDateTime(string.toUpper(), format); + QCOMPARE(parsed, expected); +} + #ifdef Q_OS_WIN // Windows only void tst_QDateTime::fromString_LOCALE_ILDATE() |