{
QDate date;
#ifndef QT_BOOTSTRAPPED
- QDateTimeParser dt(QVariant::Date, QDateTimeParser::FromString);
+ QDateTimeParser dt(QVariant::Date);
if (Q_LIKELY(dt.parseFormat(format)))
dt.fromString(string, &date, 0);
#else
{
QTime time;
#ifndef QT_BOOTSTRAPPED
- QDateTimeParser dt(QVariant::Time, QDateTimeParser::FromString);
+ QDateTimeParser dt(QVariant::Time);
if (Q_LIKELY(dt.parseFormat(format)))
dt.fromString(string, 0, &time);
#else
QTime time;
QDate date;
#ifndef QT_BOOTSTRAPPED
- QDateTimeParser dt(QVariant::DateTime, QDateTimeParser::FromString);
+ QDateTimeParser dt(QVariant::DateTime);
if (Q_LIKELY(dt.parseFormat(format)))
dt.fromString(string, &date, &time);
#else
}
}
if (QDate::isValid(year, month, day) && QTime::isValid(hour, minute, second, msec)) {
- v = QDateTime(QDate(year, month, day), QTime(hour, minute, second, msec), spec);
+ v = QDateTime(QDate(year, month, day), QTime(hour, minute, second, msec), QDATETIMEEDIT_SPEC);
v.setUtcOffset(offset);
return true;
}
}
}
}
- if (newSectionNodes.isEmpty() && context == DateTimeEdit) {
- return false;
- }
if ((newDisplay & (AmPmSection|Hour12Section)) == Hour12Section) {
const int count = newSectionNodes.size();
pos += qMax(0, used);
state = qMin<State>(state, tmpstate);
- if (state == Intermediate && context == FromString) {
+ if (state == Intermediate) {
state = Invalid;
break;
}
}
}
if (needfixday) {
- if (context == FromString) {
- state = Invalid;
- goto end;
- }
- if (state == Acceptable && fixday) {
- day = qMin<int>(day, QDate(year, month, 1).daysInMonth());
-
- const QLocale loc = locale();
- for (int i=0; i<sectionNodesCount; ++i) {
- if (sectionType(i) & (DaySection|DayOfWeekSection)) {
- input.replace(sectionPos(i), sectionSize(i), loc.toString(day));
- }
- }
- } else {
- state = qMin(Intermediate, state);
- }
+ state = Invalid;
+ goto end;
}
}
}
- newCurrentValue = QDateTime(QDate(year, month, day), QTime(hour, minute, second, msec), spec);
+ newCurrentValue = QDateTime(QDate(year, month, day), QTime(hour, minute, second, msec), QDATETIMEEDIT_SPEC);
QDTPDEBUG << year << month << day << hour << minute << second << msec;
}
QDTPDEBUGN("'%s' => '%s'(%s)", input.toLatin1().constData(),
}
end:
if (newCurrentValue.isValid()) {
- if (context != FromString && state != Invalid && newCurrentValue < minimum) {
- static const QLatin1Char space(' ');
- if (Q_UNLIKELY(newCurrentValue >= minimum))
- qWarning("QDateTimeParser::parse Internal error 3 (%s %s)",
- qPrintable(newCurrentValue.toString()), qPrintable(minimum.toString()));
-
- bool done = false;
+ // optimization
+ Q_ASSERT(getMaximum().date().toJulianDay() == 4642999);
+ if (newCurrentValue.date().toJulianDay() > 4642999)
state = Invalid;
- for (int i=0; i<sectionNodesCount && !done; ++i) {
- const SectionNode &sn = sectionNodes.at(i);
- QString t = sectionText(input, i, sn.pos).toLower();
- if ((t.size() < sectionMaxSize(i) && (((int)fieldInfo(i) & (FixedWidth|Numeric)) != Numeric))
- || t.contains(space)) {
- switch (sn.type) {
- case AmPmSection:
- switch (findAmPm(t, i)) {
- case AM:
- case PM:
- state = Acceptable;
- done = true;
- break;
- case Neither:
- state = Invalid;
- done = true;
- break;
- case PossibleAM:
- case PossiblePM:
- case PossibleBoth: {
- const QDateTime copy(newCurrentValue.addSecs(12 * 60 * 60));
- if (copy >= minimum && copy <= maximum) {
- state = Intermediate;
- done = true;
- }
- break; }
- }
- case MonthSection:
- if (sn.count >= 3) {
- int tmp = newCurrentValue.date().month();
- // 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()));
- if (copy >= minimum && copy <= maximum)
- break; // break out of while
- }
- if (tmp == -1) {
- break;
- }
- state = Intermediate;
- done = true;
- break;
- }
- // fallthrough
- default: {
- int toMin;
- int toMax;
-
- if (sn.type & TimeSectionMask) {
- if (newCurrentValue.daysTo(minimum) != 0) {
- break;
- }
- toMin = newCurrentValue.time().msecsTo(minimum.time());
- if (newCurrentValue.daysTo(maximum) > 0) {
- toMax = -1; // can't get to max
- } else {
- toMax = newCurrentValue.time().msecsTo(maximum.time());
- }
- } else {
- toMin = newCurrentValue.daysTo(minimum);
- toMax = newCurrentValue.daysTo(maximum);
- }
- const int maxChange = QDateTimeParser::maxChange(i);
- if (toMin > maxChange) {
- QDTPDEBUG << "invalid because toMin > maxChange" << toMin
- << maxChange << t << newCurrentValue << minimum;
- state = Invalid;
- done = true;
- break;
- } else if (toMax > maxChange) {
- toMax = -1; // can't get to max
- }
-
- const int min = getDigit(minimum, i);
- if (Q_UNLIKELY(min == -1)) {
- qWarning("QDateTimeParser::parse Internal error 4 (%s)",
- qPrintable(sectionName(sn.type)));
- state = Invalid;
- done = true;
- break;
- }
-
- int max = toMax != -1 ? getDigit(maximum, i) : absoluteMax(i, newCurrentValue);
- int pos = cursorPosition - sn.pos;
- if (pos < 0 || pos >= t.size())
- 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;
- state = Invalid;
- done = true;
- break;
- }
- state = Intermediate;
- done = true;
- break; }
- }
- }
- }
- } else {
- if (context == FromString) {
- // optimization
- Q_ASSERT(getMaximum().date().toJulianDay() == 4642999);
- if (newCurrentValue.date().toJulianDay() > 4642999)
- state = Invalid;
- } else {
- if (newCurrentValue > getMaximum())
- state = Invalid;
- }
- QDTPDEBUG << "not checking intermediate because newCurrentValue is" << newCurrentValue << getMinimum() << getMaximum();
- }
+ QDTPDEBUG << "not checking intermediate because newCurrentValue is" << newCurrentValue << getMinimum() << getMaximum();
}
StateNode node;
node.input = input;
node.state = state;
node.conflicts = conflicts;
- node.value = newCurrentValue.toTimeSpec(spec);
+ node.value = newCurrentValue.toTimeSpec(QDATETIMEEDIT_SPEC);
text = input;
return node;
}
} else if (str.indexOf(ampm[pmindex], 0, Qt::CaseInsensitive) == 0) {
str = ampm[pmindex];
return PM;
- } else if (context == FromString || (str.count(space) == 0 && str.size() >= size)) {
- return Neither;
}
- size = qMin(size, str.size());
-
- bool broken[2] = {false, false};
- for (int i=0; i<size; ++i) {
- if (str.at(i) != space) {
- for (int j=0; j<2; ++j) {
- if (!broken[j]) {
- int index = ampm[j].indexOf(str.at(i));
- QDTPDEBUG << "looking for" << str.at(i)
- << "in" << ampm[j] << "and got" << index;
- if (index == -1) {
- if (str.at(i).category() == QChar::Letter_Uppercase) {
- index = ampm[j].indexOf(str.at(i).toLower());
- QDTPDEBUG << "trying with" << str.at(i).toLower()
- << "in" << ampm[j] << "and got" << index;
- } else if (str.at(i).category() == QChar::Letter_Lowercase) {
- index = ampm[j].indexOf(str.at(i).toUpper());
- QDTPDEBUG << "trying with" << str.at(i).toUpper()
- << "in" << ampm[j] << "and got" << index;
- }
- if (index == -1) {
- broken[j] = true;
- if (broken[amindex] && broken[pmindex]) {
- QDTPDEBUG << str << "didn't make it";
- return Neither;
- }
- continue;
- } else {
- str[i] = ampm[j].at(index); // fix case
- }
- }
- ampm[j].remove(index, 1);
- }
- }
- }
- }
- if (!broken[pmindex] && !broken[amindex])
- return PossibleBoth;
- return (!broken[amindex] ? PossibleAM : PossiblePM);
+ return Neither;
}
/*!
QDateTime QDateTimeParser::getMinimum() const
{
- return QDateTime(QDATETIMEEDIT_DATE_MIN, QDATETIMEEDIT_TIME_MIN, spec);
+ return QDateTime(QDATETIMEEDIT_DATE_MIN, QDATETIMEEDIT_TIME_MIN, QDATETIMEEDIT_SPEC);
}
QDateTime QDateTimeParser::getMaximum() const
{
- return QDateTime(QDATETIMEEDIT_DATE_MAX, QDATETIMEEDIT_TIME_MAX, spec);
+ return QDateTime(QDATETIMEEDIT_DATE_MAX, QDATETIMEEDIT_TIME_MAX, QDATETIMEEDIT_SPEC);
}
QString QDateTimeParser::getAmPmText(AmPm ap, Case cs) const
#include "QtCore/qlocale.h"
#include "QtCore/qvector.h"
-
+#define QDATETIMEEDIT_SPEC Qt::LocalTime
#define QDATETIMEEDIT_TIME_MIN QTime(0, 0, 0, 0)
#define QDATETIMEEDIT_TIME_MAX QTime(23, 59, 59, 999)
#define QDATETIMEEDIT_DATE_MIN QDate(100, 1, 1)
class Q_CORE_EXPORT QDateTimeParser
{
public:
- enum Context {
- FromString,
- DateTimeEdit
- };
- QDateTimeParser(QVariant::Type t, Context ctx)
- : currentSectionIndex(-1), display(0), cachedDay(-1), parserType(t),
- fixday(false), spec(Qt::LocalTime), context(ctx)
+ QDateTimeParser(QVariant::Type t)
+ : currentSectionIndex(-1), display(0), cachedDay(-1), parserType(t)
{
defaultLocale = QLocale::system();
first.type = FirstSection;
none.pos = -1;
none.count = -1;
}
- virtual ~QDateTimeParser() {}
+
enum {
Neither = -1,
AM = 0,
FieldInfo fieldInfo(int index) const;
- virtual QDateTime getMinimum() const;
- virtual QDateTime getMaximum() const;
- virtual int cursorPosition() const { return -1; }
- virtual QString displayText() const { return text; }
- virtual QString getAmPmText(AmPm ap, Case cs) const;
- virtual QLocale locale() const { return defaultLocale; }
+ QDateTime getMinimum() const;
+ QDateTime getMaximum() const;
+ int cursorPosition() const { return -1; }
+ QString displayText() const { return text; }
+ QString getAmPmText(AmPm ap, Case cs) const;
+ QLocale locale() const { return defaultLocale; }
int currentSectionIndex;
Sections display;
QString displayFormat;
QLocale defaultLocale;
QVariant::Type parserType;
-
- bool fixday;
-
- Qt::TimeSpec spec; // spec if used by QDateTimeEdit
- Context context;
};
Q_CORE_EXPORT bool operator==(const QDateTimeParser::SectionNode &s1, const QDateTimeParser::SectionNode &s2);