[QDateTime] ISO Time zone designators can be [+-]HH

Added support on QDateTime::fromString to read correctly dates on ISO
format with Time zone designators at format [+-]HH

Change-Id: Ied5c3b7950aee3d0879af0e05398081395c18df5
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Mark Brand <mabrand@mabrand.nl>
bb10
Israel Lins 2014-12-15 12:24:42 -03:00 committed by Mark Brand
parent a44749855e
commit b60773934d
2 changed files with 12 additions and 5 deletions

View File

@ -248,7 +248,7 @@ static QString toOffsetString(Qt::DateFormat format, int offset)
.arg((qAbs(offset) / 60) % 60, 2, 10, QLatin1Char('0'));
}
// Parse offset in [+-]HH[:]MM format
// Parse offset in [+-]HH[[:]MM] format
static int fromOffsetString(const QString &offsetString, bool *valid)
{
*valid = false;
@ -272,7 +272,7 @@ static int fromOffsetString(const QString &offsetString, bool *valid)
// Split the hour and minute parts
QStringList parts = offsetString.mid(1).split(QLatin1Char(':'));
if (parts.count() == 1) {
// [+-]HHMM format
// [+-]HHMM or [+-]HH format
parts.append(parts.at(0).mid(2));
parts[0] = parts.at(0).left(2);
}
@ -282,7 +282,7 @@ static int fromOffsetString(const QString &offsetString, bool *valid)
if (!ok)
return 0;
const int minute = parts.at(1).toInt(&ok);
const int minute = (parts.at(1).isEmpty()) ? 0 : parts.at(1).toInt(&ok);
if (!ok || minute < 0 || minute > 59)
return 0;
@ -4428,8 +4428,7 @@ QDateTime QDateTime::fromString(const QString& string, Qt::DateFormat format)
} else {
// the loop below is faster but functionally equal to:
// const int signIndex = isoString.indexOf(QRegExp(QStringLiteral("[+-]")));
const int sizeOfTimeZoneString = 4;
int signIndex = isoString.size() - sizeOfTimeZoneString - 1;
int signIndex = isoString.size() - 1;
bool found = false;
{
const QChar plus = QLatin1Char('+');

View File

@ -1901,6 +1901,14 @@ void tst_QDateTime::fromStringDateFormat_data()
<< Qt::ISODate << QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), Qt::UTC);
QTest::newRow("ISO +00:00") << QString::fromLatin1("1970-01-01T00:12:34+00:00")
<< Qt::ISODate << QDateTime(QDate(1970, 1, 1), QTime(0, 12, 34), Qt::UTC);
QTest::newRow("ISO -03") << QString::fromLatin1("2014-12-15T12:37:09-03")
<< Qt::ISODate << QDateTime(QDate(2014, 12, 15), QTime(15, 37, 9), Qt::UTC);
QTest::newRow("ISO zzz-03") << QString::fromLatin1("2014-12-15T12:37:09.745-03")
<< Qt::ISODate << QDateTime(QDate(2014, 12, 15), QTime(15, 37, 9, 745), Qt::UTC);
QTest::newRow("ISO -3") << QString::fromLatin1("2014-12-15T12:37:09-3")
<< Qt::ISODate << QDateTime(QDate(2014, 12, 15), QTime(15, 37, 9), Qt::UTC);
QTest::newRow("ISO zzz-3") << QString::fromLatin1("2014-12-15T12:37:09.745-3")
<< Qt::ISODate << QDateTime(QDate(2014, 12, 15), QTime(15, 37, 9, 745), Qt::UTC);
// No time specified - defaults to Qt::LocalTime.
QTest::newRow("ISO data3") << QString::fromLatin1("2002-10-01")
<< Qt::ISODate << QDateTime(QDate(2002, 10, 1), QTime(0, 0, 0, 0), Qt::LocalTime);