From 15da0a5af20fe6771bcb94ef8d46edbd5c8fb64c Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Wed, 24 Apr 2013 13:38:13 +0200 Subject: [PATCH] Make QTime::toString output milliseconds for Qt::TextDate, Qt::ISODate. Section 4.2.2.4 of ISO 8601 allows for decimal fraction representations of dates and times. Currently, when calling QDateTime::toString(Qt::TextDate) or QDateTime::toString(Qt::ISODate), the milliseconds will be omitted. However, QDateTime::fromString(str, Qt::TextDate) and QDateTime::fromString(str, Qt::ISODate) already support decimal fraction representations, so this patch just adds this support to QTime::toString, and hence QDateTime::toString(). Task-number: QTBUG-30250 Change-Id: If58e4b3d3105322c51d11a76b832e5e634d8991f Reviewed-by: Thiago Macieira --- dist/changes-5.2.0 | 7 +++++ src/corelib/tools/qdatetime.cpp | 31 ++++++++++--------- .../corelib/kernel/qvariant/tst_qvariant.cpp | 4 +-- .../corelib/tools/qdatetime/tst_qdatetime.cpp | 13 +++++--- tests/auto/corelib/tools/qtime/tst_qtime.cpp | 28 +++++++++-------- 5 files changed, 48 insertions(+), 35 deletions(-) diff --git a/dist/changes-5.2.0 b/dist/changes-5.2.0 index 17144221d6e..a6beaa441d3 100644 --- a/dist/changes-5.2.0 +++ b/dist/changes-5.2.0 @@ -31,3 +31,10 @@ QtWidgets introduced to control how precise the autoResize should be. - QFileDialog::setDefaultSuffix() removes leading dot characters. + +QtCore +------ +- [QTBUG-30250] QTime, QDateTime: + When calling QTime::toString(Qt::TextDate) and QTime::toString(Qt::ISODate), + milliseconds are now included in the returned string. This also applies to + QDateTime::toString(Qt::TextDate) and QDateTime::toString(ISODate). diff --git a/src/corelib/tools/qdatetime.cpp b/src/corelib/tools/qdatetime.cpp index d8e3a78cdf1..c137049f38d 100644 --- a/src/corelib/tools/qdatetime.cpp +++ b/src/corelib/tools/qdatetime.cpp @@ -1474,15 +1474,15 @@ int QTime::msec() const /*! \overload - Returns the time as a string. Milliseconds are not included. The - \a format parameter determines the format of the string. + Returns the time as a string. The \a format parameter determines + the format of the string. - If \a format is Qt::TextDate, the string format is HH:MM:SS; e.g. 1 - second before midnight would be "23:59:59". + If \a format is Qt::TextDate, the string format is HH:MM:SS.zzz; + e.g. 1 second before midnight would be "23:59:59.000". If \a format is Qt::ISODate, the string format corresponds to the - ISO 8601 extended specification for representations of dates, - which is also HH:MM:SS. + ISO 8601 extended specification (with decimal fractions) for + representations of dates; also HH:MM:SS.zzz. If the \a format is Qt::SystemLocaleShortDate or Qt::SystemLocaleLongDate, the string format depends on the locale @@ -1521,10 +1521,11 @@ QString QTime::toString(Qt::DateFormat format) const default: case Qt::ISODate: case Qt::TextDate: - return QString::fromLatin1("%1:%2:%3") + return QString::fromLatin1("%1:%2:%3.%4") .arg(hour(), 2, 10, QLatin1Char('0')) .arg(minute(), 2, 10, QLatin1Char('0')) - .arg(second(), 2, 10, QLatin1Char('0')); + .arg(second(), 2, 10, QLatin1Char('0')) + .arg(msec(), 3, 10, QLatin1Char('0')); } } @@ -2465,15 +2466,15 @@ void QDateTime::setTime_t(uint secsSince1Jan1970UTC) the default way. QDate::shortDayName(), QDate::shortMonthName(), and QTime::toString() are used to generate the string, so the day and month names will be localized names. An example of this - formatting is "Wed May 20 03:40:13 1998". + formatting is "Wed May 20 03:40:13.456 1998". If the \a format is Qt::ISODate, the string format corresponds - to the ISO 8601 extended specification for representations of - dates and times, taking the form YYYY-MM-DDTHH:MM:SS[Z|[+|-]HH:MM], - depending on the timeSpec() of the QDateTime. If the timeSpec() - is Qt::UTC, Z will be appended to the string; if the timeSpec() is - Qt::OffsetFromUTC, the offset in hours and minutes from UTC will - be appended to the string. + to the ISO 8601 extended specification (with decimal fractions) for + representations of dates and times, taking the form + YYYY-MM-DDTHH:MM:SS.zzz[Z|[+|-]HH:MM], depending on the timeSpec() + of the QDateTime. If the timeSpec() is Qt::UTC, Z will be appended + to the string; if the timeSpec() is Qt::OffsetFromUTC, the offset + in hours and minutes from UTC will be appended to the string. If the \a format is Qt::SystemLocaleShortDate or Qt::SystemLocaleLongDate, the string format depends on the locale diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index eceeed51e74..fff65a50a42 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -952,8 +952,8 @@ void tst_QVariant::toString_data() QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" ); QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" ); QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 1, 1 ) ) << QString( "2002-01-01" ); - QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QString( "12:34:56" ); - QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 1, 1 ), QTime( 12, 34, 56 ) ) ) << QString( "2002-01-01T12:34:56" ); + QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QString( "12:34:56.000" ); + QTest::newRow( "qdatetime" ) << QVariant( QDateTime( QDate( 2002, 1, 1 ), QTime( 12, 34, 56 ) ) ) << QString( "2002-01-01T12:34:56.000" ); QTest::newRow( "llong" ) << QVariant( (qlonglong)Q_INT64_C(123456789012) ) << QString( "123456789012" ); } diff --git a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp index 0ec3f640203..cd7bbd22516 100644 --- a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp @@ -556,22 +556,25 @@ void tst_QDateTime::toString_isoDate_data() QTest::newRow("localtime") << QDateTime(QDate(1978, 11, 9), QTime(13, 28, 34)) - << QString("1978-11-09T13:28:34"); + << QString("1978-11-09T13:28:34.000"); QTest::newRow("UTC") << QDateTime(QDate(1978, 11, 9), QTime(13, 28, 34), Qt::UTC) - << QString("1978-11-09T13:28:34Z"); + << QString("1978-11-09T13:28:34.000Z"); QDateTime dt(QDate(1978, 11, 9), QTime(13, 28, 34)); dt.setUtcOffset(19800); QTest::newRow("positive OffsetFromUTC") << dt - << QString("1978-11-09T13:28:34+05:30"); + << QString("1978-11-09T13:28:34.000+05:30"); dt.setUtcOffset(-7200); QTest::newRow("negative OffsetFromUTC") << dt - << QString("1978-11-09T13:28:34-02:00"); + << QString("1978-11-09T13:28:34.000-02:00"); QTest::newRow("invalid") << QDateTime(QDate(-1, 11, 9), QTime(13, 28, 34), Qt::UTC) << QString(); + QTest::newRow("999 milliseconds UTC") + << QDateTime(QDate(2000, 1, 1), QTime(13, 28, 34, 999), Qt::UTC) + << QString("2000-01-01T13:28:34.999Z"); } void tst_QDateTime::toString_isoDate() @@ -591,7 +594,7 @@ void tst_QDateTime::toString_enumformat() QVERIFY(!str1.isEmpty()); // It's locale dependent everywhere QString str2 = dt1.toString(Qt::ISODate); - QCOMPARE(str2, QString("1995-05-20T12:34:56")); + QCOMPARE(str2, QString("1995-05-20T12:34:56.000")); QString str3 = dt1.toString(Qt::LocalDate); QVERIFY(!str3.isEmpty()); diff --git a/tests/auto/corelib/tools/qtime/tst_qtime.cpp b/tests/auto/corelib/tools/qtime/tst_qtime.cpp index 97645ea7f6d..95aed05e7c9 100644 --- a/tests/auto/corelib/tools/qtime/tst_qtime.cpp +++ b/tests/auto/corelib/tools/qtime/tst_qtime.cpp @@ -614,25 +614,27 @@ void tst_QTime::fromStringDateFormat() void tst_QTime::toStringDateFormat_data() { - // Since we can't define an element of Qt::DateFormat, str1 will be the string - // in TextDate format, and str2 will be the time in ISODate format. + QTest::addColumn("time"); + QTest::addColumn("format"); + QTest::addColumn("expected"); - QTest::addColumn("t"); - QTest::addColumn("str1"); - QTest::addColumn("str2"); - - QTest::newRow( "data0" ) << QTime(0,0,0,0) << QString("00:00:00") << QString("00:00:00"); - QTest::newRow( "data1" ) << QTime(10,12,34,0) << QString("10:12:34") << QString("10:12:34"); + QTest::newRow("00:00:00.000") << QTime(0, 0, 0, 0) << Qt::TextDate << QString("00:00:00.000"); + QTest::newRow("ISO 00:00:00.000") << QTime(0, 0, 0, 0) << Qt::ISODate << QString("00:00:00.000"); + QTest::newRow("Text 10:12:34.000") << QTime(10, 12, 34, 0) << Qt::TextDate << QString("10:12:34.000"); + QTest::newRow("ISO 10:12:34.000") << QTime(10, 12, 34, 0) << Qt::ISODate << QString("10:12:34.000"); + QTest::newRow("Text 10:12:34.001") << QTime(10, 12, 34, 001) << Qt::TextDate << QString("10:12:34.001"); + QTest::newRow("ISO 10:12:34.001") << QTime(10, 12, 34, 001) << Qt::ISODate << QString("10:12:34.001"); + QTest::newRow("Text 10:12:34.999") << QTime(10, 12, 34, 999) << Qt::TextDate << QString("10:12:34.999"); + QTest::newRow("ISO 10:12:34.999") << QTime(10, 12, 34, 999) << Qt::ISODate << QString("10:12:34.999"); } void tst_QTime::toStringDateFormat() { - QFETCH( QTime, t ); - QFETCH( QString, str1 ); - QFETCH( QString, str2 ); + QFETCH(QTime, time); + QFETCH(Qt::DateFormat, format); + QFETCH(QString, expected); - QCOMPARE( str1, t.toString( Qt::TextDate ) ); - QCOMPARE( str2, t.toString( Qt::ISODate ) ); + QCOMPARE(time.toString(format), expected); } void tst_QTime::toStringFormat_data()