From fe7a0c19a64d339996e1429c8e31fedf919d0668 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sat, 24 Dec 2022 00:12:45 +0200 Subject: QFileSystemModel: add lastModified() overload that takes a QTimeZone MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is useful when sorting by time and using QTimeZone::UTC, since native filesystem API uses UTC, that means less conversions, which is potentially faster. Use UTC internally except for strings that are going to be shown in a GUI to the user, these should be in QTimeZone::LocalTime. [ChangeLog][QtGui][QFileSystemModel] Added lastModified() overload that takes a QTimeZone, this is useful when e.g. comparing file timestamps and all that's needed is time in UTC (this is faster as no time zone conversions is required). QTimeZone::UTC is used internally when sorting by time (using the sort() function), which should ideally make it faster too. Task-number: QTBUG-100349 Change-Id: I0e58f8eb7856b8da7d55db86ddd2c009ff83d970 Reviewed-by: Kai Köhne --- src/gui/itemmodels/qfileinfogatherer_p.h | 6 +++--- src/gui/itemmodels/qfilesystemmodel.cpp | 36 +++++++++++++++++++++++++++----- src/gui/itemmodels/qfilesystemmodel.h | 3 +++ src/gui/itemmodels/qfilesystemmodel_p.h | 2 +- 4 files changed, 38 insertions(+), 9 deletions(-) (limited to 'src/gui/itemmodels') diff --git a/src/gui/itemmodels/qfileinfogatherer_p.h b/src/gui/itemmodels/qfileinfogatherer_p.h index c8a56a333f..e4b2bc889f 100644 --- a/src/gui/itemmodels/qfileinfogatherer_p.h +++ b/src/gui/itemmodels/qfileinfogatherer_p.h @@ -51,7 +51,7 @@ public: return mFileInfo == fileInfo.mFileInfo && displayType == fileInfo.displayType && permissions() == fileInfo.permissions() - && lastModified() == fileInfo.lastModified(); + && lastModified(QTimeZone::UTC) == fileInfo.lastModified(QTimeZone::UTC); } #ifndef QT_NO_FSFILEENGINE @@ -95,8 +95,8 @@ public: return mFileInfo; } - QDateTime lastModified() const { - return mFileInfo.lastModified(); + QDateTime lastModified(const QTimeZone &tz) const { + return mFileInfo.lastModified(tz); } qint64 size() const { diff --git a/src/gui/itemmodels/qfilesystemmodel.cpp b/src/gui/itemmodels/qfilesystemmodel.cpp index d44e89fde0..efe0b568c8 100644 --- a/src/gui/itemmodels/qfilesystemmodel.cpp +++ b/src/gui/itemmodels/qfilesystemmodel.cpp @@ -529,14 +529,38 @@ QString QFileSystemModel::type(const QModelIndex &index) const } /*! - Returns the date and time when \a index was last modified. + Returns the date and time (in local time) when \a index was last modified. + + This is an overloaded function, equivalent to calling: + \code + lastModified(index, QTimeZone::LocalTime); + \endcode + + If \a index is invalid, a default constructed QDateTime is returned. */ QDateTime QFileSystemModel::lastModified(const QModelIndex &index) const +{ + return lastModified(index, QTimeZone::LocalTime); +} + +/*! + \since 6.6 + Returns the date and time, in the time zone \a tz, when + \a index was last modified. + + Typical arguments for \a tz are \c QTimeZone::UTC or \c QTimeZone::LocalTime. + UTC does not require any conversion from the time returned by the native file + system API, therefore getting the time in UTC is potentially faster. LocalTime + is typically chosen if the time is shown to the user. + + If \a index is invalid, a default constructed QDateTime is returned. + */ +QDateTime QFileSystemModel::lastModified(const QModelIndex &index, const QTimeZone &tz) const { Q_D(const QFileSystemModel); if (!index.isValid()) return QDateTime(); - return d->node(index)->lastModified(); + return d->node(index)->lastModified(tz); } /*! @@ -765,7 +789,7 @@ QString QFileSystemModelPrivate::time(const QModelIndex &index) const if (!index.isValid()) return QString(); #if QT_CONFIG(datestring) - return QLocale::system().toString(node(index)->lastModified(), QLocale::ShortFormat); + return QLocale::system().toString(node(index)->lastModified(QTimeZone::LocalTime), QLocale::ShortFormat); #else Q_UNUSED(index); return QString(); @@ -1027,10 +1051,12 @@ public: } case 3: { - if (l->lastModified() == r->lastModified()) + const QDateTime left = l->lastModified(QTimeZone::UTC); + const QDateTime right = r->lastModified(QTimeZone::UTC); + if (left == right) return naturalCompare.compare(l->fileName, r->fileName) < 0; - return l->lastModified() < r->lastModified(); + return left < right; } } Q_ASSERT(false); diff --git a/src/gui/itemmodels/qfilesystemmodel.h b/src/gui/itemmodels/qfilesystemmodel.h index d7bdf82589..d614ec2329 100644 --- a/src/gui/itemmodels/qfilesystemmodel.h +++ b/src/gui/itemmodels/qfilesystemmodel.h @@ -113,7 +113,10 @@ public: bool isDir(const QModelIndex &index) const; qint64 size(const QModelIndex &index) const; QString type(const QModelIndex &index) const; + + // ### Qt7 merge the two overloads, with tz QTimeZone::LocalTime QDateTime lastModified(const QModelIndex &index) const; + QDateTime lastModified(const QModelIndex &index, const QTimeZone &tz) const; QModelIndex mkdir(const QModelIndex &parent, const QString &name); bool rmdir(const QModelIndex &index); diff --git a/src/gui/itemmodels/qfilesystemmodel_p.h b/src/gui/itemmodels/qfilesystemmodel_p.h index 95d1ac9438..f30b4801bc 100644 --- a/src/gui/itemmodels/qfilesystemmodel_p.h +++ b/src/gui/itemmodels/qfilesystemmodel_p.h @@ -84,7 +84,7 @@ public: inline qint64 size() const { if (info && !info->isDir()) return info->size(); return 0; } inline QString type() const { if (info) return info->displayType; return QLatin1StringView(""); } - inline QDateTime lastModified() const { if (info) return info->lastModified(); return QDateTime(); } + inline QDateTime lastModified(const QTimeZone &tz) const { return info ? info->lastModified(tz) : QDateTime(); } inline QFile::Permissions permissions() const { if (info) return info->permissions(); return { }; } inline bool isReadable() const { return ((permissions() & QFile::ReadUser) != 0); } inline bool isWritable() const { return ((permissions() & QFile::WriteUser) != 0); } -- cgit v1.2.3