LCOV - code coverage report
Current view: top level - src/util - time.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 97.5 % 81 79
Test Date: 2026-04-07 04:59:12 Functions: 93.8 % 16 15
Branches: 93.3 % 60 56

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-present 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                 :             : #include <util/time.h>
       7                 :             : 
       8                 :             : #include <tinyformat.h>
       9                 :             : #include <util/check.h>
      10                 :             : #include <util/strencodings.h>
      11                 :             : 
      12                 :             : #include <array>
      13                 :             : #include <atomic>
      14                 :             : #include <chrono>
      15                 :             : #include <compare>
      16                 :             : #include <optional>
      17                 :             : #include <string>
      18                 :             : #include <string_view>
      19                 :             : #include <thread>
      20                 :             : 
      21                 :             : #ifdef WIN32
      22                 :             : #include <winsock2.h>
      23                 :             : #else
      24                 :             : #include <sys/time.h>
      25                 :             : #endif
      26                 :             : 
      27                 :             : static constexpr std::array<std::string_view, 7> weekdays{"Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed"}; // 1970-01-01 was a Thursday.
      28                 :             : static constexpr std::array<std::string_view, 12> months{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
      29                 :             : 
      30                 :        1045 : void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
      31                 :             : 
      32                 :             : static std::atomic<std::chrono::seconds> g_mock_time{}; //!< For testing
      33                 :             : std::atomic<bool> g_used_system_time{false};
      34                 :             : static std::atomic<MockableSteadyClock::mock_time_point::duration> g_mock_steady_time{}; //!< For testing
      35                 :             : 
      36                 :     3965845 : NodeClock::time_point NodeClock::now() noexcept
      37                 :             : {
      38         [ +  + ]:     3965845 :     const auto mocktime{g_mock_time.load(std::memory_order_relaxed)};
      39         [ +  + ]:     3965845 :     if (!mocktime.count()) {
      40                 :     3189726 :         g_used_system_time = true;
      41                 :             :     }
      42         [ +  + ]:     3965845 :     const auto ret{
      43         [ +  + ]:     3965845 :         mocktime.count() ?
      44                 :             :             mocktime :
      45                 :     3965845 :             std::chrono::system_clock::now().time_since_epoch()};
      46         [ -  + ]:     3965845 :     assert(ret > 0s);
      47                 :     3965845 :     return time_point{ret};
      48                 :             : };
      49                 :             : 
      50                 :        8471 : void SetMockTime(int64_t nMockTimeIn) { SetMockTime(std::chrono::seconds{nMockTimeIn}); }
      51                 :           9 : void SetMockTime(std::chrono::time_point<NodeClock, std::chrono::seconds> mock) { SetMockTime(mock.time_since_epoch()); }
      52                 :        9174 : void SetMockTime(std::chrono::seconds mock_time_in)
      53                 :             : {
      54         [ -  + ]:        9174 :     Assert(mock_time_in >= 0s);
      55                 :        9174 :     g_mock_time.store(mock_time_in, std::memory_order_relaxed);
      56                 :        9174 : }
      57                 :             : 
      58                 :     5607833 : std::chrono::seconds GetMockTime()
      59                 :             : {
      60                 :     5607833 :     return g_mock_time.load(std::memory_order_relaxed);
      61                 :             : }
      62                 :             : 
      63                 :          34 : MockableSteadyClock::time_point MockableSteadyClock::now() noexcept
      64                 :             : {
      65         [ +  + ]:          34 :     const auto mocktime{g_mock_steady_time.load(std::memory_order_relaxed)};
      66         [ +  + ]:          34 :     if (!mocktime.count()) {
      67                 :           2 :         g_used_system_time = true;
      68                 :             :     }
      69         [ +  + ]:          34 :     const auto ret{
      70         [ +  + ]:          34 :         mocktime.count() ?
      71                 :             :             mocktime :
      72                 :          34 :             std::chrono::steady_clock::now().time_since_epoch()};
      73                 :          34 :     return time_point{ret};
      74                 :             : };
      75                 :             : 
      76                 :          27 : void MockableSteadyClock::SetMockTime(mock_time_point::duration mock_time_in)
      77                 :             : {
      78         [ -  + ]:          27 :     Assert(mock_time_in >= 0s);
      79                 :          27 :     g_mock_steady_time.store(mock_time_in, std::memory_order_relaxed);
      80                 :          27 : }
      81                 :             : 
      82                 :          10 : void MockableSteadyClock::ClearMockTime()
      83                 :             : {
      84                 :          10 :     g_mock_steady_time.store(0ms, std::memory_order_relaxed);
      85                 :          10 : }
      86                 :             : 
      87                 :      302948 : int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
      88                 :             : 
      89                 :     6712280 : std::string FormatISO8601DateTime(int64_t nTime)
      90                 :             : {
      91                 :     6712280 :     const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
      92                 :     6712280 :     const auto days{std::chrono::floor<std::chrono::days>(secs)};
      93                 :     6712280 :     const std::chrono::year_month_day ymd{days};
      94                 :     6712280 :     const std::chrono::hh_mm_ss hms{secs - days};
      95                 :     6712280 :     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());
      96                 :             : }
      97                 :             : 
      98                 :        2770 : std::string FormatISO8601Date(int64_t nTime)
      99                 :             : {
     100                 :        2770 :     const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
     101                 :        2770 :     const auto days{std::chrono::floor<std::chrono::days>(secs)};
     102                 :        2770 :     const std::chrono::year_month_day ymd{days};
     103                 :        2770 :     return strprintf("%04i-%02u-%02u", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()});
     104                 :             : }
     105                 :             : 
     106                 :          76 : std::optional<int64_t> ParseISO8601DateTime(std::string_view str)
     107                 :             : {
     108                 :          76 :     constexpr auto FMT_SIZE{std::string_view{"2000-01-01T01:01:01Z"}.size()};
     109   [ +  +  +  +  :          76 :     if (str.size() != FMT_SIZE || str[4] != '-' || str[7] != '-' || str[10] != 'T' || str[13] != ':' || str[16] != ':' || str[19] != 'Z') {
          +  +  +  +  +  
             +  +  +  +  
                      + ]
     110                 :          20 :         return {};
     111                 :             :     }
     112                 :          56 :     const auto year{ToIntegral<uint16_t>(str.substr(0, 4))};
     113                 :          56 :     const auto month{ToIntegral<uint8_t>(str.substr(5, 2))};
     114                 :          56 :     const auto day{ToIntegral<uint8_t>(str.substr(8, 2))};
     115                 :          56 :     const auto hour{ToIntegral<uint8_t>(str.substr(11, 2))};
     116                 :          56 :     const auto min{ToIntegral<uint8_t>(str.substr(14, 2))};
     117                 :          56 :     const auto sec{ToIntegral<uint8_t>(str.substr(17, 2))};
     118   [ +  +  +  +  :          56 :     if (!year || !month || !day || !hour || !min || !sec) {
          +  +  +  +  +  
                +  +  + ]
     119                 :          28 :         return {};
     120                 :             :     }
     121         [ +  + ]:          28 :     const std::chrono::year_month_day ymd{std::chrono::year{*year}, std::chrono::month{*month}, std::chrono::day{*day}};
     122         [ +  + ]:          28 :     if (!ymd.ok()) {
     123                 :           4 :         return {};
     124                 :             :     }
     125                 :          24 :     const auto time{std::chrono::hours{*hour} + std::chrono::minutes{*min} + std::chrono::seconds{*sec}};
     126                 :          24 :     const auto tp{std::chrono::sys_days{ymd} + time};
     127                 :          24 :     return int64_t{TicksSinceEpoch<std::chrono::seconds>(tp)};
     128                 :             : }
     129                 :             : 
     130                 :          20 : std::string FormatRFC1123DateTime(int64_t time)
     131                 :             : {
     132         [ +  + ]:          20 :     if (time < -62167219200 || 253402300799 < time) {
     133                 :             :         // 4-digit year, so only support years 0 to 9999
     134                 :           6 :         return "";
     135                 :             :     }
     136                 :          14 :     const std::chrono::sys_seconds secs{std::chrono::seconds{time}};
     137                 :          14 :     const auto days{std::chrono::floor<std::chrono::days>(secs)};
     138         [ +  + ]:          14 :     const auto w{days.time_since_epoch().count() % 7}; // will be in the range [-6, 6]
     139         [ +  + ]:          14 :     std::string_view weekday{weekdays.at(w >= 0 ? w : w + 7)};
     140                 :          14 :     const std::chrono::year_month_day ymd{days};
     141         [ +  - ]:          14 :     std::string_view month{months.at(unsigned{ymd.month()} - 1)};
     142                 :          14 :     const std::chrono::hh_mm_ss hms{secs - days};
     143                 :             :     // examples: Mon, 27 Jul 2009 12:28:53 GMT
     144                 :             :     //           Fri, 31 May 2024 19:18:04 GMT
     145                 :          14 :     return strprintf("%03s, %02u %03s %04i %02i:%02i:%02i GMT", weekday, unsigned{ymd.day()}, month, signed{ymd.year()}, hms.hours().count(), hms.minutes().count(), hms.seconds().count());
     146                 :             : }
     147                 :             : 
     148                 :           3 : struct timeval MillisToTimeval(int64_t nTimeout)
     149                 :             : {
     150                 :           3 :     struct timeval timeout;
     151                 :           3 :     timeout.tv_sec  = nTimeout / 1000;
     152                 :           3 :     timeout.tv_usec = (nTimeout % 1000) * 1000;
     153                 :           3 :     return timeout;
     154                 :             : }
     155                 :             : 
     156                 :           0 : struct timeval MillisToTimeval(std::chrono::milliseconds ms)
     157                 :             : {
     158                 :           0 :     return MillisToTimeval(count_milliseconds(ms));
     159                 :             : }
        

Generated by: LCOV version 2.0-1