Fix handling of time-zones in QDateTime::toString(Qt::TextDate)

Previously, a zone-based time would claim to be GMT, rather than
identifying its zone properly.  Sadly, testing this reveals that
proprietary operating systems don't handle abbreviations ideally.

Task-number: QTBUG-57320
Task-number: QTBUG-57298
Change-Id: I8d8b7fffdbf65ac6178a65f5fc2df4d25afb1a14
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2016-11-21 17:48:04 +01:00
parent 7c33c644d3
commit ff4f0c3276
2 changed files with 54 additions and 1 deletions

View File

@ -3808,7 +3808,16 @@ QString QDateTime::toString(Qt::DateFormat format) const
.arg(dt.day())
.arg(tm.toString(Qt::TextDate))
.arg(dt.year());
if (timeSpec() != Qt::LocalTime) {
// Append zone/offset indicator, as appropriate:
switch (timeSpec()) {
case Qt::LocalTime:
break;
# if QT_CONFIG(timezone)
case Qt::TimeZone:
buf += QLatin1Char(' ') + d->m_timeZone.abbreviation(*this);
break;
# endif
default:
buf += QLatin1String(" GMT");
if (getSpec(d) == Qt::OffsetFromUTC)
buf += toOffsetString(Qt::TextDate, offsetFromUtc());

View File

@ -80,6 +80,7 @@ private slots:
void toString_isoDate_extra();
void toString_textDate_data();
void toString_textDate();
void toString_textDate_extra();
void toString_rfcDate_data();
void toString_rfcDate();
void toString_enumformat();
@ -854,6 +855,49 @@ void tst_QDateTime::toString_textDate()
QCOMPARE(resultDatetime.utcOffset(), datetime.utcOffset());
}
void tst_QDateTime::toString_textDate_extra()
{
QLatin1String GMT("GMT");
QDateTime dt = QDateTime::fromMSecsSinceEpoch(0, Qt::LocalTime);
QVERIFY(!dt.toString().endsWith(GMT));
dt = QDateTime::fromMSecsSinceEpoch(0, Qt::UTC).toLocalTime();
QVERIFY(!dt.toString().endsWith(GMT));
if (QTimeZone::systemTimeZone().offsetFromUtc(dt))
QVERIFY(dt.toString() != QLatin1String("Thu Jan 1 00:00:00 1970"));
else
QCOMPARE(dt.toString(), QLatin1String("Thu Jan 1 00:00:00 1970"));
#if QT_CONFIG(timezone)
QTimeZone PST("America/Vancouver");
if (PST.isValid()) {
dt = QDateTime::fromMSecsSinceEpoch(0, PST);
# if defined Q_OS_UNIX && !defined Q_OS_DARWIN
QCOMPARE(dt.toString(), QLatin1String("Wed Dec 31 16:00:00 1969 PST"));
# else // QTBUG-57320, QTBUG-57298
QVERIFY(dt.toString().startsWith(QLatin1String("Wed Dec 31 16:00:00 1969 ")));
# endif
dt = dt.toLocalTime();
QVERIFY(!dt.toString().endsWith(GMT));
} else {
qDebug("Missed zone test: no America/Vancouver zone available");
}
QTimeZone CET("Europe/Berlin");
if (CET.isValid()) {
dt = QDateTime::fromMSecsSinceEpoch(0, CET);
# if defined Q_OS_UNIX && !defined Q_OS_DARWIN
QCOMPARE(dt.toString(), QLatin1String("Thu Jan 1 01:00:00 1970 CET"));
# else // QTBUG-57320, QTBUG-57298
QVERIFY(dt.toString().startsWith(QLatin1String("Thu Jan 1 01:00:00 1970 ")));
# endif
dt = dt.toLocalTime();
QVERIFY(!dt.toString().endsWith(GMT));
} else {
qDebug("Missed zone test: no Europe/Berlin zone available");
}
#endif // timezone
dt = QDateTime::fromMSecsSinceEpoch(0, Qt::UTC);
QVERIFY(dt.toString().endsWith(GMT));
}
void tst_QDateTime::toString_rfcDate_data()
{
QTest::addColumn<QDateTime>("dt");