Skip to content

Commit

Permalink
wrap ::time() to allow simulator runs to produce deterministic results
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Jul 27, 2024
1 parent 97ac96e commit c014f1d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 6 deletions.
3 changes: 3 additions & 0 deletions include/libtorrent/aux_/time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion include/libtorrent/peer_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
4 changes: 2 additions & 2 deletions include/libtorrent/torrent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename Fun, typename... Args>
Expand Down
5 changes: 3 additions & 2 deletions src/create_torrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sys/types.h>
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,12 @@ namespace libtorrent { namespace aux {
return std::chrono::time_point_cast<seconds32>(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
}
} }
2 changes: 1 addition & 1 deletion src/torrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c014f1d

Please sign in to comment.