Provide safe helper for converting to 'seconds since epoch'

This commit is contained in:
Chocobo1 2024-01-06 21:04:07 +08:00
parent 8bdb83d973
commit ad22237a2f
7 changed files with 141 additions and 20 deletions

View file

@ -95,6 +95,7 @@ add_library(qbt_base STATIC
unicodestrings.h unicodestrings.h
utils/bytearray.h utils/bytearray.h
utils/compare.h utils/compare.h
utils/datetime.h
utils/foreignapps.h utils/foreignapps.h
utils/fs.h utils/fs.h
utils/gzip.h utils/gzip.h
@ -183,6 +184,7 @@ add_library(qbt_base STATIC
torrentfilter.cpp torrentfilter.cpp
utils/bytearray.cpp utils/bytearray.cpp
utils/compare.cpp utils/compare.cpp
utils/datetime.cpp
utils/foreignapps.cpp utils/foreignapps.cpp
utils/fs.cpp utils/fs.cpp
utils/gzip.cpp utils/gzip.cpp

View file

@ -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 <QDateTime>
qint64 Utils::DateTime::toSecsSinceEpoch(const QDateTime &dateTime)
{
return dateTime.isValid() ? dateTime.toSecsSinceEpoch() : -1;
}

38
src/base/utils/datetime.h Normal file
View file

@ -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 <QtTypes>
class QDateTime;
namespace Utils::DateTime
{
qint64 toSecsSinceEpoch(const QDateTime &dateTime);
}

View file

@ -36,6 +36,7 @@
#include "base/bittorrent/trackerentry.h" #include "base/bittorrent/trackerentry.h"
#include "base/path.h" #include "base/path.h"
#include "base/tagset.h" #include "base/tagset.h"
#include "base/utils/datetime.h"
#include "base/utils/string.h" #include "base/utils/string.h"
namespace namespace
@ -98,16 +99,11 @@ QVariantMap serialize(const BitTorrent::Torrent &torrent)
return (ratio > BitTorrent::Torrent::MAX_RATIO) ? -1 : ratio; return (ratio > BitTorrent::Torrent::MAX_RATIO) ? -1 : ratio;
}; };
const auto toTimeStamp = [](const QDateTime &dateTime) -> qint64 const auto getLastActivityTime = [&torrent]() -> qlonglong
{
return dateTime.isValid() ? dateTime.toSecsSinceEpoch() : -1;
};
const auto getLastActivityTime = [&torrent, &toTimeStamp]() -> qlonglong
{ {
const qlonglong timeSinceActivity = torrent.timeSinceActivity(); const qlonglong timeSinceActivity = torrent.timeSinceActivity();
return (timeSinceActivity < 0) return (timeSinceActivity < 0)
? toTimeStamp(torrent.addedTime()) ? Utils::DateTime::toSecsSinceEpoch(torrent.addedTime())
: (QDateTime::currentDateTime().toSecsSinceEpoch() - timeSinceActivity); : (QDateTime::currentDateTime().toSecsSinceEpoch() - timeSinceActivity);
}; };
@ -139,8 +135,8 @@ QVariantMap serialize(const BitTorrent::Torrent &torrent)
{KEY_TORRENT_SAVE_PATH, torrent.savePath().toString()}, {KEY_TORRENT_SAVE_PATH, torrent.savePath().toString()},
{KEY_TORRENT_DOWNLOAD_PATH, torrent.downloadPath().toString()}, {KEY_TORRENT_DOWNLOAD_PATH, torrent.downloadPath().toString()},
{KEY_TORRENT_CONTENT_PATH, torrent.contentPath().toString()}, {KEY_TORRENT_CONTENT_PATH, torrent.contentPath().toString()},
{KEY_TORRENT_ADDED_ON, toTimeStamp(torrent.addedTime())}, {KEY_TORRENT_ADDED_ON, Utils::DateTime::toSecsSinceEpoch(torrent.addedTime())},
{KEY_TORRENT_COMPLETION_ON, toTimeStamp(torrent.completedTime())}, {KEY_TORRENT_COMPLETION_ON, Utils::DateTime::toSecsSinceEpoch(torrent.completedTime())},
{KEY_TORRENT_TRACKER, torrent.currentTracker()}, {KEY_TORRENT_TRACKER, torrent.currentTracker()},
{KEY_TORRENT_TRACKERS_COUNT, torrent.trackers().size()}, {KEY_TORRENT_TRACKERS_COUNT, torrent.trackers().size()},
{KEY_TORRENT_DL_LIMIT, torrent.downloadLimit()}, {KEY_TORRENT_DL_LIMIT, torrent.downloadLimit()},
@ -158,7 +154,7 @@ QVariantMap serialize(const BitTorrent::Torrent &torrent)
{KEY_TORRENT_RATIO_LIMIT, torrent.ratioLimit()}, {KEY_TORRENT_RATIO_LIMIT, torrent.ratioLimit()},
{KEY_TORRENT_SEEDING_TIME_LIMIT, torrent.seedingTimeLimit()}, {KEY_TORRENT_SEEDING_TIME_LIMIT, torrent.seedingTimeLimit()},
{KEY_TORRENT_INACTIVE_SEEDING_TIME_LIMIT, torrent.inactiveSeedingTimeLimit()}, {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_AUTO_TORRENT_MANAGEMENT, torrent.isAutoTMMEnabled()},
{KEY_TORRENT_TIME_ACTIVE, torrent.activeTime()}, {KEY_TORRENT_TIME_ACTIVE, torrent.activeTime()},
{KEY_TORRENT_SEEDING_TIME, torrent.finishedTime()}, {KEY_TORRENT_SEEDING_TIME, torrent.finishedTime()},

View file

@ -53,6 +53,7 @@
#include "base/logger.h" #include "base/logger.h"
#include "base/net/downloadmanager.h" #include "base/net/downloadmanager.h"
#include "base/torrentfilter.h" #include "base/torrentfilter.h"
#include "base/utils/datetime.h"
#include "base/utils/fs.h" #include "base/utils/fs.h"
#include "base/utils/string.h" #include "base/utils/string.h"
#include "apierror.h" #include "apierror.h"
@ -417,11 +418,6 @@ void TorrentsController::propertiesAction()
if (!torrent) if (!torrent)
throw APIError(APIErrorType::NotFound); throw APIError(APIErrorType::NotFound);
const auto toTimeStamp = [](const QDateTime &dateTime) -> qint64
{
return dateTime.isValid() ? dateTime.toSecsSinceEpoch() : -1;
};
const BitTorrent::InfoHash infoHash = torrent->infoHash(); const BitTorrent::InfoHash infoHash = torrent->infoHash();
const qlonglong totalDownload = torrent->totalDownload(); const qlonglong totalDownload = torrent->totalDownload();
const qlonglong totalUpload = torrent->totalUpload(); const qlonglong totalUpload = torrent->totalUpload();
@ -465,10 +461,10 @@ void TorrentsController::propertiesAction()
{KEY_PROP_PIECES_HAVE, torrent->piecesHave()}, {KEY_PROP_PIECES_HAVE, torrent->piecesHave()},
{KEY_PROP_CREATED_BY, torrent->creator()}, {KEY_PROP_CREATED_BY, torrent->creator()},
{KEY_PROP_ISPRIVATE, torrent->isPrivate()}, {KEY_PROP_ISPRIVATE, torrent->isPrivate()},
{KEY_PROP_ADDITION_DATE, toTimeStamp(torrent->addedTime())}, {KEY_PROP_ADDITION_DATE, Utils::DateTime::toSecsSinceEpoch(torrent->addedTime())},
{KEY_PROP_LAST_SEEN, toTimeStamp(torrent->lastSeenComplete())}, {KEY_PROP_LAST_SEEN, Utils::DateTime::toSecsSinceEpoch(torrent->lastSeenComplete())},
{KEY_PROP_COMPLETION_DATE, toTimeStamp(torrent->completedTime())}, {KEY_PROP_COMPLETION_DATE, Utils::DateTime::toSecsSinceEpoch(torrent->completedTime())},
{KEY_PROP_CREATION_DATE, toTimeStamp(torrent->creationDate())}, {KEY_PROP_CREATION_DATE, Utils::DateTime::toSecsSinceEpoch(torrent->creationDate())},
{KEY_PROP_SAVE_PATH, torrent->savePath().toString()}, {KEY_PROP_SAVE_PATH, torrent->savePath().toString()},
{KEY_PROP_DOWNLOAD_PATH, torrent->downloadPath().toString()}, {KEY_PROP_DOWNLOAD_PATH, torrent->downloadPath().toString()},
{KEY_PROP_COMMENT, torrent->comment()} {KEY_PROP_COMMENT, torrent->comment()}

View file

@ -13,8 +13,9 @@ set(testFiles
testglobal.cpp testglobal.cpp
testorderedset.cpp testorderedset.cpp
testpath.cpp testpath.cpp
testutilscompare.cpp
testutilsbytearray.cpp testutilsbytearray.cpp
testutilscompare.cpp
testutilsdatetime.cpp
testutilsgzip.cpp testutilsgzip.cpp
testutilsio.cpp testutilsio.cpp
testutilsstring.cpp testutilsstring.cpp

View file

@ -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 <QDateTime>
#include <QTest>
#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"