Allow ISODate string without seconds in QTime::fromString().

According to ISO 8601 (section 4.2.2.3), seconds can be omitted
from a string representing time.

Task-number: QTBUG-2813
Change-Id: I2578f290845e46a8f49be489f1d7427984ae7f08
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Mitch Curtis 2012-06-05 16:18:59 +02:00 committed by Qt by Nokia
parent 1f461ac45b
commit 168c583a78
2 changed files with 32 additions and 0 deletions

View File

@ -1803,6 +1803,10 @@ QTime QTime::fromString(const QString& s, Qt::DateFormat f)
const int minute(s.mid(3, 2).toInt(&ok));
if (!ok)
return QTime();
if (f == Qt::ISODate && s.size() == 5) {
// Do not need to specify seconds if using ISO format.
return QTime(hour, minute, 0, 0);
}
const int second(s.mid(6, 2).toInt(&ok));
if (!ok)
return QTime();

View File

@ -115,6 +115,8 @@ private slots:
void dateFromStringFormat();
void timeFromStringFormat_data();
void timeFromStringFormat();
void timeFromStringDateFormat_data();
void timeFromStringDateFormat();
void dateTimeFromStringFormat_data();
void dateTimeFromStringFormat();
@ -1435,6 +1437,32 @@ void tst_QDateTime::timeFromStringFormat()
QCOMPARE(dt, expected);
}
void tst_QDateTime::timeFromStringDateFormat_data()
{
QTest::addColumn<QString>("string");
QTest::addColumn<Qt::DateFormat>("format");
QTest::addColumn<QTime>("expected");
QTest::newRow("valid, start of day, omit seconds") << QString::fromLatin1("00:00") << Qt::ISODate << QTime(0, 0, 0);
QTest::newRow("valid, omit seconds") << QString::fromLatin1("22:21") << Qt::ISODate << QTime(22, 21, 0);
QTest::newRow("valid, omit seconds (2)") << QString::fromLatin1("23:59") << Qt::ISODate << QTime(23, 59, 0);
QTest::newRow("valid, end of day") << QString::fromLatin1("23:59:59") << Qt::ISODate << QTime(23, 59, 59);
QTest::newRow("invalid, empty string") << QString::fromLatin1("") << Qt::ISODate << invalidTime();
QTest::newRow("invalid, too many hours") << QString::fromLatin1("25:00") << Qt::ISODate << invalidTime();
QTest::newRow("invalid, too many minutes") << QString::fromLatin1("10:70") << Qt::ISODate << invalidTime();
QTest::newRow("invalid, too many seconds") << QString::fromLatin1("23:59:60") << Qt::ISODate << invalidTime();
}
void tst_QDateTime::timeFromStringDateFormat()
{
QFETCH(QString, string);
QFETCH(Qt::DateFormat, format);
QFETCH(QTime, expected);
QTime dt = QTime::fromString(string, format);
QCOMPARE(dt, expected);
}
void tst_QDateTime::dateTimeFromStringFormat_data()
{