summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qdatetimeparser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/tools/qdatetimeparser.cpp')
-rw-r--r--src/corelib/tools/qdatetimeparser.cpp186
1 files changed, 91 insertions, 95 deletions
diff --git a/src/corelib/tools/qdatetimeparser.cpp b/src/corelib/tools/qdatetimeparser.cpp
index cf4fcd4929..bbcd64d34f 100644
--- a/src/corelib/tools/qdatetimeparser.cpp
+++ b/src/corelib/tools/qdatetimeparser.cpp
@@ -1,31 +1,37 @@
/****************************************************************************
**
-** Copyright (C) 2015 The Qt Company Ltd.
-** Contact: http://www.qt.io/licensing/
+** Copyright (C) 2016 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
-** $QT_BEGIN_LICENSE:LGPL21$
+** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see http://www.qt.io/terms-conditions. For further
-** information use the contact form at http://www.qt.io/contact-us.
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 2.1 or version 3 as published by the Free
-** Software Foundation and appearing in the file LICENSE.LGPLv21 and
-** LICENSE.LGPLv3 included in the packaging of this file. Please review the
-** following information to ensure the GNU Lesser General Public License
-** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
-** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
-** As a special exception, The Qt Company gives you certain additional
-** rights. These rights are described in The Qt Company LGPL Exception
-** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
@@ -125,13 +131,15 @@ bool QDateTimeParser::setDigit(QDateTime &v, int index, int newVal) const
}
const SectionNode &node = sectionNodes.at(index);
- int year = v.date().year();
- int month = v.date().month();
- int day = v.date().day();
- int hour = v.time().hour();
- int minute = v.time().minute();
- int second = v.time().second();
- int msec = v.time().msec();
+ const QDate date = v.date();
+ const QTime time = v.time();
+ int year = date.year();
+ int month = date.month();
+ int day = date.day();
+ int hour = time.hour();
+ int minute = time.minute();
+ int second = time.second();
+ int msec = time.msec();
switch (node.type) {
case Hour24Section: case Hour12Section: hour = newVal; break;
@@ -155,7 +163,7 @@ bool QDateTimeParser::setDigit(QDateTime &v, int index, int newVal) const
case AmPmSection: hour = (newVal == 0 ? hour % 12 : (hour % 12) + 12); break;
default:
qWarning("QDateTimeParser::setDigit() Internal error (%s)",
- qPrintable(sectionName(node.type)));
+ qPrintable(node.name()));
break;
}
@@ -206,7 +214,7 @@ int QDateTimeParser::absoluteMax(int s, const QDateTime &cur) const
default: break;
}
qWarning("QDateTimeParser::absoluteMax() Internal error (%s)",
- qPrintable(sectionName(sn.type)));
+ qPrintable(sn.name()));
return -1;
}
@@ -235,7 +243,7 @@ int QDateTimeParser::absoluteMin(int s) const
default: break;
}
qWarning("QDateTimeParser::absoluteMin() Internal error (%s, %0x)",
- qPrintable(sectionName(sn.type)), sn.type);
+ qPrintable(sn.name()), sn.type);
return -1;
}
@@ -290,7 +298,7 @@ int QDateTimeParser::sectionPos(const SectionNode &sn) const
default: break;
}
if (sn.pos == -1) {
- qWarning("QDateTimeParser::sectionPos Internal error (%s)", qPrintable(sectionName(sn.type)));
+ qWarning("QDateTimeParser::sectionPos Internal error (%s)", qPrintable(sn.name()));
return -1;
}
return sn.pos;
@@ -514,7 +522,7 @@ bool QDateTimeParser::parseFormat(const QString &newFormat)
last.pos = -1;
// for (int i=0; i<sectionNodes.size(); ++i) {
-// QDTPDEBUG << sectionName(sectionNodes.at(i).type) << sectionNodes.at(i).count;
+// QDTPDEBUG << sectionNodes.at(i).name() << sectionNodes.at(i).count;
// }
QDTPDEBUG << newFormat << displayFormat;
@@ -625,7 +633,7 @@ int QDateTimeParser::sectionMaxSize(Section s, int count) const
case TimeSectionMask:
case DateSectionMask:
qWarning("QDateTimeParser::sectionMaxSize: Invalid section %s",
- sectionName(s).toLatin1().constData());
+ SectionNode::name(s).toLatin1().constData());
case NoSectionIndex:
case FirstSectionIndex:
@@ -691,7 +699,7 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
const SectionNode &sn = sectionNode(sectionIndex);
if ((sn.type & Internal) == Internal) {
qWarning("QDateTimeParser::parseSection Internal error (%s %d)",
- qPrintable(sectionName(sn.type)), sectionIndex);
+ qPrintable(sn.name()), sectionIndex);
return -1;
}
@@ -699,7 +707,7 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
QString sectiontext = text.mid(index, sectionmaxsize);
int sectiontextSize = sectiontext.size();
- QDTPDEBUG << "sectionValue for" << sectionName(sn.type)
+ QDTPDEBUG << "sectionValue for" << sn.name()
<< "with text" << text << "and st" << sectiontext
<< text.mid(index, sectionmaxsize)
<< index;
@@ -849,7 +857,7 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
break; }
default:
qWarning("QDateTimeParser::parseSection Internal error (%s %d)",
- qPrintable(sectionName(sn.type)), sectionIndex);
+ qPrintable(sn.name()), sectionIndex);
return -1;
}
@@ -881,14 +889,16 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos
QDTPDEBUG << "parse" << input;
{
int year, month, day;
- currentValue.date().getDate(&year, &month, &day);
+ QDate currentDate = currentValue.date();
+ const QTime currentTime = currentValue.time();
+ currentDate.getDate(&year, &month, &day);
int year2digits = year % 100;
- int hour = currentValue.time().hour();
+ int hour = currentTime.hour();
int hour12 = -1;
- int minute = currentValue.time().minute();
- int second = currentValue.time().second();
- int msec = currentValue.time().msec();
- int dayofweek = currentValue.date().dayOfWeek();
+ int minute = currentTime.minute();
+ int second = currentTime.second();
+ int msec = currentTime.msec();
+ int dayofweek = currentDate.dayOfWeek();
int ampm = -1;
Sections isSet = NoSection;
@@ -910,7 +920,7 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos
int used;
num = parseSection(currentValue, index, input, cursorPosition, pos, tmpstate, &used);
- QDTPDEBUG << "sectionValue" << sectionName(sectionType(index)) << input
+ QDTPDEBUG << "sectionValue" << sn.name() << input
<< "pos" << pos << "used" << used << stateName(tmpstate);
if (fixup && tmpstate == Intermediate && used < sn.count) {
const FieldInfo fi = fieldInfo(index);
@@ -928,7 +938,7 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos
break;
}
- QDTPDEBUG << index << sectionName(sectionType(index)) << "is set to"
+ QDTPDEBUG << index << sn.name() << "is set to"
<< pos << "state is" << stateName(state);
@@ -948,7 +958,7 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos
case AmPmSection: current = &ampm; break;
default:
qWarning("QDateTimeParser::parse Internal error (%s)",
- qPrintable(sectionName(sn.type)));
+ qPrintable(sn.name()));
break;
}
if (!current) {
@@ -956,7 +966,7 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos
return StateNode();
}
if (isSet & sn.type && *current != num) {
- QDTPDEBUG << "CONFLICT " << sectionName(sn.type) << *current << num;
+ QDTPDEBUG << "CONFLICT " << sn.name() << *current << num;
conflicts = true;
if (index != currentSectionIndex || num == -1) {
continue;
@@ -1040,15 +1050,16 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos
const QLocale loc = locale();
for (int i=0; i<sectionNodesCount; ++i) {
- const Section thisSectionType = sectionType(i);
- if (thisSectionType & (DaySection)) {
- input.replace(sectionPos(i), sectionSize(i), loc.toString(day));
- } else if (thisSectionType & (DayOfWeekSectionShort|DayOfWeekSectionLong)) {
+ const SectionNode sn = sectionNode(i);
+ if (sn.type & DaySection) {
+ input.replace(sectionPos(sn), sectionSize(i), loc.toString(day));
+ } else if (sn.type & (DayOfWeekSectionShort | DayOfWeekSectionLong)) {
const int dayOfWeek = QDate(year, month, day).dayOfWeek();
- const QLocale::FormatType dayFormat = (thisSectionType == DayOfWeekSectionShort
- ? QLocale::ShortFormat : QLocale::LongFormat);
+ const QLocale::FormatType dayFormat =
+ (sn.type == DayOfWeekSectionShort
+ ? QLocale::ShortFormat : QLocale::LongFormat);
const QString dayName(loc.dayName(dayOfWeek, dayFormat));
- input.replace(sectionPos(i), sectionSize(i), dayName);
+ input.replace(sectionPos(sn), sectionSize(i), dayName);
}
}
} else {
@@ -1129,10 +1140,11 @@ end:
}
case MonthSection:
if (sn.count >= 3) {
- int tmp = newCurrentValue.date().month();
+ const int currentMonth = newCurrentValue.date().month();
+ int tmp = currentMonth;
// I know the first possible month makes the date too early
while ((tmp = findMonth(t, tmp + 1, i)) != -1) {
- const QDateTime copy(newCurrentValue.addMonths(tmp - newCurrentValue.date().month()));
+ const QDateTime copy(newCurrentValue.addMonths(tmp - currentMonth));
if (copy >= minimum && copy <= maximum)
break; // break out of while
}
@@ -1162,7 +1174,7 @@ end:
toMin = newCurrentValue.daysTo(minimum);
toMax = newCurrentValue.daysTo(maximum);
}
- const int maxChange = QDateTimeParser::maxChange(i);
+ const int maxChange = sn.maxChange();
if (toMin > maxChange) {
QDTPDEBUG << "invalid because toMin > maxChange" << toMin
<< maxChange << t << newCurrentValue << minimum;
@@ -1176,7 +1188,7 @@ end:
const int min = getDigit(minimum, i);
if (min == -1) {
qWarning("QDateTimeParser::parse Internal error 4 (%s)",
- qPrintable(sectionName(sn.type)));
+ qPrintable(sn.name()));
state = Invalid;
done = true;
break;
@@ -1188,7 +1200,7 @@ end:
pos = -1;
if (!potentialValue(t.simplified(), min, max, i, newCurrentValue, pos)) {
QDTPDEBUG << "invalid because potentialValue(" << t.simplified() << min << max
- << sectionName(sn.type) << "returned" << toMax << toMin << pos;
+ << sn.name() << "returned" << toMax << toMin << pos;
state = Invalid;
done = true;
break;
@@ -1246,7 +1258,8 @@ int QDateTimeParser::findMonth(const QString &str1, int startMonth, int sectionI
QLocale l = locale();
for (int month=startMonth; month<=12; ++month) {
- QString str2 = l.monthName(month, type).toLower();
+ const QString monthName = l.monthName(month, type);
+ QString str2 = monthName.toLower();
if (str1.startsWith(str2)) {
if (used) {
@@ -1254,7 +1267,7 @@ int QDateTimeParser::findMonth(const QString &str1, int startMonth, int sectionI
*used = str2.size();
}
if (usedMonth)
- *usedMonth = l.monthName(month, type);
+ *usedMonth = monthName;
return month;
}
@@ -1279,7 +1292,7 @@ int QDateTimeParser::findMonth(const QString &str1, int startMonth, int sectionI
if (used)
*used = limit;
if (usedMonth)
- *usedMonth = l.monthName(month, type);
+ *usedMonth = monthName;
return month;
}
}
@@ -1354,22 +1367,20 @@ int QDateTimeParser::findDay(const QString &str1, int startDay, int sectionIndex
/*!
\internal
- returns
- 0 if str == tr("AM")
- 1 if str == tr("PM")
- 2 if str can become tr("AM")
- 3 if str can become tr("PM")
- 4 if str can become tr("PM") and can become tr("AM")
- -1 can't become anything sensible
-
+ Returns
+ AM if str == tr("AM")
+ PM if str == tr("PM")
+ PossibleAM if str can become tr("AM")
+ PossiblePM if str can become tr("PM")
+ PossibleBoth if str can become tr("PM") and can become tr("AM")
+ Neither if str can't become anything sensible
*/
-
-int QDateTimeParser::findAmPm(QString &str, int sectionIndex, int *used) const
+QDateTimeParser::AmPmFinder QDateTimeParser::findAmPm(QString &str, int sectionIndex, int *used) const
{
const SectionNode &s = sectionNode(sectionIndex);
if (s.type != AmPmSection) {
qWarning("QDateTimeParser::findAmPm Internal error");
- return -1;
+ return Neither;
}
if (used)
*used = str.size();
@@ -1446,10 +1457,9 @@ int QDateTimeParser::findAmPm(QString &str, int sectionIndex, int *used) const
Max number of units that can be changed by this section.
*/
-int QDateTimeParser::maxChange(int index) const
+int QDateTimeParser::SectionNode::maxChange() const
{
- const SectionNode &sn = sectionNode(index);
- switch (sn.type) {
+ switch (type) {
// Time. unit is msec
case MSecSection: return 999;
case SecondSection: return 59 * 1000;
@@ -1465,7 +1475,7 @@ int QDateTimeParser::maxChange(int index) const
case YearSection2Digits: return 100 * 365;
default:
qWarning("QDateTimeParser::maxChange() Internal error (%s)",
- qPrintable(sectionName(sectionType(index))));
+ qPrintable(name()));
}
return -1;
@@ -1475,8 +1485,7 @@ QDateTimeParser::FieldInfo QDateTimeParser::fieldInfo(int index) const
{
FieldInfo ret = 0;
const SectionNode &sn = sectionNode(index);
- const Section s = sn.type;
- switch (s) {
+ switch (sn.type) {
case MSecSection:
ret |= Fraction;
// fallthrough
@@ -1487,7 +1496,7 @@ QDateTimeParser::FieldInfo QDateTimeParser::fieldInfo(int index) const
case YearSection:
case YearSection2Digits:
ret |= Numeric;
- if (s != YearSection) {
+ if (sn.type != YearSection) {
ret |= AllowPartial;
}
if (sn.count != 1) {
@@ -1515,30 +1524,16 @@ QDateTimeParser::FieldInfo QDateTimeParser::fieldInfo(int index) const
break;
default:
qWarning("QDateTimeParser::fieldInfo Internal error 2 (%d %s %d)",
- index, qPrintable(sectionName(sn.type)), sn.count);
+ index, qPrintable(sn.name()), sn.count);
break;
}
return ret;
}
-/*!
- \internal
-
- Get a number that str can become which is between min
- and max or -1 if this is not possible.
-*/
-
-
-QString QDateTimeParser::sectionFormat(int index) const
-{
- const SectionNode &sn = sectionNode(index);
- return sectionFormat(sn.type, sn.count);
-}
-
-QString QDateTimeParser::sectionFormat(Section s, int count) const
+QString QDateTimeParser::SectionNode::format() const
{
QChar fillChar;
- switch (s) {
+ switch (type) {
case AmPmSection: return count == 1 ? QLatin1String("AP") : QLatin1String("ap");
case MSecSection: fillChar = QLatin1Char('z'); break;
case SecondSection: fillChar = QLatin1Char('s'); break;
@@ -1553,7 +1548,7 @@ QString QDateTimeParser::sectionFormat(Section s, int count) const
case YearSection: fillChar = QLatin1Char('y'); break;
default:
qWarning("QDateTimeParser::sectionFormat Internal error (%s)",
- qPrintable(sectionName(s)));
+ qPrintable(name(type)));
return QString();
}
if (fillChar.isNull()) {
@@ -1584,7 +1579,8 @@ bool QDateTimeParser::potentialValue(const QString &str, int min, int max, int i
int val = (int)locale().toUInt(str);
const SectionNode &sn = sectionNode(index);
if (sn.type == YearSection2Digits) {
- val += currentValue.date().year() - (currentValue.date().year() % 100);
+ const int year = currentValue.date().year();
+ val += year - (year % 100);
}
if (val >= min && val <= max && str.size() == size) {
return true;
@@ -1651,7 +1647,7 @@ bool QDateTimeParser::skipToNextSection(int index, const QDateTime &current, con
For debugging. Returns the name of the section \a s.
*/
-QString QDateTimeParser::sectionName(int s) const
+QString QDateTimeParser::SectionNode::name(QDateTimeParser::Section s)
{
switch (s) {
case QDateTimeParser::AmPmSection: return QLatin1String("AmPmSection");
@@ -1669,7 +1665,7 @@ QString QDateTimeParser::sectionName(int s) const
case QDateTimeParser::NoSection: return QLatin1String("NoSection");
case QDateTimeParser::FirstSection: return QLatin1String("FirstSection");
case QDateTimeParser::LastSection: return QLatin1String("LastSection");
- default: return QLatin1String("Unknown section ") + QString::number(s);
+ default: return QLatin1String("Unknown section ") + QString::number(int(s));
}
}
@@ -1678,7 +1674,7 @@ QString QDateTimeParser::sectionName(int s) const
For debugging. Returns the name of the state \a s.
*/
-QString QDateTimeParser::stateName(int s) const
+QString QDateTimeParser::stateName(State s) const
{
switch (s) {
case Invalid: return QLatin1String("Invalid");