LCOV - code coverage report
Current view: top level - src/util - time.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 27 55 49.1 %
Date: 2023-11-10 23:46:46 Functions: 7 11 63.6 %
Branches: 12 40 30.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :            : // Copyright (c) 2009-2022 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                 :          0 : void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
      24                 :            : 
      25                 :            : static std::atomic<int64_t> nMockTime(0); //!< For testing
      26                 :            : 
      27                 :          0 : bool ChronoSanityCheck()
      28                 :            : {
      29                 :            :     // std::chrono::system_clock.time_since_epoch and time_t(0) are not guaranteed
      30                 :            :     // to use the Unix epoch timestamp, prior to C++20, but in practice they almost
      31                 :            :     // certainly will. Any differing behavior will be assumed to be an error, unless
      32                 :            :     // certain platforms prove to consistently deviate, at which point we'll cope
      33                 :            :     // with it by adding offsets.
      34                 :            : 
      35                 :            :     // Create a new clock from time_t(0) and make sure that it represents 0
      36                 :            :     // seconds from the system_clock's time_since_epoch. Then convert that back
      37                 :            :     // to a time_t and verify that it's the same as before.
      38                 :          0 :     const time_t time_t_epoch{};
      39                 :          0 :     auto clock = std::chrono::system_clock::from_time_t(time_t_epoch);
      40         [ #  # ]:          0 :     if (std::chrono::duration_cast<std::chrono::seconds>(clock.time_since_epoch()).count() != 0) {
      41                 :          0 :         return false;
      42                 :            :     }
      43                 :            : 
      44                 :          0 :     time_t time_val = std::chrono::system_clock::to_time_t(clock);
      45         [ #  # ]:          0 :     if (time_val != time_t_epoch) {
      46                 :          0 :         return false;
      47                 :            :     }
      48                 :            : 
      49                 :            :     // Check that the above zero time is actually equal to the known unix timestamp.
      50                 :            :     struct tm epoch;
      51                 :            : #ifdef HAVE_GMTIME_R
      52         [ #  # ]:          0 :     if (gmtime_r(&time_val, &epoch) == nullptr) {
      53                 :            : #else
      54                 :            :     if (gmtime_s(&epoch, &time_val) != 0) {
      55                 :            : #endif
      56                 :          0 :         return false;
      57                 :            :     }
      58                 :            : 
      59 [ #  # ][ #  # ]:          0 :     if ((epoch.tm_sec != 0)  ||
      60         [ #  # ]:          0 :        (epoch.tm_min  != 0)  ||
      61         [ #  # ]:          0 :        (epoch.tm_hour != 0)  ||
      62         [ #  # ]:          0 :        (epoch.tm_mday != 1)  ||
      63         [ #  # ]:          0 :        (epoch.tm_mon  != 0)  ||
      64                 :          0 :        (epoch.tm_year != 70)) {
      65                 :          0 :         return false;
      66                 :            :     }
      67                 :          0 :     return true;
      68                 :          0 : }
      69                 :            : 
      70                 :      10139 : NodeClock::time_point NodeClock::now() noexcept
      71                 :            : {
      72         [ +  - ]:      10139 :     const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)};
      73                 :            :     const auto ret{
      74 [ +  - ][ +  + ]:      20278 :         mocktime.count() ?
      75         [ +  - ]:      10131 :             mocktime :
      76         [ +  - ]:          8 :             std::chrono::system_clock::now().time_since_epoch()};
      77 [ +  - ][ +  - ]:      10139 :     assert(ret > 0s);
                 [ -  + ]
      78         [ +  - ]:      10139 :     return time_point{ret};
      79                 :            : };
      80                 :            : 
      81                 :        102 : void SetMockTime(int64_t nMockTimeIn)
      82                 :            : {
      83                 :        102 :     Assert(nMockTimeIn >= 0);
      84                 :        102 :     nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
      85                 :        102 : }
      86                 :            : 
      87                 :          1 : void SetMockTime(std::chrono::seconds mock_time_in)
      88                 :            : {
      89                 :          1 :     nMockTime.store(mock_time_in.count(), std::memory_order_relaxed);
      90                 :          1 : }
      91                 :            : 
      92                 :          2 : std::chrono::seconds GetMockTime()
      93                 :            : {
      94                 :          2 :     return std::chrono::seconds(nMockTime.load(std::memory_order_relaxed));
      95                 :            : }
      96                 :            : 
      97                 :       9530 : int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
      98                 :            : 
      99                 :        153 : std::string FormatISO8601DateTime(int64_t nTime) {
     100                 :            :     struct tm ts;
     101                 :        153 :     time_t time_val = nTime;
     102                 :            : #ifdef HAVE_GMTIME_R
     103         [ -  + ]:        153 :     if (gmtime_r(&time_val, &ts) == nullptr) {
     104                 :            : #else
     105                 :            :     if (gmtime_s(&ts, &time_val) != 0) {
     106                 :            : #endif
     107                 :          0 :         return {};
     108                 :            :     }
     109                 :        153 :     return strprintf("%04i-%02i-%02iT%02i:%02i:%02iZ", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec);
     110                 :        153 : }
     111                 :            : 
     112                 :          2 : std::string FormatISO8601Date(int64_t nTime) {
     113                 :            :     struct tm ts;
     114                 :          2 :     time_t time_val = nTime;
     115                 :            : #ifdef HAVE_GMTIME_R
     116         [ -  + ]:          2 :     if (gmtime_r(&time_val, &ts) == nullptr) {
     117                 :            : #else
     118                 :            :     if (gmtime_s(&ts, &time_val) != 0) {
     119                 :            : #endif
     120                 :          0 :         return {};
     121                 :            :     }
     122                 :          2 :     return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
     123                 :          2 : }
     124                 :            : 
     125                 :          0 : struct timeval MillisToTimeval(int64_t nTimeout)
     126                 :            : {
     127                 :            :     struct timeval timeout;
     128                 :          0 :     timeout.tv_sec  = nTimeout / 1000;
     129                 :          0 :     timeout.tv_usec = (nTimeout % 1000) * 1000;
     130                 :          0 :     return timeout;
     131                 :            : }
     132                 :            : 
     133                 :          0 : struct timeval MillisToTimeval(std::chrono::milliseconds ms)
     134                 :            : {
     135                 :          0 :     return MillisToTimeval(count_milliseconds(ms));
     136                 :            : }

Generated by: LCOV version 1.14