From ad22237a2f50f9a636ed39bfc3f70e32c0e439ea Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 6 Jan 2024 21:04:07 +0800 Subject: [PATCH] Provide safe helper for converting to 'seconds since epoch' --- src/base/CMakeLists.txt | 2 + src/base/utils/datetime.cpp | 36 +++++++++++++ src/base/utils/datetime.h | 38 ++++++++++++++ src/webui/api/serialize/serialize_torrent.cpp | 16 +++--- src/webui/api/torrentscontroller.cpp | 14 ++--- test/CMakeLists.txt | 3 +- test/testutilsdatetime.cpp | 52 +++++++++++++++++++ 7 files changed, 141 insertions(+), 20 deletions(-) create mode 100644 src/base/utils/datetime.cpp create mode 100644 src/base/utils/datetime.h create mode 100644 test/testutilsdatetime.cpp diff --git a/src/base/CMakeLists.txt b/src/base/CMakeLists.txt index 47f5a4d03..854bd5b38 100644 --- a/src/base/CMakeLists.txt +++ b/src/base/CMakeLists.txt @@ -95,6 +95,7 @@ add_library(qbt_base STATIC unicodestrings.h utils/bytearray.h utils/compare.h + utils/datetime.h utils/foreignapps.h utils/fs.h utils/gzip.h @@ -183,6 +184,7 @@ add_library(qbt_base STATIC torrentfilter.cpp utils/bytearray.cpp utils/compare.cpp + utils/datetime.cpp utils/foreignapps.cpp utils/fs.cpp utils/gzip.cpp diff --git a/src/base/utils/datetime.cpp b/src/base/utils/datetime.cpp new file mode 100644 index 000000000..5791d6452 --- /dev/null +++ b/src/base/utils/datetime.cpp @@ -0,0 +1,36 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2024 Mike Tzou (Chocobo1) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give permission to + * link this program with the OpenSSL project's "OpenSSL" library (or with + * modified versions of it that use the same license as the "OpenSSL" library), + * and distribute the linked executables. You must obey the GNU General Public + * License in all respects for all of the code used other than "OpenSSL". If you + * modify file(s), you may extend this exception to your version of the file(s), + * but you are not obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + */ + +#include "datetime.h" + +#include + +qint64 Utils::DateTime::toSecsSinceEpoch(const QDateTime &dateTime) +{ + return dateTime.isValid() ? dateTime.toSecsSinceEpoch() : -1; +} diff --git a/src/base/utils/datetime.h b/src/base/utils/datetime.h new file mode 100644 index 000000000..d3ee99f62 --- /dev/null +++ b/src/base/utils/datetime.h @@ -0,0 +1,38 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2024 Mike Tzou (Chocobo1) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give permission to + * link this program with the OpenSSL project's "OpenSSL" library (or with + * modified versions of it that use the same license as the "OpenSSL" library), + * and distribute the linked executables. You must obey the GNU General Public + * License in all respects for all of the code used other than "OpenSSL". If you + * modify file(s), you may extend this exception to your version of the file(s), + * but you are not obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + */ + +#pragma once + +#include + +class QDateTime; + +namespace Utils::DateTime +{ + qint64 toSecsSinceEpoch(const QDateTime &dateTime); +} diff --git a/src/webui/api/serialize/serialize_torrent.cpp b/src/webui/api/serialize/serialize_torrent.cpp index 959d6ff43..35d733e9c 100644 --- a/src/webui/api/serialize/serialize_torrent.cpp +++ b/src/webui/api/serialize/serialize_torrent.cpp @@ -36,6 +36,7 @@ #include "base/bittorrent/trackerentry.h" #include "base/path.h" #include "base/tagset.h" +#include "base/utils/datetime.h" #include "base/utils/string.h" namespace @@ -98,16 +99,11 @@ QVariantMap serialize(const BitTorrent::Torrent &torrent) return (ratio > BitTorrent::Torrent::MAX_RATIO) ? -1 : ratio; }; - const auto toTimeStamp = [](const QDateTime &dateTime) -> qint64 - { - return dateTime.isValid() ? dateTime.toSecsSinceEpoch() : -1; - }; - - const auto getLastActivityTime = [&torrent, &toTimeStamp]() -> qlonglong + const auto getLastActivityTime = [&torrent]() -> qlonglong { const qlonglong timeSinceActivity = torrent.timeSinceActivity(); return (timeSinceActivity < 0) - ? toTimeStamp(torrent.addedTime()) + ? Utils::DateTime::toSecsSinceEpoch(torrent.addedTime()) : (QDateTime::currentDateTime().toSecsSinceEpoch() - timeSinceActivity); }; @@ -139,8 +135,8 @@ QVariantMap serialize(const BitTorrent::Torrent &torrent) {KEY_TORRENT_SAVE_PATH, torrent.savePath().toString()}, {KEY_TORRENT_DOWNLOAD_PATH, torrent.downloadPath().toString()}, {KEY_TORRENT_CONTENT_PATH, torrent.contentPath().toString()}, - {KEY_TORRENT_ADDED_ON, toTimeStamp(torrent.addedTime())}, - {KEY_TORRENT_COMPLETION_ON, toTimeStamp(torrent.completedTime())}, + {KEY_TORRENT_ADDED_ON, Utils::DateTime::toSecsSinceEpoch(torrent.addedTime())}, + {KEY_TORRENT_COMPLETION_ON, Utils::DateTime::toSecsSinceEpoch(torrent.completedTime())}, {KEY_TORRENT_TRACKER, torrent.currentTracker()}, {KEY_TORRENT_TRACKERS_COUNT, torrent.trackers().size()}, {KEY_TORRENT_DL_LIMIT, torrent.downloadLimit()}, @@ -158,7 +154,7 @@ QVariantMap serialize(const BitTorrent::Torrent &torrent) {KEY_TORRENT_RATIO_LIMIT, torrent.ratioLimit()}, {KEY_TORRENT_SEEDING_TIME_LIMIT, torrent.seedingTimeLimit()}, {KEY_TORRENT_INACTIVE_SEEDING_TIME_LIMIT, torrent.inactiveSeedingTimeLimit()}, - {KEY_TORRENT_LAST_SEEN_COMPLETE_TIME, toTimeStamp(torrent.lastSeenComplete())}, + {KEY_TORRENT_LAST_SEEN_COMPLETE_TIME, Utils::DateTime::toSecsSinceEpoch(torrent.lastSeenComplete())}, {KEY_TORRENT_AUTO_TORRENT_MANAGEMENT, torrent.isAutoTMMEnabled()}, {KEY_TORRENT_TIME_ACTIVE, torrent.activeTime()}, {KEY_TORRENT_SEEDING_TIME, torrent.finishedTime()}, diff --git a/src/webui/api/torrentscontroller.cpp b/src/webui/api/torrentscontroller.cpp index b1b0d0797..25618da55 100644 --- a/src/webui/api/torrentscontroller.cpp +++ b/src/webui/api/torrentscontroller.cpp @@ -53,6 +53,7 @@ #include "base/logger.h" #include "base/net/downloadmanager.h" #include "base/torrentfilter.h" +#include "base/utils/datetime.h" #include "base/utils/fs.h" #include "base/utils/string.h" #include "apierror.h" @@ -417,11 +418,6 @@ void TorrentsController::propertiesAction() if (!torrent) throw APIError(APIErrorType::NotFound); - const auto toTimeStamp = [](const QDateTime &dateTime) -> qint64 - { - return dateTime.isValid() ? dateTime.toSecsSinceEpoch() : -1; - }; - const BitTorrent::InfoHash infoHash = torrent->infoHash(); const qlonglong totalDownload = torrent->totalDownload(); const qlonglong totalUpload = torrent->totalUpload(); @@ -465,10 +461,10 @@ void TorrentsController::propertiesAction() {KEY_PROP_PIECES_HAVE, torrent->piecesHave()}, {KEY_PROP_CREATED_BY, torrent->creator()}, {KEY_PROP_ISPRIVATE, torrent->isPrivate()}, - {KEY_PROP_ADDITION_DATE, toTimeStamp(torrent->addedTime())}, - {KEY_PROP_LAST_SEEN, toTimeStamp(torrent->lastSeenComplete())}, - {KEY_PROP_COMPLETION_DATE, toTimeStamp(torrent->completedTime())}, - {KEY_PROP_CREATION_DATE, toTimeStamp(torrent->creationDate())}, + {KEY_PROP_ADDITION_DATE, Utils::DateTime::toSecsSinceEpoch(torrent->addedTime())}, + {KEY_PROP_LAST_SEEN, Utils::DateTime::toSecsSinceEpoch(torrent->lastSeenComplete())}, + {KEY_PROP_COMPLETION_DATE, Utils::DateTime::toSecsSinceEpoch(torrent->completedTime())}, + {KEY_PROP_CREATION_DATE, Utils::DateTime::toSecsSinceEpoch(torrent->creationDate())}, {KEY_PROP_SAVE_PATH, torrent->savePath().toString()}, {KEY_PROP_DOWNLOAD_PATH, torrent->downloadPath().toString()}, {KEY_PROP_COMMENT, torrent->comment()} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 330a555bf..7299f8e27 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -13,8 +13,9 @@ set(testFiles testglobal.cpp testorderedset.cpp testpath.cpp - testutilscompare.cpp testutilsbytearray.cpp + testutilscompare.cpp + testutilsdatetime.cpp testutilsgzip.cpp testutilsio.cpp testutilsstring.cpp diff --git a/test/testutilsdatetime.cpp b/test/testutilsdatetime.cpp new file mode 100644 index 000000000..93036067f --- /dev/null +++ b/test/testutilsdatetime.cpp @@ -0,0 +1,52 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2024 Mike Tzou (Chocobo1) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give permission to + * link this program with the OpenSSL project's "OpenSSL" library (or with + * modified versions of it that use the same license as the "OpenSSL" library), + * and distribute the linked executables. You must obey the GNU General Public + * License in all respects for all of the code used other than "OpenSSL". If you + * modify file(s), you may extend this exception to your version of the file(s), + * but you are not obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + */ + +#include +#include + +#include "base/utils/datetime.h" + +class TestUtilsDateTime final : public QObject +{ + Q_OBJECT + Q_DISABLE_COPY_MOVE(TestUtilsDateTime) + +public: + TestUtilsDateTime() = default; + +private slots: + void testToSecsSinceEpoch() const + { + QCOMPARE(Utils::DateTime::toSecsSinceEpoch(QDateTime()), -1); + QCOMPARE(Utils::DateTime::toSecsSinceEpoch(QDateTime::fromSecsSinceEpoch(0)), 0); + QCOMPARE(Utils::DateTime::toSecsSinceEpoch(QDateTime::fromSecsSinceEpoch(1)), 1); + } +}; + +QTEST_APPLESS_MAIN(TestUtilsDateTime) +#include "testutilsdatetime.moc"