QFileSystemModel: add lastModified() overload that takes a QTimeZone

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 <kai.koehne@qt.io>
This commit is contained in:
Ahmad Samir 2022-12-24 00:12:45 +02:00
parent eeb469869e
commit fe7a0c19a6
4 changed files with 38 additions and 9 deletions

View File

@ -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 {

View File

@ -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);

View File

@ -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);

View File

@ -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); }