From f77117d6ca4605917ca71135cf3166f4f02fe7d3 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 10 Jul 2017 15:16:47 -0700 Subject: [PATCH] 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 --- src/corelib/io/qfilesystemengine_win.cpp | 45 ++++++++++++++++-------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/corelib/io/qfilesystemengine_win.cpp b/src/corelib/io/qfilesystemengine_win.cpp index e171de9e9f..79a9936785 100644 --- a/src/corelib/io/qfilesystemengine_win.cpp +++ b/src/corelib/io/qfilesystemengine_win.cpp @@ -233,22 +233,37 @@ Q_CORE_EXPORT int qt_ntfs_permission_lookup = 0; 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; - if (!::TzSpecificLocalTimeToSystemTime(0, &lTime, &sTime)) - return false; + if (date.timeSpec() == Qt::LocalTime) { + 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); }