From c014f1d8d8cc355c816cece8a76cb8a637bb01a3 Mon Sep 17 00:00:00 2001 From: arvidn Date: Fri, 26 Jul 2024 22:06:52 -0700 Subject: [PATCH] wrap ::time() to allow simulator runs to produce deterministic results --- include/libtorrent/aux_/time.hpp | 3 +++ include/libtorrent/peer_connection.hpp | 2 +- include/libtorrent/torrent.hpp | 4 ++-- src/create_torrent.cpp | 5 +++-- src/time.cpp | 8 ++++++++ src/torrent.cpp | 2 +- 6 files changed, 18 insertions(+), 6 deletions(-) diff --git a/include/libtorrent/aux_/time.hpp b/include/libtorrent/aux_/time.hpp index 9f2fcd5b54c..c9d970ef53c 100644 --- a/include/libtorrent/aux_/time.hpp +++ b/include/libtorrent/aux_/time.hpp @@ -48,6 +48,9 @@ namespace libtorrent { namespace aux { TORRENT_EXTRA_EXPORT std::time_t to_time_t(time_point32 tp); TORRENT_EXTRA_EXPORT time_point32 from_time_t(std::time_t t); + + // returns the current posix time (UTC) + TORRENT_EXTRA_EXPORT time_t posix_time(); } } #endif diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index ce327a1b786..e27de87c699 100644 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -686,7 +686,7 @@ namespace aux { int max_out_request_queue() const; std::time_t last_seen_complete() const { return m_last_seen_complete; } - void set_last_seen_complete(int ago) { m_last_seen_complete = ::time(nullptr) - ago; } + void set_last_seen_complete(int ago) { m_last_seen_complete = aux::posix_time() - ago; } std::int64_t uploaded_in_last_round() const { return m_statistics.total_payload_upload() - m_uploaded_at_last_round; } diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index ecca52f34ea..0de5168bd09 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -1135,8 +1135,8 @@ namespace libtorrent { void write_resume_data(resume_data_flags_t const flags, add_torrent_params& ret) const; - void seen_complete() { m_last_seen_complete = ::time(nullptr); } - int time_since_complete() const { return int(::time(nullptr) - m_last_seen_complete); } + void seen_complete() { m_last_seen_complete = aux::posix_time(); } + int time_since_complete() const { return int(aux::posix_time() - m_last_seen_complete); } time_t last_seen_complete() const { return m_last_seen_complete; } template diff --git a/src/create_torrent.cpp b/src/create_torrent.cpp index dfddb43e0cb..f022d81a714 100644 --- a/src/create_torrent.cpp +++ b/src/create_torrent.cpp @@ -46,6 +46,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/session_settings.hpp" #include "libtorrent/session.hpp" // for default_disk_io_constructor #include "libtorrent/aux_/directory.hpp" +#include "libtorrent/aux_/time.hpp" // for posix_time #include "libtorrent/disk_interface.hpp" #include @@ -393,7 +394,7 @@ namespace { create_torrent::create_torrent(file_storage& fs, int piece_size , create_flags_t const flags) : m_files(fs) - , m_creation_date(::time(nullptr)) + , m_creation_date(aux::posix_time()) , m_multifile(fs.num_files() > 1) , m_private(false) , m_include_mtime(bool(flags & create_torrent::modification_time)) @@ -466,7 +467,7 @@ namespace { create_torrent::create_torrent(torrent_info const& ti) : m_files(ti.files()) - , m_creation_date(::time(nullptr)) + , m_creation_date(aux::posix_time()) , m_multifile(ti.num_files() > 1) , m_private(ti.priv()) , m_include_mtime(false) diff --git a/src/time.cpp b/src/time.cpp index 23ea3928b3d..014379892c1 100644 --- a/src/time.cpp +++ b/src/time.cpp @@ -71,4 +71,12 @@ namespace libtorrent { namespace aux { return std::chrono::time_point_cast(r + milliseconds(500)); } + time_t posix_time() + { +#ifdef TORRENT_BUILD_SIMULATOR + return 1722056360 + clock_type::now().time_since_epoch().count(); +#else + return ::time(nullptr); +#endif + } } } diff --git a/src/torrent.cpp b/src/torrent.cpp index 3ec5361de54..acfb3b194dd 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -197,7 +197,7 @@ bool is_downloading_state(int const st) , m_trackerid(p.trackerid) , m_save_path(complete(p.save_path)) , m_stats_counters(ses.stats_counters()) - , m_added_time(p.added_time ? p.added_time : std::time(nullptr)) + , m_added_time(p.added_time ? p.added_time : aux::posix_time()) , m_completed_time(p.completed_time) , m_last_seen_complete(p.last_seen_complete) , m_swarm_last_seen_complete(p.last_seen_complete)