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 <thiago.macieira@intel.com>
This commit is contained in:
Mitch Curtis 2013-04-24 13:38:13 +02:00 committed by The Qt Project
parent 96cd00bc0f
commit 15da0a5af2
5 changed files with 48 additions and 35 deletions

7
dist/changes-5.2.0 vendored
View File

@ -31,3 +31,10 @@ QtWidgets
introduced to control how precise the autoResize should be. introduced to control how precise the autoResize should be.
- QFileDialog::setDefaultSuffix() removes leading dot characters. - 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).

View File

@ -1474,15 +1474,15 @@ int QTime::msec() const
/*! /*!
\overload \overload
Returns the time as a string. Milliseconds are not included. The Returns the time as a string. The \a format parameter determines
\a format parameter determines the format of the string. the format of the string.
If \a format is Qt::TextDate, the string format is HH:MM:SS; e.g. 1 If \a format is Qt::TextDate, the string format is HH:MM:SS.zzz;
second before midnight would be "23:59:59". e.g. 1 second before midnight would be "23:59:59.000".
If \a format is Qt::ISODate, the string format corresponds to the If \a format is Qt::ISODate, the string format corresponds to the
ISO 8601 extended specification for representations of dates, ISO 8601 extended specification (with decimal fractions) for
which is also HH:MM:SS. representations of dates; also HH:MM:SS.zzz.
If the \a format is Qt::SystemLocaleShortDate or If the \a format is Qt::SystemLocaleShortDate or
Qt::SystemLocaleLongDate, the string format depends on the locale Qt::SystemLocaleLongDate, the string format depends on the locale
@ -1521,10 +1521,11 @@ QString QTime::toString(Qt::DateFormat format) const
default: default:
case Qt::ISODate: case Qt::ISODate:
case Qt::TextDate: case Qt::TextDate:
return QString::fromLatin1("%1:%2:%3") return QString::fromLatin1("%1:%2:%3.%4")
.arg(hour(), 2, 10, QLatin1Char('0')) .arg(hour(), 2, 10, QLatin1Char('0'))
.arg(minute(), 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(), the default way. QDate::shortDayName(), QDate::shortMonthName(),
and QTime::toString() are used to generate the string, so the and QTime::toString() are used to generate the string, so the
day and month names will be localized names. An example of this 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 If the \a format is Qt::ISODate, the string format corresponds
to the ISO 8601 extended specification for representations of to the ISO 8601 extended specification (with decimal fractions) for
dates and times, taking the form YYYY-MM-DDTHH:MM:SS[Z|[+|-]HH:MM], representations of dates and times, taking the form
depending on the timeSpec() of the QDateTime. If the timeSpec() YYYY-MM-DDTHH:MM:SS.zzz[Z|[+|-]HH:MM], depending on the timeSpec()
is Qt::UTC, Z will be appended to the string; if the timeSpec() is of the QDateTime. If the timeSpec() is Qt::UTC, Z will be appended
Qt::OffsetFromUTC, the offset in hours and minutes from UTC will to the string; if the timeSpec() is Qt::OffsetFromUTC, the offset
be appended to the string. in hours and minutes from UTC will be appended to the string.
If the \a format is Qt::SystemLocaleShortDate or If the \a format is Qt::SystemLocaleShortDate or
Qt::SystemLocaleLongDate, the string format depends on the locale Qt::SystemLocaleLongDate, the string format depends on the locale

View File

@ -952,8 +952,8 @@ void tst_QVariant::toString_data()
QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" ); QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" );
QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" ); QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" );
QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 1, 1 ) ) << QString( "2002-01-01" ); 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( "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" ); 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) ) << QTest::newRow( "llong" ) << QVariant( (qlonglong)Q_INT64_C(123456789012) ) <<
QString( "123456789012" ); QString( "123456789012" );
} }

View File

@ -556,22 +556,25 @@ void tst_QDateTime::toString_isoDate_data()
QTest::newRow("localtime") QTest::newRow("localtime")
<< QDateTime(QDate(1978, 11, 9), QTime(13, 28, 34)) << 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") QTest::newRow("UTC")
<< QDateTime(QDate(1978, 11, 9), QTime(13, 28, 34), Qt::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)); QDateTime dt(QDate(1978, 11, 9), QTime(13, 28, 34));
dt.setUtcOffset(19800); dt.setUtcOffset(19800);
QTest::newRow("positive OffsetFromUTC") QTest::newRow("positive OffsetFromUTC")
<< dt << dt
<< QString("1978-11-09T13:28:34+05:30"); << QString("1978-11-09T13:28:34.000+05:30");
dt.setUtcOffset(-7200); dt.setUtcOffset(-7200);
QTest::newRow("negative OffsetFromUTC") QTest::newRow("negative OffsetFromUTC")
<< dt << dt
<< QString("1978-11-09T13:28:34-02:00"); << QString("1978-11-09T13:28:34.000-02:00");
QTest::newRow("invalid") QTest::newRow("invalid")
<< QDateTime(QDate(-1, 11, 9), QTime(13, 28, 34), Qt::UTC) << QDateTime(QDate(-1, 11, 9), QTime(13, 28, 34), Qt::UTC)
<< QString(); << 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() void tst_QDateTime::toString_isoDate()
@ -591,7 +594,7 @@ void tst_QDateTime::toString_enumformat()
QVERIFY(!str1.isEmpty()); // It's locale dependent everywhere QVERIFY(!str1.isEmpty()); // It's locale dependent everywhere
QString str2 = dt1.toString(Qt::ISODate); 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); QString str3 = dt1.toString(Qt::LocalDate);
QVERIFY(!str3.isEmpty()); QVERIFY(!str3.isEmpty());

View File

@ -614,25 +614,27 @@ void tst_QTime::fromStringDateFormat()
void tst_QTime::toStringDateFormat_data() void tst_QTime::toStringDateFormat_data()
{ {
// Since we can't define an element of Qt::DateFormat, str1 will be the string QTest::addColumn<QTime>("time");
// in TextDate format, and str2 will be the time in ISODate format. QTest::addColumn<Qt::DateFormat>("format");
QTest::addColumn<QString>("expected");
QTest::addColumn<QTime>("t"); QTest::newRow("00:00:00.000") << QTime(0, 0, 0, 0) << Qt::TextDate << QString("00:00:00.000");
QTest::addColumn<QString>("str1"); QTest::newRow("ISO 00:00:00.000") << QTime(0, 0, 0, 0) << Qt::ISODate << QString("00:00:00.000");
QTest::addColumn<QString>("str2"); 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( "data0" ) << QTime(0,0,0,0) << QString("00:00:00") << QString("00:00:00"); QTest::newRow("Text 10:12:34.001") << QTime(10, 12, 34, 001) << Qt::TextDate << QString("10:12:34.001");
QTest::newRow( "data1" ) << QTime(10,12,34,0) << QString("10:12:34") << QString("10:12:34"); 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() void tst_QTime::toStringDateFormat()
{ {
QFETCH( QTime, t ); QFETCH(QTime, time);
QFETCH( QString, str1 ); QFETCH(Qt::DateFormat, format);
QFETCH( QString, str2 ); QFETCH(QString, expected);
QCOMPARE( str1, t.toString( Qt::TextDate ) ); QCOMPARE(time.toString(format), expected);
QCOMPARE( str2, t.toString( Qt::ISODate ) );
} }
void tst_QTime::toStringFormat_data() void tst_QTime::toStringFormat_data()