Skip to content

Commit 2fadb26

Browse files
committed
Merge bitcoin/bitcoin#27233: refactor: Replace GetTimeMicros by SystemClock
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
2 parents 4c6b7d3 + faf3f12 commit 2fadb26

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

src/logging.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,12 @@ std::string BCLog::Logger::LogTimestampStr(const std::string& str)
349349
return str;
350350

351351
if (m_started_new_line) {
352-
int64_t nTimeMicros = GetTimeMicros();
353-
strStamped = FormatISO8601DateTime(nTimeMicros/1000000);
352+
const auto now{SystemClock::now()};
353+
const auto now_seconds{std::chrono::time_point_cast<std::chrono::seconds>(now)};
354+
strStamped = FormatISO8601DateTime(TicksSinceEpoch<std::chrono::seconds>(now_seconds));
354355
if (m_log_time_micros) {
355356
strStamped.pop_back();
356-
strStamped += strprintf(".%06dZ", nTimeMicros%1000000);
357+
strStamped += strprintf(".%06dZ", Ticks<std::chrono::microseconds>(now - now_seconds));
357358
}
358359
std::chrono::seconds mocktime = GetMockTime();
359360
if (mocktime > 0s) {

src/util/time.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,6 @@ int64_t GetTimeMillis()
107107
return int64_t{GetSystemTime<std::chrono::milliseconds>().count()};
108108
}
109109

110-
int64_t GetTimeMicros()
111-
{
112-
return int64_t{GetSystemTime<std::chrono::microseconds>().count()};
113-
}
114-
115110
int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
116111

117112
std::string FormatISO8601DateTime(int64_t nTime) {

src/util/time.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::ch
2929
using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>;
3030
using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>;
3131

32+
using SystemClock = std::chrono::system_clock;
33+
3234
void UninterruptibleSleep(const std::chrono::microseconds& n);
3335

3436
/**
@@ -63,16 +65,14 @@ using MillisecondsDouble = std::chrono::duration<double, std::chrono::millisecon
6365
* DEPRECATED
6466
* Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
6567
* ClockType is
66-
* - std::chrono::steady_clock for steady time
67-
* - std::chrono::system_clock for system time
68-
* - NodeClock for mockable system time
68+
* - SteadyClock/std::chrono::steady_clock for steady time
69+
* - SystemClock/std::chrono::system_clock for system time
70+
* - NodeClock for mockable system time
6971
*/
7072
int64_t GetTime();
7173

7274
/** Returns the system time (not mockable) */
7375
int64_t GetTimeMillis();
74-
/** Returns the system time (not mockable) */
75-
int64_t GetTimeMicros();
7676

7777
/**
7878
* DEPRECATED

0 commit comments

Comments
 (0)