refactor: Replace GetTimeMicros by SystemClock #27233

pull maflcko wants to merge 1 commits into bitcoin:master from maflcko:2303-time-🎁 changing 3 files +9 −13
  1. maflcko commented at 2:21 pm on March 9, 2023: member

    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.

  2. refactor: Replace GetTimeMicros by SystemClock faf3f12424
  3. DrahtBot commented at 2:21 pm on March 9, 2023: contributor

    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.

  4. DrahtBot added the label Refactoring on Mar 9, 2023
  5. dergoegge commented at 3:19 pm on March 9, 2023: member
    Concept ACK
  6. fanquake requested review from willcl-ark on Mar 10, 2023
  7. fanquake commented at 8:04 pm on March 10, 2023: member
    Concept ACK
  8. in src/util/time.h:32 in faf3f12424
    28@@ -29,6 +29,8 @@ using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::ch
    29 using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>;
    30 using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>;
    31 
    32+using SystemClock = std::chrono::system_clock;
    


    willcl-ark commented at 12:01 pm on March 21, 2023:
    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.

    maflcko commented at 12:20 pm on March 21, 2023:
    If this is used in more places in the future, it seems convenient to have NodeClock, SteadyClock, and SystemClock in the same header?
  9. willcl-ark approved
  10. willcl-ark commented at 12:14 pm on March 21, 2023: contributor

    tACK faf3f1242

    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.

  11. fanquake requested review from john-moffett on Mar 22, 2023
  12. in src/logging.cpp:356 in faf3f12424
    353-        strStamped = FormatISO8601DateTime(nTimeMicros/1000000);
    354+        const auto now{SystemClock::now()};
    355+        const auto now_seconds{std::chrono::time_point_cast<std::chrono::seconds>(now)};
    356+        strStamped = FormatISO8601DateTime(TicksSinceEpoch<std::chrono::seconds>(now_seconds));
    357         if (m_log_time_micros) {
    358             strStamped.pop_back();
    


    john-moffett commented at 5:32 pm on March 22, 2023:
    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?

    maflcko commented at 8:48 am on March 23, 2023:
    Yes, I am happy to review a pull doing this.

    fanquake commented at 10:17 am on March 23, 2023:
    @john-moffett want to open a follow up?

    maflcko commented at 11:06 am on March 23, 2023:
    gmtime can also be removed once we have C++20, but not sure if and when that happens

    maflcko commented at 5:22 pm on December 14, 2023:
  13. john-moffett approved
  14. john-moffett commented at 6:36 pm on March 22, 2023: contributor
    ACK faf3f12424fa8558e65fa3f1dd3aa1d0eea8604e changes, but left a comment for the existing code.
  15. fanquake merged this on Mar 23, 2023
  16. fanquake closed this on Mar 23, 2023

  17. maflcko deleted the branch on Mar 23, 2023
  18. sidhujag referenced this in commit 6d78180de9 on Mar 23, 2023
  19. fanquake referenced this in commit 27ad26de2f on Apr 5, 2023
  20. sidhujag referenced this in commit 8040a5b31b on Apr 5, 2023

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-23 09:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me