From d8e457323c2d2ef72ab81838ba82300672e4e26e Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Mon, 4 Jun 2012 16:59:46 +0200 Subject: [PATCH] Allow 24:00:00 for ISO dates in QDateTime::fromString(). ISO 8601 section 4.2.3 states that "The end of one calendar day [24:00] coincides with [00:00] at the start of the next calendar day", so fromString() was updated to account for this. Task-number: QTBUG-25387 Change-Id: I391db0da755dbc822ba0820c302a2c10391e1f3b Reviewed-by: Oswald Buddenhagen Reviewed-by: Thiago Macieira --- src/corelib/tools/qdatetime.cpp | 12 ++++++++++-- .../corelib/tools/qdatetime/tst_qdatetime.cpp | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp index 630deccadd..ca398170a3 100644 --- a/src/corelib/tools/qdatetime.cpp +++ b/src/corelib/tools/qdatetime.cpp @@ -3206,7 +3206,7 @@ QDateTime QDateTime::fromString(const QString& s, Qt::DateFormat f) case Qt::ISODate: { QString tmp = s; Qt::TimeSpec ts = Qt::LocalTime; - const QDate date = QDate::fromString(tmp.left(10), Qt::ISODate); + QDate date = QDate::fromString(tmp.left(10), Qt::ISODate); if (tmp.size() == 10) return QDateTime(date); @@ -3240,7 +3240,15 @@ QDateTime QDateTime::fromString(const QString& s, Qt::DateFormat f) return dt; } } - return QDateTime(date, QTime::fromString(tmp, Qt::ISODate), ts); + + QTime time(QTime::fromString(tmp, Qt::ISODate)); + if (!time.isValid() && tmp == QString::fromLatin1("24:00:00")) { + // ISO 8601 (section 4.2.3) says that 24:00 is equivalent to 00:00 the next day. + date = date.addDays(1); + // Don't need to correct time since QDateTime constructor will do it for us. + } + + return QDateTime(date, time, ts); } case Qt::SystemLocaleDate: case Qt::SystemLocaleShortDate: diff --git a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp index eb6af23ba5..e4cdb1a5c8 100644 --- a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp @@ -1268,6 +1268,22 @@ void tst_QDateTime::fromStringTextDate_data() << 28 << 6 << 2005 << 7 << 57 << 30 << 110 << int(Qt::LocalTime); + // Should be next day according to ISO 8601 section 4.2.3. + QTest::newRow("ISO date 24:00") << QString("2012-06-04T24:00:00") + << int(Qt::ISODate) + << 5 << 6 << 2012 << 0 << 0 << 0 << 0 + << int(Qt::LocalTime); + + QTest::newRow("ISO date 24:00 end of month") << QString("2012-06-30T24:00:00") + << int(Qt::ISODate) + << 1 << 7 << 2012 << 0 << 0 << 0 << 0 + << int(Qt::LocalTime); + + QTest::newRow("ISO date 24:00 end of month and year") << QString("2012-12-31T24:00:00") + << int(Qt::ISODate) + << 1 << 1 << 2013 << 0 << 0 << 0 << 0 + << int(Qt::LocalTime); + QTest::newRow("Year 0999") << QString("Tue Jun 17 08:00:10 0999") << int(Qt::TextDate) << 17 << 6 << 999 << 8 << 0 << 10 << 0