QFileSystemEngine/Windows: fix setFileTime when input date is UTC

I was getting a weird 7-hour offset when doing:

    QVERIFY(file.setFileTime(before.addSecs(-10), QFile::FileModificationTime));
    mtime = file.fileTime(QFile::FileModificationTime).toUTC();
    QCOMPARE(mtime, before.addSecs(-10));

Change-Id: I35c4da69d754686a43e675b3d215213e448df7cf
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
This commit is contained in:
Thiago Macieira 2017-07-10 15:16:47 -07:00
parent a3c71b71e2
commit f77117d6ca

View File

@ -233,22 +233,37 @@ Q_CORE_EXPORT int qt_ntfs_permission_lookup = 0;
static inline bool toFileTime(const QDateTime &date, FILETIME *fileTime) static inline bool toFileTime(const QDateTime &date, FILETIME *fileTime)
{ {
SYSTEMTIME lTime;
const QDate d = date.date();
const QTime t = date.time();
lTime.wYear = d.year();
lTime.wMonth = d.month();
lTime.wDay = d.day();
lTime.wHour = t.hour();
lTime.wMinute = t.minute();
lTime.wSecond = t.second();
lTime.wMilliseconds = t.msec();
lTime.wDayOfWeek = d.dayOfWeek() % 7;
SYSTEMTIME sTime; SYSTEMTIME sTime;
if (!::TzSpecificLocalTimeToSystemTime(0, &lTime, &sTime)) if (date.timeSpec() == Qt::LocalTime) {
return false; SYSTEMTIME lTime;
const QDate d = date.date();
const QTime t = date.time();
lTime.wYear = d.year();
lTime.wMonth = d.month();
lTime.wDay = d.day();
lTime.wHour = t.hour();
lTime.wMinute = t.minute();
lTime.wSecond = t.second();
lTime.wMilliseconds = t.msec();
lTime.wDayOfWeek = d.dayOfWeek() % 7;
if (!::TzSpecificLocalTimeToSystemTime(0, &lTime, &sTime))
return false;
} else {
QDateTime utcDate = date.toUTC();
const QDate d = utcDate.date();
const QTime t = utcDate.time();
sTime.wYear = d.year();
sTime.wMonth = d.month();
sTime.wDay = d.day();
sTime.wHour = t.hour();
sTime.wMinute = t.minute();
sTime.wSecond = t.second();
sTime.wMilliseconds = t.msec();
sTime.wDayOfWeek = d.dayOfWeek() % 7;
}
return ::SystemTimeToFileTime(&sTime, fileTime); return ::SystemTimeToFileTime(&sTime, fileTime);
} }