Skip to content

Commit

Permalink
Merge bitcoin#27233: refactor: Replace GetTimeMicros by SystemClock
Browse files Browse the repository at this point in the history
faf3f12 refactor: Replace GetTimeMicros by SystemClock (MarcoFalke)

Pull request description:

  It is unclear from the name that `GetTimeMicros` returns the system time. Also, it is not using the type-safe `std::chrono` types.

  Fix both issues by replacing it with `SystemClock` in the only place it is used.

  This refactor should not change behavior.

ACKs for top commit:
  willcl-ark:
    tACK faf3f12
  john-moffett:
    ACK faf3f12 changes, but left a comment for the existing code.

Tree-SHA512: 069e6ef26467a469f128b98a4aeb334f141742befd7880cb3a7d280480e9f0684dc0686fa6a828cdcb3d11943ae5c7f8ad5d9d9dab4c668be85e5d28c78cd489
  • Loading branch information
fanquake committed Mar 23, 2023
2 parents 4c6b7d3 + faf3f12 commit 2fadb26
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,12 @@ std::string BCLog::Logger::LogTimestampStr(const std::string& str)
return str;

if (m_started_new_line) {
int64_t nTimeMicros = GetTimeMicros();
strStamped = FormatISO8601DateTime(nTimeMicros/1000000);
const auto now{SystemClock::now()};
const auto now_seconds{std::chrono::time_point_cast<std::chrono::seconds>(now)};
strStamped = FormatISO8601DateTime(TicksSinceEpoch<std::chrono::seconds>(now_seconds));
if (m_log_time_micros) {
strStamped.pop_back();
strStamped += strprintf(".%06dZ", nTimeMicros%1000000);
strStamped += strprintf(".%06dZ", Ticks<std::chrono::microseconds>(now - now_seconds));
}
std::chrono::seconds mocktime = GetMockTime();
if (mocktime > 0s) {
Expand Down
5 changes: 0 additions & 5 deletions src/util/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,6 @@ int64_t GetTimeMillis()
return int64_t{GetSystemTime<std::chrono::milliseconds>().count()};
}

int64_t GetTimeMicros()
{
return int64_t{GetSystemTime<std::chrono::microseconds>().count()};
}

int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }

std::string FormatISO8601DateTime(int64_t nTime) {
Expand Down
10 changes: 5 additions & 5 deletions src/util/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::ch
using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>;
using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>;

using SystemClock = std::chrono::system_clock;

void UninterruptibleSleep(const std::chrono::microseconds& n);

/**
Expand Down Expand Up @@ -63,16 +65,14 @@ using MillisecondsDouble = std::chrono::duration<double, std::chrono::millisecon
* DEPRECATED
* Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
* ClockType is
* - std::chrono::steady_clock for steady time
* - std::chrono::system_clock for system time
* - NodeClock for mockable system time
* - SteadyClock/std::chrono::steady_clock for steady time
* - SystemClock/std::chrono::system_clock for system time
* - NodeClock for mockable system time
*/
int64_t GetTime();

/** Returns the system time (not mockable) */
int64_t GetTimeMillis();
/** Returns the system time (not mockable) */
int64_t GetTimeMicros();

/**
* DEPRECATED
Expand Down

0 comments on commit 2fadb26

Please sign in to comment.