Fix handling of last second in 1969

Due to a limitation of mktime(), we would have declared it invalid.
Tidied up qt_mktime() slightly in the process.

Change-Id: I25469e314afee6e0394e564bc69a98883005d4ec
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Edward Welbourne 2018-06-13 18:12:28 +02:00
parent 1f96709fdb
commit 72d14fe32f
2 changed files with 28 additions and 11 deletions

View File

@ -2441,24 +2441,26 @@ static qint64 qt_mktime(QDate *date, QTime *time, QDateTimePrivate::DaylightStat
local.tm_isdst = 1;
}
#endif // Q_OS_WIN
if (local.tm_isdst >= 1) {
if (local.tm_isdst > 0) {
if (daylightStatus)
*daylightStatus = QDateTimePrivate::DaylightTime;
if (abbreviation)
*abbreviation = qt_tzname(QDateTimePrivate::DaylightTime);
} else if (local.tm_isdst == 0) {
if (daylightStatus)
*daylightStatus = QDateTimePrivate::StandardTime;
if (abbreviation)
*abbreviation = qt_tzname(QDateTimePrivate::StandardTime);
} else {
if (daylightStatus)
*daylightStatus = QDateTimePrivate::UnknownDaylightTime;
if (daylightStatus) {
*daylightStatus = (local.tm_isdst == 0
? QDateTimePrivate::StandardTime
: QDateTimePrivate::UnknownDaylightTime);
}
if (abbreviation)
*abbreviation = qt_tzname(QDateTimePrivate::StandardTime);
}
if (ok)
*ok = true;
} else if (yy == 1969 && mm == 12 && dd == 31
&& time->second() == MSECS_PER_DAY - 1) {
// There was, of course, a last second in 1969, at time_t(-1); we won't
// rescue it if it's not in normalised form, and we don't know its DST
// status (unless we did already), but let's not wantonly declare it
// invalid.
} else {
*date = QDate();
*time = QTime();
@ -2468,9 +2470,12 @@ static qint64 qt_mktime(QDate *date, QTime *time, QDateTimePrivate::DaylightStat
*abbreviation = QString();
if (ok)
*ok = false;
return 0;
}
if (ok)
*ok = true;
return ((qint64)secsSinceEpoch * 1000) + msec;
return qint64(secsSinceEpoch) * 1000 + msec;
}
// Calls the platform variant of localtime for the given msecs, and updates

View File

@ -1095,6 +1095,11 @@ void tst_QDateTime::addDays()
QCOMPARE(dt2.timeSpec(), Qt::OffsetFromUTC);
QCOMPARE(dt2.offsetFromUtc(), 60 * 60);
// test last second of 1969 *is* valid (despite being time_t(-1))
dt1 = QDateTime(QDate(1970, 1, 1), QTime(23, 59, 59));
dt2 = dt1.addDays(-1);
QVERIFY(dt2.isValid());
// ### test invalid QDateTime()
}
@ -1286,6 +1291,13 @@ void tst_QDateTime::addSecs_data()
<< 60 * 60
<< QDateTime(QDate(2013, 1, 1), QTime(2, 2, 3),
Qt::OffsetFromUTC, 60 * 60);
// Check last second of 1969
QTest::newRow("epoch-1s-utc")
<< QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0), Qt::UTC) << -1
<< QDateTime(QDate(1969, 12, 31), QTime(23, 59, 59), Qt::UTC);
QTest::newRow("epoch-1s-local")
<< QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0)) << -1
<< QDateTime(QDate(1969, 12, 31), QTime(23, 59, 59));
}
void tst_QDateTime::addSecs()