Skip to content

Conversation

@maflcko
Copy link
Member

@maflcko maflcko commented Mar 9, 2023

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.

@DrahtBot
Copy link
Contributor

DrahtBot commented Mar 9, 2023

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK willcl-ark, john-moffett
Concept ACK dergoegge, fanquake

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

@dergoegge
Copy link
Member

Concept ACK

@fanquake fanquake requested a review from willcl-ark March 10, 2023 20:04
@fanquake
Copy link
Member

Concept ACK

Copy link
Member

@willcl-ark willcl-ark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK faf3f12

As I was unfamiliar with this code it was a little counter-intuitive to me at first that we truncate the time to seconds, before re-adding the microseconds if m_log_time_micros was set, but sure enough it mirrors the old behaviour.

I also left one tiny nit/comment which doesn't need to be addressed.

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: as we only use this in one single place (logging.cpp) it almost seems to make more sense just to drop this and directly use std::chrono::system_clock there? Although having SteadyClock and SystemClock as the two options perhaps prefereable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is used in more places in the future, it seems convenient to have NodeClock, SteadyClock, and SystemClock in the same header?

@fanquake fanquake requested a review from john-moffett March 22, 2023 09:47
Copy link
Contributor

@john-moffett john-moffett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK faf3f12 changes, but left a comment for the existing code.

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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very unlikely, but could this result in undefined behavior? FormatISO8601DateTime can theoretically return an empty string if it's fed, eg, an extremely large value and gmtime fails. While I can't see any reason for that happening (the libstdc++ ::max() values I checked are reasonable) maybe best to avoid it just in case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I am happy to review a pull doing this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@john-moffett want to open a follow up?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gmtime can also be removed once we have C++20, but not sure if and when that happens

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #29081

@fanquake fanquake merged commit 2fadb26 into bitcoin:master Mar 23, 2023
@maflcko maflcko deleted the 2303-time-🎁 branch March 23, 2023 11:06
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Mar 23, 2023
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
fanquake added a commit to bitcoin-core/gui that referenced this pull request Apr 5, 2023
…non-empty to avoid undefined behavior

73f4eb5 Check that the Timestamp String is valid (John Moffett)

Pull request description:

  Follow-up to bitcoin/bitcoin#27233

  The current `FormatISO8601DateTime` function will return an empty string if it encounters an error when converting the `int64_t` seconds-since-epoch to a formatted date time. In the unlikely case that happens, here `strStamped.pop_back()` would be undefined behavior.

ACKs for top commit:
  MarcoFalke:
    lgtm ACK 73f4eb5
  stickies-v:
    ACK 73f4eb5

Tree-SHA512: 089ed639c193deb98870a8385039b31b4baed821ea907937bfc6f65a5b0981bbf8284b2afec81b2d0a922e2340716b48cf55349640eb6b8c311ef7af25abc361
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Apr 5, 2023
…y to avoid undefined behavior

73f4eb5 Check that the Timestamp String is valid (John Moffett)

Pull request description:

  Follow-up to bitcoin#27233

  The current `FormatISO8601DateTime` function will return an empty string if it encounters an error when converting the `int64_t` seconds-since-epoch to a formatted date time. In the unlikely case that happens, here `strStamped.pop_back()` would be undefined behavior.

ACKs for top commit:
  MarcoFalke:
    lgtm ACK 73f4eb5
  stickies-v:
    ACK 73f4eb5

Tree-SHA512: 089ed639c193deb98870a8385039b31b4baed821ea907937bfc6f65a5b0981bbf8284b2afec81b2d0a922e2340716b48cf55349640eb6b8c311ef7af25abc361
@bitcoin bitcoin locked and limited conversation to collaborators Dec 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants