-
Notifications
You must be signed in to change notification settings - Fork 37.7k
util: Add type safe GetTime #16046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
util: Add type safe GetTime #16046
Conversation
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsNo conflicts as of last run. |
2d19686
to
fac4143
Compare
Concept ACK. |
Concept ACK |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK faaff50.
src/test/util_tests.cpp
Outdated
{ | ||
SetMockTime(111); | ||
// Check that mock time does not change after a sleep | ||
for (const auto& num_sleep : {0, 1}) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the loop? Isn't enough one loop after MilliSleep(1)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, those are unit tests. Probably enough to not have them at all 🤷♀️
@@ -8,27 +8,34 @@ | |||
|
|||
#include <stdint.h> | |||
#include <string> | |||
#include <chrono> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, sorted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not possible and would lead to compile errors:
util/time.cpp: In function ‘std::__cxx11::string FormatISO8601DateTime(int64_t)’:
util/time.cpp:84:9: error: ‘gmtime_r’ was not declared in this scope
if (gmtime_r(&time_val, &ts) == nullptr) {
^~~~~~~~
util/time.cpp:84:9: note: suggested alternative: ‘gmtime_s’
if (gmtime_r(&time_val, &ts) == nullptr) {
^~~~~~~~
gmtime_s
util/time.cpp: In function ‘std::__cxx11::string FormatISO8601Date(int64_t)’:
util/time.cpp:97:9: error: ‘gmtime_r’ was not declared in this scope
if (gmtime_r(&time_val, &ts) == nullptr) {
^~~~~~~~
util/time.cpp:97:9: note: suggested alternative: ‘gmtime_s’
if (gmtime_r(&time_val, &ts) == nullptr) {
^~~~~~~~
gmtime_s
Makefile:11303: recipe for target 'util/libbitcoin_util_a-time.o' failed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in #18358, maybe?
|
||
/** | ||
* GetTimeMicros() and GetTimeMillis() both return the system time, but in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to keep the information here that GetTimeMicros() and GetTimeMillis() aren't mocked
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Their docstring already says Returns the system time
. Anything I should add?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to explicitly mention that they ignore the mocked time. My implicit assumption while reading the documentation would be that mocktime would affect everyting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, makes sense. Fixed up documentation a bit
utACK fa01366. |
utACK fa01366 |
fa01366 util: Add type safe GetTime (MarcoFalke) Pull request description: There are basically two ways to get the time in Bitcoin Core: * get the system time (via `GetSystemTimeInSeconds` or `GetTime{Millis,Micros}`) * get the mockable time (via `GetTime`) Both return the same type (a plain int). This can lead to (test-only) bugs such as 99464bc. Fix that by deprecating `GetTime` and adding a `GetTime<>` that returns the mockable time in a non-int type. The new util function is currently unused, but new code should it where possible. ACKs for commit fa0136: promag: utACK fa01366. Tree-SHA512: efab9c463f079fd8fd3030c479637c7b1e8be567a881234bd0f555c8f87e518e3b43ef2466128103db8fc40295aaf24e87ad76d91f338c631246fc703477e95c
Summary: This is a backport of [[bitcoin/bitcoin#16046 | PR16046]] Test Plan: ninja check ninja bench-bitcoin Reviewers: #bitcoin_abc, nakihito Reviewed By: nakihito Differential Revision: https://reviews.bitcoinabc.org/D5445
fa01366 util: Add type safe GetTime (MarcoFalke) Pull request description: There are basically two ways to get the time in Bitcoin Core: * get the system time (via `GetSystemTimeInSeconds` or `GetTime{Millis,Micros}`) * get the mockable time (via `GetTime`) Both return the same type (a plain int). This can lead to (test-only) bugs such as 99464bc. Fix that by deprecating `GetTime` and adding a `GetTime<>` that returns the mockable time in a non-int type. The new util function is currently unused, but new code should it where possible. ACKs for commit fa0136: promag: utACK fa01366. Tree-SHA512: efab9c463f079fd8fd3030c479637c7b1e8be567a881234bd0f555c8f87e518e3b43ef2466128103db8fc40295aaf24e87ad76d91f338c631246fc703477e95c
Summary: This is a backport of [[bitcoin/bitcoin#16046 | PR16046]] Test Plan: ninja check ninja bench-bitcoin Reviewers: #bitcoin_abc, nakihito Reviewed By: nakihito Differential Revision: https://reviews.bitcoinabc.org/D5445
f3f3d6c util: fix compilation with mingw-w64 7.0.0 (Fuzzbawls) f24da7d util: Avoid potential uninitialized read in FormatISO8601DateTime (Fuzzbawls) 120a12a util: Add type safe GetTime (Fuzzbawls) be48766 Fix for utiltime to compile with msvc. (Aaron Clauson) 2833406 Avoid std::locale/imbue in DateTimeStrFormat (Pieter Wuille) 3331c25 Format automatic wallet backup file names in ISO 8601 basic format (Fuzzbawls) c32a208 Format timestamps using ISO 8601 formatting (e.g. "2018-02-28T12:34:56Z") (practicalswift) f8bd173 [logging] log system time and mock time (John Newbery) Pull request description: This is a backport of a collection of upstream PRs to bring our time utility more up to date. The following upstream PRs are included: - bitcoin#10383 - bitcoin#12567 - bitcoin#12973 - bitcoin#13031 - bitcoin#16046 - bitcoin#18162 - bitcoin#18358 The two user-facing notable changes here have been documented in the release notes template. I've also connected the functionality that `-logtimemicros` was supposed to provide. ACKs for top commit: furszy: code review ACK f3f3d6c random-zebra: utACK f3f3d6c after rebase, and merging... Tree-SHA512: 64f9cc1f7fc65c192f3b3ab869c057a81e007cf0fae82ecd23993c6b51830e4bc80656c699ba40bb8e019115a24d1ea88a618be0c0d7112749d9ce125d62ce44
fa01366 util: Add type safe GetTime (MarcoFalke) Pull request description: There are basically two ways to get the time in Bitcoin Core: * get the system time (via `GetSystemTimeInSeconds` or `GetTime{Millis,Micros}`) * get the mockable time (via `GetTime`) Both return the same type (a plain int). This can lead to (test-only) bugs such as 99464bc. Fix that by deprecating `GetTime` and adding a `GetTime<>` that returns the mockable time in a non-int type. The new util function is currently unused, but new code should it where possible. ACKs for commit fa0136: promag: utACK fa01366. Tree-SHA512: efab9c463f079fd8fd3030c479637c7b1e8be567a881234bd0f555c8f87e518e3b43ef2466128103db8fc40295aaf24e87ad76d91f338c631246fc703477e95c
There are basically two ways to get the time in Bitcoin Core:
GetSystemTimeInSeconds
orGetTime{Millis,Micros}
)GetTime
)Both return the same type (a plain int). This can lead to (test-only) bugs such as 99464bc.
Fix that by deprecating
GetTime
and adding aGetTime<>
that returns the mockable time in a non-int type. The new util function is currently unused, but new code should it where possible.