Skip to content

Commit

Permalink
Provide safe helper for converting to 'seconds since epoch'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobo1 committed Jan 8, 2024
1 parent 8bdb83d commit ad22237
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions src/base/utils/datetime.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 38 additions & 0 deletions src/base/utils/datetime.h
Original file line number Diff line number Diff line change
@@ -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);
}
16 changes: 6 additions & 10 deletions src/webui/api/serialize/serialize_torrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
};

Expand Down Expand Up @@ -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()},
Expand All @@ -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()},
Expand Down
14 changes: 5 additions & 9 deletions src/webui/api/torrentscontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()}
Expand Down
3 changes: 2 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions test/testutilsdatetime.cpp
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit ad22237

Please sign in to comment.