Coverage Report

Created: 2026-06-09 19:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/util/time.cpp
Line
Count
Source
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
0
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
static_assert(NodeClock::epoch.time_since_epoch().count() == 0);
37
38
NodeClock::time_point NodeClock::now() noexcept
39
0
{
40
0
    const auto mocktime{g_mock_time.load(std::memory_order_relaxed)};
41
0
    if (!mocktime.count()) {
  Branch (41:9): [True: 0, False: 0]
42
0
        g_used_system_time = true;
43
0
    }
44
0
    const auto ret{
45
0
        mocktime.count() ?
  Branch (45:9): [True: 0, False: 0]
46
0
            mocktime :
47
0
            std::chrono::system_clock::now().time_since_epoch()};
48
0
    assert(ret > 0s);
  Branch (48:5): [True: 0, False: 0]
49
0
    return time_point{ret};
50
0
};
51
52
0
void SetMockTime(int64_t nMockTimeIn) { SetMockTime(std::chrono::seconds{nMockTimeIn}); }
53
0
void SetMockTime(std::chrono::time_point<NodeClock, std::chrono::seconds> mock) { SetMockTime(mock.time_since_epoch()); }
54
void SetMockTime(std::chrono::seconds mock_time_in)
55
1.05k
{
56
1.05k
    Assert(mock_time_in >= 0s);
57
1.05k
    g_mock_time.store(mock_time_in, std::memory_order_relaxed);
58
1.05k
}
59
60
std::chrono::seconds GetMockTime()
61
2.00k
{
62
2.00k
    return g_mock_time.load(std::memory_order_relaxed);
63
2.00k
}
64
65
MockableSteadyClock::time_point MockableSteadyClock::now() noexcept
66
0
{
67
0
    const auto mocktime{g_mock_steady_time.load(std::memory_order_relaxed)};
68
0
    if (!mocktime.count()) {
  Branch (68:9): [True: 0, False: 0]
69
0
        g_used_system_time = true;
70
0
    }
71
0
    const auto ret{
72
0
        mocktime.count() ?
  Branch (72:9): [True: 0, False: 0]
73
0
            mocktime :
74
0
            std::chrono::steady_clock::now().time_since_epoch()};
75
0
    return time_point{ret};
76
0
};
77
78
void MockableSteadyClock::SetMockTime(mock_time_point::duration mock_time_in)
79
0
{
80
0
    Assert(mock_time_in >= 0s);
81
0
    g_mock_steady_time.store(mock_time_in, std::memory_order_relaxed);
82
0
}
83
84
void MockableSteadyClock::ClearMockTime()
85
0
{
86
0
    g_mock_steady_time.store(0ms, std::memory_order_relaxed);
87
0
}
88
89
0
int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
90
91
std::string FormatISO8601DateTime(int64_t nTime)
92
0
{
93
0
    const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
94
0
    const auto days{std::chrono::floor<std::chrono::days>(secs)};
95
0
    const std::chrono::year_month_day ymd{days};
96
0
    const std::chrono::hh_mm_ss hms{secs - days};
97
0
    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());
98
0
}
99
100
std::string FormatISO8601Date(int64_t nTime)
101
0
{
102
0
    const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
103
0
    const auto days{std::chrono::floor<std::chrono::days>(secs)};
104
0
    const std::chrono::year_month_day ymd{days};
105
0
    return strprintf("%04i-%02u-%02u", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()});
106
0
}
107
108
std::optional<int64_t> ParseISO8601DateTime(std::string_view str)
109
0
{
110
0
    constexpr auto FMT_SIZE{std::string_view{"2000-01-01T01:01:01Z"}.size()};
111
0
    if (str.size() != FMT_SIZE || str[4] != '-' || str[7] != '-' || str[10] != 'T' || str[13] != ':' || str[16] != ':' || str[19] != 'Z') {
  Branch (111:9): [True: 0, False: 0]
  Branch (111:35): [True: 0, False: 0]
  Branch (111:52): [True: 0, False: 0]
  Branch (111:69): [True: 0, False: 0]
  Branch (111:87): [True: 0, False: 0]
  Branch (111:105): [True: 0, False: 0]
  Branch (111:123): [True: 0, False: 0]
112
0
        return {};
113
0
    }
114
0
    const auto year{ToIntegral<uint16_t>(str.substr(0, 4))};
115
0
    const auto month{ToIntegral<uint8_t>(str.substr(5, 2))};
116
0
    const auto day{ToIntegral<uint8_t>(str.substr(8, 2))};
117
0
    const auto hour{ToIntegral<uint8_t>(str.substr(11, 2))};
118
0
    const auto min{ToIntegral<uint8_t>(str.substr(14, 2))};
119
0
    const auto sec{ToIntegral<uint8_t>(str.substr(17, 2))};
120
0
    if (!year || !month || !day || !hour || !min || !sec) {
  Branch (120:9): [True: 0, False: 0]
  Branch (120:18): [True: 0, False: 0]
  Branch (120:28): [True: 0, False: 0]
  Branch (120:36): [True: 0, False: 0]
  Branch (120:45): [True: 0, False: 0]
  Branch (120:53): [True: 0, False: 0]
121
0
        return {};
122
0
    }
123
0
    const std::chrono::year_month_day ymd{std::chrono::year{*year}, std::chrono::month{*month}, std::chrono::day{*day}};
124
0
    if (!ymd.ok()) {
  Branch (124:9): [True: 0, False: 0]
125
0
        return {};
126
0
    }
127
0
    const auto time{std::chrono::hours{*hour} + std::chrono::minutes{*min} + std::chrono::seconds{*sec}};
128
0
    const auto tp{std::chrono::sys_days{ymd} + time};
129
0
    return int64_t{TicksSinceEpoch<std::chrono::seconds>(tp)};
130
0
}
131
132
std::string FormatRFC1123DateTime(int64_t time)
133
0
{
134
0
    if (time < -62167219200 || 253402300799 < time) {
  Branch (134:9): [True: 0, False: 0]
  Branch (134:32): [True: 0, False: 0]
135
        // 4-digit year, so only support years 0 to 9999
136
0
        return "";
137
0
    }
138
0
    const std::chrono::sys_seconds secs{std::chrono::seconds{time}};
139
0
    const auto days{std::chrono::floor<std::chrono::days>(secs)};
140
0
    const auto w{days.time_since_epoch().count() % 7}; // will be in the range [-6, 6]
141
0
    std::string_view weekday{weekdays.at(w >= 0 ? w : w + 7)};
  Branch (141:42): [True: 0, False: 0]
142
0
    const std::chrono::year_month_day ymd{days};
143
0
    std::string_view month{months.at(unsigned{ymd.month()} - 1)};
144
0
    const std::chrono::hh_mm_ss hms{secs - days};
145
    // examples: Mon, 27 Jul 2009 12:28:53 GMT
146
    //           Fri, 31 May 2024 19:18:04 GMT
147
0
    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());
148
0
}
149
150
struct timeval MillisToTimeval(int64_t nTimeout)
151
0
{
152
0
    struct timeval timeout;
153
0
    timeout.tv_sec  = nTimeout / 1000;
154
0
    timeout.tv_usec = (nTimeout % 1000) * 1000;
155
0
    return timeout;
156
0
}
157
158
struct timeval MillisToTimeval(std::chrono::milliseconds ms)
159
0
{
160
0
    return MillisToTimeval(count_milliseconds(ms));
161
0
}