Q(Date|Time)+: in QDebug's <<, handle invalid explicitly

The output didn't previously make clear that the datum was invalid.
It's now explicitly invalid.  At the same time, use QDebug's space()
and nospace() methods to make spacing choices explicit.
Revised a QDate test to match.

Change-Id: I4699f5897530b4caa31c22fdb07de149832b30f4
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2017-06-02 15:18:43 +02:00
parent cc3b9cf3b3
commit fcb423d1f2
2 changed files with 32 additions and 18 deletions

View File

@ -5303,39 +5303,53 @@ QDataStream &operator>>(QDataStream &in, QDateTime &dateTime)
QDebug operator<<(QDebug dbg, const QDate &date)
{
QDebugStateSaver saver(dbg);
dbg.nospace() << "QDate(" << date.toString(Qt::ISODate) << ')';
dbg.nospace() << "QDate(";
if (date.isValid())
dbg.nospace() << date.toString(Qt::ISODate);
else
dbg.nospace() << "Invalid";
dbg.nospace() << ')';
return dbg;
}
QDebug operator<<(QDebug dbg, const QTime &time)
{
QDebugStateSaver saver(dbg);
dbg.nospace() << "QTime(" << time.toString(QStringViewLiteral("HH:mm:ss.zzz")) << ')';
dbg.nospace() << "QTime(";
if (time.isValid())
dbg.nospace() << time.toString(QStringViewLiteral("HH:mm:ss.zzz"));
else
dbg.nospace() << "Invalid";
dbg.nospace() << ')';
return dbg;
}
QDebug operator<<(QDebug dbg, const QDateTime &date)
{
QDebugStateSaver saver(dbg);
const Qt::TimeSpec ts = date.timeSpec();
dbg.nospace() << "QDateTime(";
dbg.noquote() << date.toString(QStringViewLiteral("yyyy-MM-dd HH:mm:ss.zzz t"))
<< ' ' << ts;
switch (ts) {
case Qt::UTC:
break;
case Qt::OffsetFromUTC:
dbg << ' ' << date.offsetFromUtc() << 's';
break;
case Qt::TimeZone:
if (date.isValid()) {
const Qt::TimeSpec ts = date.timeSpec();
dbg.noquote() << date.toString(QStringViewLiteral("yyyy-MM-dd HH:mm:ss.zzz t"))
<< ' ' << ts;
switch (ts) {
case Qt::UTC:
break;
case Qt::OffsetFromUTC:
dbg.space() << date.offsetFromUtc() << 's';
break;
case Qt::TimeZone:
#if QT_CONFIG(timezone)
dbg << ' ' << date.timeZone().id();
dbg.space() << date.timeZone().id();
#endif // timezone
break;
case Qt::LocalTime:
break;
break;
case Qt::LocalTime:
break;
}
} else {
dbg.nospace() << "Invalid";
}
return dbg << ')';
return dbg.nospace() << ')';
}
#endif

View File

@ -1490,7 +1490,7 @@ void tst_QDate::roundtrip() const
void tst_QDate::qdebug() const
{
QTest::ignoreMessage(QtDebugMsg, "QDate(\"\")");
QTest::ignoreMessage(QtDebugMsg, "QDate(Invalid)");
qDebug() << QDate();
QTest::ignoreMessage(QtDebugMsg, "QDate(\"1983-08-07\")");
qDebug() << QDate(1983, 8, 7);