From 4be9c76f1cdccc0dc2d868827d9a0e788399cdc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Matysiak?= Date: Tue, 14 May 2024 18:09:07 +0200 Subject: [PATCH] Make tst_QFileInfo::setFileTimes more robust There are some systems that do not support millisecond resolution in timestamps. An example of such system is VxWorks. POSIX2008 demands that `stat` contains an `st_mtim` field of type `timespec`. That field holds modification time with a nanosecond resolution. VxWorks reports _POSIX_VERSION as 200112. The `stat` struct does not contain `st_mtim`, but rather an `st_mtime` which holds a `time_t` which contains seconds. This leads to setFileTimes failing, because the test tries to save the current datetime as the modification time of a file, but the ms part is cut off. Fix the problem by checking if the timestamp read back from the filesystem contains milliseconds and only check it if it's not zero. Task-number: QTBUG-115777 Change-Id: I8c8a3b1c8e97955f13f059bcebf66d1b5af174fe Reviewed-by: Thiago Macieira (cherry picked from commit 88cb405514270a320d2993e8fb1c7b7b62646112) Reviewed-by: Qt Cherry-pick Bot --- tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp index f202ad3a2f4..9ae596973c4 100644 --- a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp +++ b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp @@ -1210,9 +1210,16 @@ void tst_QFileInfo::setFileTimes() QCOMPARE(file.write(data), data.size()); QCOMPARE(file.size(), data.size()); - const QDateTime before = QDateTime::currentDateTimeUtc().addMSecs(-5000); + QDateTime before = QDateTime::currentDateTimeUtc().addMSecs(-5000); + QVERIFY(file.setFileTime(before, QFile::FileModificationTime)); const QDateTime mtime = file.fileTime(QFile::FileModificationTime).toUTC(); + if (mtime.time().msec() == 0) + { + const QTime beforeTime = before.time(); + const QTime beforeTimeWithMSCutOff{beforeTime.hour(), beforeTime.minute(), beforeTime.second(), 0}; + before.setTime(beforeTimeWithMSCutOff); + } QCOMPARE(mtime, before); }