LCOV - code coverage report
Current view: top level - src/util - time.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 89 103 86.4 %
Date: 2022-04-21 14:51:19 Functions: 20 20 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 23 34 67.6 %

           Branch data     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.h>
#      11                 :            : #include <util/time.h>
#      12                 :            : 
#      13                 :            : #include <util/check.h>
#      14                 :            : 
#      15                 :            : #include <atomic>
#      16                 :            : #include <boost/date_time/posix_time/posix_time.hpp>
#      17                 :            : #include <ctime>
#      18                 :            : #include <thread>
#      19                 :            : 
#      20                 :            : #include <tinyformat.h>
#      21                 :            : 
#      22                 :       1052 : void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
#      23                 :            : 
#      24                 :            : static std::atomic<int64_t> nMockTime(0); //!< For testing
#      25                 :            : 
#      26                 :            : int64_t GetTime()
#      27                 :     745915 : {
#      28                 :     745915 :     int64_t mocktime = nMockTime.load(std::memory_order_relaxed);
#      29         [ +  + ]:     745915 :     if (mocktime) return mocktime;
#      30                 :            : 
#      31                 :     648807 :     time_t now = time(nullptr);
#      32                 :     648807 :     assert(now > 0);
#      33                 :          0 :     return now;
#      34                 :     745915 : }
#      35                 :            : 
#      36                 :            : bool ChronoSanityCheck()
#      37                 :        797 : {
#      38                 :            :     // std::chrono::system_clock.time_since_epoch and time_t(0) are not guaranteed
#      39                 :            :     // to use the Unix epoch timestamp, prior to C++20, but in practice they almost
#      40                 :            :     // certainly will. Any differing behavior will be assumed to be an error, unless
#      41                 :            :     // certain platforms prove to consistently deviate, at which point we'll cope
#      42                 :            :     // with it by adding offsets.
#      43                 :            : 
#      44                 :            :     // Create a new clock from time_t(0) and make sure that it represents 0
#      45                 :            :     // seconds from the system_clock's time_since_epoch. Then convert that back
#      46                 :            :     // to a time_t and verify that it's the same as before.
#      47                 :        797 :     const time_t time_t_epoch{};
#      48                 :        797 :     auto clock = std::chrono::system_clock::from_time_t(time_t_epoch);
#      49         [ -  + ]:        797 :     if (std::chrono::duration_cast<std::chrono::seconds>(clock.time_since_epoch()).count() != 0) {
#      50                 :          0 :         return false;
#      51                 :          0 :     }
#      52                 :            : 
#      53                 :        797 :     time_t time_val = std::chrono::system_clock::to_time_t(clock);
#      54         [ -  + ]:        797 :     if (time_val != time_t_epoch) {
#      55                 :          0 :         return false;
#      56                 :          0 :     }
#      57                 :            : 
#      58                 :            :     // Check that the above zero time is actually equal to the known unix timestamp.
#      59                 :        797 :     struct tm epoch;
#      60                 :        797 : #ifdef HAVE_GMTIME_R
#      61         [ -  + ]:        797 :     if (gmtime_r(&time_val, &epoch) == nullptr) {
#      62                 :            : #else
#      63                 :            :     if (gmtime_s(&epoch, &time_val) != 0) {
#      64                 :            : #endif
#      65                 :          0 :         return false;
#      66                 :          0 :     }
#      67                 :            : 
#      68         [ -  + ]:        797 :     if ((epoch.tm_sec != 0)  ||
#      69         [ -  + ]:        797 :        (epoch.tm_min  != 0)  ||
#      70         [ -  + ]:        797 :        (epoch.tm_hour != 0)  ||
#      71         [ -  + ]:        797 :        (epoch.tm_mday != 1)  ||
#      72         [ -  + ]:        797 :        (epoch.tm_mon  != 0)  ||
#      73         [ -  + ]:        797 :        (epoch.tm_year != 70)) {
#      74                 :          0 :         return false;
#      75                 :          0 :     }
#      76                 :        797 :     return true;
#      77                 :        797 : }
#      78                 :            : 
#      79                 :            : template <typename T>
#      80                 :            : T GetTime()
#      81                 :    2061315 : {
#      82                 :    2061315 :     const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)};
#      83                 :            : 
#      84                 :    2061315 :     return std::chrono::duration_cast<T>(
#      85 [ +  + ][ +  + ]:    2061315 :         mocktime.count() ?
#                 [ +  + ]
#      86                 :     214664 :             mocktime :
#      87                 :    2061315 :             std::chrono::microseconds{GetTimeMicros()});
#      88                 :    2061315 : }
#      89                 :            : template std::chrono::seconds GetTime();
#      90                 :            : template std::chrono::milliseconds GetTime();
#      91                 :            : template std::chrono::microseconds GetTime();
#      92                 :            : 
#      93                 :            : template <typename T>
#      94                 :            : static T GetSystemTime()
#      95                 :    5839859 : {
#      96                 :    5839859 :     const auto now = std::chrono::duration_cast<T>(std::chrono::system_clock::now().time_since_epoch());
#      97                 :    5839859 :     assert(now.count() > 0);
#      98                 :          0 :     return now;
#      99                 :    5839859 : }
#     100                 :            : 
#     101                 :            : void SetMockTime(int64_t nMockTimeIn)
#     102                 :       8583 : {
#     103                 :       8583 :     Assert(nMockTimeIn >= 0);
#     104                 :       8583 :     nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
#     105                 :       8583 : }
#     106                 :            : 
#     107                 :            : void SetMockTime(std::chrono::seconds mock_time_in)
#     108                 :        850 : {
#     109                 :        850 :     nMockTime.store(mock_time_in.count(), std::memory_order_relaxed);
#     110                 :        850 : }
#     111                 :            : 
#     112                 :            : std::chrono::seconds GetMockTime()
#     113                 :    2746104 : {
#     114                 :    2746104 :     return std::chrono::seconds(nMockTime.load(std::memory_order_relaxed));
#     115                 :    2746104 : }
#     116                 :            : 
#     117                 :            : int64_t GetTimeMillis()
#     118                 :      13831 : {
#     119                 :      13831 :     return int64_t{GetSystemTime<std::chrono::milliseconds>().count()};
#     120                 :      13831 : }
#     121                 :            : 
#     122                 :            : int64_t GetTimeMicros()
#     123                 :    5826061 : {
#     124                 :    5826061 :     return int64_t{GetSystemTime<std::chrono::microseconds>().count()};
#     125                 :    5826061 : }
#     126                 :            : 
#     127                 :            : int64_t GetTimeSeconds()
#     128                 :          2 : {
#     129                 :          2 :     return int64_t{GetSystemTime<std::chrono::seconds>().count()};
#     130                 :          2 : }
#     131                 :            : 
#     132                 :    3183997 : std::string FormatISO8601DateTime(int64_t nTime) {
#     133                 :    3183997 :     struct tm ts;
#     134                 :    3183997 :     time_t time_val = nTime;
#     135                 :    3183997 : #ifdef HAVE_GMTIME_R
#     136         [ -  + ]:    3183997 :     if (gmtime_r(&time_val, &ts) == nullptr) {
#     137                 :            : #else
#     138                 :            :     if (gmtime_s(&ts, &time_val) != 0) {
#     139                 :            : #endif
#     140                 :          0 :         return {};
#     141                 :          0 :     }
#     142                 :    3183997 :     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);
#     143                 :    3183997 : }
#     144                 :            : 
#     145                 :       1898 : std::string FormatISO8601Date(int64_t nTime) {
#     146                 :       1898 :     struct tm ts;
#     147                 :       1898 :     time_t time_val = nTime;
#     148                 :       1898 : #ifdef HAVE_GMTIME_R
#     149         [ -  + ]:       1898 :     if (gmtime_r(&time_val, &ts) == nullptr) {
#     150                 :            : #else
#     151                 :            :     if (gmtime_s(&ts, &time_val) != 0) {
#     152                 :            : #endif
#     153                 :          0 :         return {};
#     154                 :          0 :     }
#     155                 :       1898 :     return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
#     156                 :       1898 : }
#     157                 :            : 
#     158                 :            : int64_t ParseISO8601DateTime(const std::string& str)
#     159                 :       1725 : {
#     160                 :       1725 :     static const boost::posix_time::ptime epoch = boost::posix_time::from_time_t(0);
#     161                 :       1725 :     static const std::locale loc(std::locale::classic(),
#     162                 :       1725 :         new boost::posix_time::time_input_facet("%Y-%m-%dT%H:%M:%SZ"));
#     163                 :       1725 :     std::istringstream iss(str);
#     164                 :       1725 :     iss.imbue(loc);
#     165                 :       1725 :     boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
#     166                 :       1725 :     iss >> ptime;
#     167 [ +  + ][ +  + ]:       1725 :     if (ptime.is_not_a_date_time() || epoch > ptime)
#     168                 :        861 :         return 0;
#     169                 :        864 :     return (ptime - epoch).total_seconds();
#     170                 :       1725 : }
#     171                 :            : 
#     172                 :            : struct timeval MillisToTimeval(int64_t nTimeout)
#     173                 :        469 : {
#     174                 :        469 :     struct timeval timeout;
#     175                 :        469 :     timeout.tv_sec  = nTimeout / 1000;
#     176                 :        469 :     timeout.tv_usec = (nTimeout % 1000) * 1000;
#     177                 :        469 :     return timeout;
#     178                 :        469 : }
#     179                 :            : 
#     180                 :            : struct timeval MillisToTimeval(std::chrono::milliseconds ms)
#     181                 :        469 : {
#     182                 :        469 :     return MillisToTimeval(count_milliseconds(ms));
#     183                 :        469 : }

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a