summaryrefslogtreecommitdiffstats
path: root/src/gui/itemmodels/qfilesystemmodel.cpp
diff options
context:
space:
mode:
authorAhmad Samir <a.samirh78@gmail.com>2022-12-24 00:12:45 +0200
committerAhmad Samir <a.samirh78@gmail.com>2023-01-06 17:52:07 +0200
commitfe7a0c19a64d339996e1429c8e31fedf919d0668 (patch)
treec25d3aad18a9f56ec44484d08cea58aa61625e3e /src/gui/itemmodels/qfilesystemmodel.cpp
parenteeb469869e4fe7fd873bb9e8a0185ec7afc6d332 (diff)
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>
Diffstat (limited to 'src/gui/itemmodels/qfilesystemmodel.cpp')
-rw-r--r--src/gui/itemmodels/qfilesystemmodel.cpp36
1 files changed, 31 insertions, 5 deletions
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);