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

@@ -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()},

View File

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