Skip to content

Commit dd9f61a

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#25102: Remove unused GetTimeSeconds
fab9e8a Remove unused GetTimeSeconds (MacroFake) Pull request description: Seems confusing to have this helper when it is possible to get the system time in a type-safe way by simply calling `std::chrono::system_clock::now` (C++11). This patch replaces `GetTimeSeconds` and removes it: * in `bitcoin-cli.cpp` by `system_clock` * in `test/fuzz/fuzz.cpp` by `steady_clock` ACKs for top commit: laanwj: Code review ACK fab9e8a naumenkogs: ACK fab9e8a Tree-SHA512: 517e300b0baf271cfbeebd4a0838871acbea9360f9dd23572a751335c20c1ba261b1b5ee0aec8a36abd20c94fab83ce94f46042745279aca1f0ca2f885a03b6e
2 parents e3bab43 + fab9e8a commit dd9f61a

File tree

5 files changed

+21
-20
lines changed

5 files changed

+21
-20
lines changed

src/bitcoin-cli.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,26 @@
99

1010
#include <chainparamsbase.h>
1111
#include <clientversion.h>
12+
#include <compat/stdin.h>
1213
#include <policy/feerate.h>
1314
#include <rpc/client.h>
1415
#include <rpc/mining.h>
1516
#include <rpc/protocol.h>
1617
#include <rpc/request.h>
1718
#include <tinyformat.h>
19+
#include <univalue.h>
1820
#include <util/strencodings.h>
1921
#include <util/system.h>
2022
#include <util/translation.h>
2123
#include <util/url.h>
2224

2325
#include <algorithm>
26+
#include <chrono>
2427
#include <cmath>
28+
#include <cstdio>
2529
#include <functional>
2630
#include <memory>
2731
#include <optional>
28-
#include <stdio.h>
2932
#include <string>
3033
#include <tuple>
3134

@@ -37,8 +40,11 @@
3740
#include <event2/keyvalq_struct.h>
3841
#include <support/events.h>
3942

40-
#include <univalue.h>
41-
#include <compat/stdin.h>
43+
// The server returns time values from a mockable system clock, but it is not
44+
// trivial to get the mocked time from the server, nor is it needed for now, so
45+
// just use a plain system_clock.
46+
using CliClock = std::chrono::system_clock;
47+
using CliSeconds = std::chrono::time_point<CliClock, std::chrono::seconds>;
4248

4349
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
4450
UrlDecodeFn* const URL_DECODE = urlDecode;
@@ -433,7 +439,7 @@ class NetinfoRequestHandler : public BaseRequestHandler
433439
if (conn_type == "addr-fetch") return "addr";
434440
return "";
435441
}
436-
const int64_t m_time_now{GetTimeSeconds()};
442+
const int64_t m_time_now{count_seconds(Now<CliSeconds>())};
437443

438444
public:
439445
static constexpr int ID_PEERINFO = 0;

src/test/fuzz/fuzz.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ int main(int argc, char** argv)
194194
return 0;
195195
}
196196
std::signal(SIGABRT, signal_handler);
197-
int64_t start_time = GetTimeSeconds();
197+
const auto start_time{Now<SteadySeconds>()};
198198
int tested = 0;
199199
for (int i = 1; i < argc; ++i) {
200200
fs::path input_path(*(argv + i));
@@ -215,8 +215,8 @@ int main(int argc, char** argv)
215215
buffer.clear();
216216
}
217217
}
218-
int64_t end_time = GetTimeSeconds();
219-
std::cout << g_fuzz_target << ": succeeded against " << tested << " files in " << (end_time - start_time) << "s." << std::endl;
218+
const auto end_time{Now<SteadySeconds>()};
219+
std::cout << g_fuzz_target << ": succeeded against " << tested << " files in " << count_seconds(end_time - start_time) << "s." << std::endl;
220220
#endif
221221
return 0;
222222
}

src/test/util_tests.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,6 @@ BOOST_AUTO_TEST_CASE(util_FormatParseISO8601DateTime)
265265
BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:00Z"), 0);
266266
BOOST_CHECK_EQUAL(ParseISO8601DateTime("1960-01-01T00:00:00Z"), 0);
267267
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2011-09-30T23:36:17Z"), 1317425777);
268-
269-
auto time = GetTimeSeconds();
270-
BOOST_CHECK_EQUAL(ParseISO8601DateTime(FormatISO8601DateTime(time)), time);
271268
}
272269

273270
BOOST_AUTO_TEST_CASE(util_FormatISO8601Date)

src/util/time.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,6 @@ int64_t GetTimeMicros()
115115
return int64_t{GetSystemTime<std::chrono::microseconds>().count()};
116116
}
117117

118-
int64_t GetTimeSeconds()
119-
{
120-
return int64_t{GetSystemTime<std::chrono::seconds>().count()};
121-
}
122-
123118
int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
124119

125120
std::string FormatISO8601DateTime(int64_t nTime) {

src/util/time.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,20 @@ using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, st
2121
void UninterruptibleSleep(const std::chrono::microseconds& n);
2222

2323
/**
24-
* Helper to count the seconds of a duration.
24+
* Helper to count the seconds of a duration/time_point.
2525
*
26-
* All durations should be using std::chrono and calling this should generally
26+
* All durations/time_points should be using std::chrono and calling this should generally
2727
* be avoided in code. Though, it is still preferred to an inline t.count() to
2828
* protect against a reliance on the exact type of t.
2929
*
30-
* This helper is used to convert durations before passing them over an
30+
* This helper is used to convert durations/time_points before passing them over an
3131
* interface that doesn't support std::chrono (e.g. RPC, debug log, or the GUI)
3232
*/
33+
template <typename Clock>
34+
constexpr int64_t count_seconds(std::chrono::time_point<Clock, std::chrono::seconds> t)
35+
{
36+
return t.time_since_epoch().count();
37+
}
3338
constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); }
3439
constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); }
3540
constexpr int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); }
@@ -51,8 +56,6 @@ int64_t GetTime();
5156
int64_t GetTimeMillis();
5257
/** Returns the system time (not mockable) */
5358
int64_t GetTimeMicros();
54-
/** Returns the system time (not mockable) */
55-
int64_t GetTimeSeconds(); // Like GetTime(), but not mockable
5659

5760
/**
5861
* DEPRECATED

0 commit comments

Comments
 (0)