LCOV - code coverage report
Current view: top level - src/util - time.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 50 50 100.0 %
Date: 2026-06-25 07:23:43 Functions: 15 15 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2021 The Bitcoin Core developers
       3             : // Distributed under the MIT software license, see the accompanying
       4             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5             : 
       6             : #if defined(HAVE_CONFIG_H)
       7             : #include <config/bitcoin-config.h>
       8             : #endif
       9             : 
      10             : #include <compat/compat.h>
      11             : #include <tinyformat.h>
      12             : #include <util/time.h>
      13             : #include <util/check.h>
      14             : 
      15             : #include <atomic>
      16             : #include <chrono>
      17             : #include <ctime>
      18             : #include <locale>
      19             : #include <thread>
      20             : #include <sstream>
      21             : #include <string>
      22             : 
      23     1297140 : void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
      24             : 
      25             : static std::atomic<std::chrono::seconds> g_mock_time{}; //!< For testing
      26             : 
      27    17783012 : NodeClock::time_point NodeClock::now() noexcept
      28             : {
      29    17783012 :     const auto mocktime{g_mock_time.load(std::memory_order_relaxed)};
      30             :     const auto ret{
      31    35569257 :         mocktime.count() ?
      32    16574509 :             mocktime :
      33     1208233 :             std::chrono::system_clock::now().time_since_epoch()};
      34    17783137 :     assert(ret > 0s);
      35    17782113 :     return time_point{ret};
      36             : };
      37             : 
      38             : template <typename T>
      39    38009803 : static T GetSystemTime()
      40             : {
      41    38009803 :     const auto now = std::chrono::duration_cast<T>(std::chrono::system_clock::now().time_since_epoch());
      42    38009803 :     assert(now.count() > 0);
      43    38009803 :     return now;
      44             : }
      45             : 
      46       69743 : void SetMockTime(int64_t nMockTimeIn) { SetMockTime(std::chrono::seconds{nMockTimeIn}); }
      47       70380 : void SetMockTime(std::chrono::seconds mock_time_in)
      48             : {
      49       70380 :     Assert(mock_time_in >= 0s);
      50       70380 :     g_mock_time.store(mock_time_in, std::memory_order_relaxed);
      51       70380 : }
      52             : 
      53    26366037 : std::chrono::seconds GetMockTime()
      54             : {
      55    26366037 :     return g_mock_time.load(std::memory_order_relaxed);
      56             : }
      57             : 
      58    38009907 : int64_t GetTimeMicros()
      59             : {
      60    38009907 :     return int64_t{GetSystemTime<std::chrono::microseconds>().count()};
      61             : }
      62             : 
      63     6569575 : int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
      64             : 
      65    26658093 : std::string FormatISO8601DateTime(int64_t nTime)
      66             : {
      67    26658093 :     const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
      68    26658093 :     const auto days{std::chrono::floor<std::chrono::days>(secs)};
      69    26658093 :     const std::chrono::year_month_day ymd{days};
      70    26658093 :     const std::chrono::hh_mm_ss hms{secs - days};
      71    26658093 :     return strprintf("%04i-%02u-%02uT%02i:%02i:%02iZ", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()}, hms.hours().count(), hms.minutes().count(), hms.seconds().count());
      72             : }
      73             : 
      74        6048 : std::string FormatISO8601Date(int64_t nTime)
      75             : {
      76        6048 :     const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
      77        6048 :     const auto days{std::chrono::floor<std::chrono::days>(secs)};
      78        6048 :     const std::chrono::year_month_day ymd{days};
      79        6048 :     return strprintf("%04i-%02u-%02u", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()});
      80             : }
      81             : 
      82           2 : std::string FormatISO8601Time(int64_t nTime)
      83             : {
      84           2 :     const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
      85           2 :     const auto days{std::chrono::floor<std::chrono::days>(secs)};
      86           2 :     const std::chrono::hh_mm_ss hms{secs - days};
      87           2 :     return strprintf("%02i:%02i:%02iZ", hms.hours().count(), hms.minutes().count(), hms.seconds().count());
      88             : }
      89             : 
      90    29784563 : struct timespec MillisToTimespec(int64_t nTimeout)
      91             : {
      92             :     struct timespec timeout;
      93    29784563 :     timeout.tv_sec = nTimeout / 1000;
      94    29784563 :     timeout.tv_nsec = (nTimeout % 1000) * 1000 * 1000;
      95    29784563 :     return timeout;
      96             : }
      97             : 
      98        6096 : struct timeval MillisToTimeval(int64_t nTimeout)
      99             : {
     100             :     struct timeval timeout;
     101        6096 :     timeout.tv_sec  = nTimeout / 1000;
     102        6096 :     timeout.tv_usec = (nTimeout % 1000) * 1000;
     103        6096 :     return timeout;
     104             : }
     105             : 
     106    29784563 : struct timespec MillisToTimespec(std::chrono::milliseconds ms)
     107             : {
     108    29784563 :     return MillisToTimespec(count_milliseconds(ms));
     109             : }
     110             : 
     111        6094 : struct timeval MillisToTimeval(std::chrono::milliseconds ms)
     112             : {
     113        6094 :     return MillisToTimeval(count_milliseconds(ms));
     114             : }

Generated by: LCOV version 1.16