Skip to content

Commit

Permalink
util: Use std::chrono for time getters
Browse files Browse the repository at this point in the history
Cherry-picked from: 9266f74
  • Loading branch information
MarcoFalke authored and xanimo committed Mar 31, 2024
1 parent 360633e commit fa7fd1f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
26 changes: 11 additions & 15 deletions src/utiltime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include <atomic>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
#include <ctime>
#include <chrono>
#include <thread>
Expand Down Expand Up @@ -47,6 +46,14 @@ template std::chrono::seconds GetTime();
template std::chrono::milliseconds GetTime();
template std::chrono::microseconds GetTime();

template <typename T>
static T GetSystemTime()
{
const auto now = std::chrono::duration_cast<T>(std::chrono::system_clock::now().time_since_epoch());
assert(now.count() > 0);
return now;
}

int64_t GetMockableTimeMicros()
{
int64_t mocktime = GetMockTime();
Expand All @@ -66,23 +73,17 @@ int64_t GetMockTime()

int64_t GetTimeMillis()
{
int64_t now = (boost::posix_time::microsec_clock::universal_time() -
boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
assert(now > 0);
return now;
return int64_t{GetSystemTime<std::chrono::milliseconds>().count()};
}

int64_t GetTimeMicros()
{
int64_t now = (boost::posix_time::microsec_clock::universal_time() -
boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
assert(now > 0);
return now;
return int64_t{GetSystemTime<std::chrono::microseconds>().count()};
}

int64_t GetSystemTimeInSeconds()
{
return GetTimeMicros()/1000000;
return int64_t{GetSystemTime<std::chrono::seconds>().count()};
}

/** Return a time useful for the debug log */
Expand All @@ -93,11 +94,6 @@ int64_t GetLogTimeMicros()
return GetTimeMicros();
}

void MilliSleep(int64_t n)
{
boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
}

std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
{
static std::locale classic(std::locale::classic());
Expand Down
2 changes: 0 additions & 2 deletions src/utiltime.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ void SetMockTime(int64_t nMockTimeIn);
/** For testing */
int64_t GetMockTime();

void MilliSleep(int64_t n);

/** Return system time (or mocked time, if set) */
template <typename T>
T GetTime();
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4032,7 +4032,7 @@ bool CWallet::BackupWallet(const std::string& strDest)
}
}
}
MilliSleep(100);
boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ void ThreadFlushWalletDB()
int64_t nLastWalletUpdate = GetTime();
while (true)
{
MilliSleep(500);
boost::this_thread::sleep_for(boost::chrono::milliseconds(500));

if (nLastSeen != CWalletDB::GetUpdateCounter())
{
Expand Down

0 comments on commit fa7fd1f

Please sign in to comment.