Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#include <util/time.h>8#include <tinyformat.h>9#include <util/check.h>10#include <util/strencodings.h>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>21#ifdef WIN3222#include <winsock2.h>23#else24#include <sys/time.h>25#endif27static constexpr std::array<std::string_view, 7> weekdays{"Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed"}; // 1970-01-01 was a Thursday.28static constexpr std::array<std::string_view, 12> months{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};30void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }32static std::atomic<std::chrono::seconds> g_mock_time{}; //!< For testing33std::atomic<bool> g_used_system_time{false};34static std::atomic<MockableSteadyClock::mock_time_point::duration> g_mock_steady_time{}; //!< For testing36static_assert(NodeClock::epoch.time_since_epoch().count() == 0);38NodeClock::time_point NodeClock::now() noexcept39{40const auto mocktime{g_mock_time.load(std::memory_order_relaxed)};41if (!mocktime.count()) {42g_used_system_time = true;43}44const auto ret{45mocktime.count() ?46mocktime :47std::chrono::system_clock::now().time_since_epoch()};48assert(ret > 0s);49return time_point{ret};50};52void SetMockTime(std::chrono::time_point<NodeClock, std::chrono::seconds> mock) { SetMockTime(mock.time_since_epoch()); }53void SetMockTime(std::chrono::seconds mock_time_in)54{55Assert(mock_time_in >= 0s);56g_mock_time.store(mock_time_in, std::memory_order_relaxed);57}59std::chrono::seconds GetMockTime()60{61return g_mock_time.load(std::memory_order_relaxed);62}64MockableSteadyClock::time_point MockableSteadyClock::now() noexcept65{66const auto mocktime{g_mock_steady_time.load(std::memory_order_relaxed)};67if (!mocktime.count()) {68g_used_system_time = true;69}70const auto ret{71mocktime.count() ?72mocktime :73std::chrono::steady_clock::now().time_since_epoch()};74return time_point{ret};75};77void MockableSteadyClock::SetMockTime(mock_time_point::duration mock_time_in)78{79Assert(mock_time_in >= 0s);80g_mock_steady_time.store(mock_time_in, std::memory_order_relaxed);81}83void MockableSteadyClock::ClearMockTime()84{85g_mock_steady_time.store(0ms, std::memory_order_relaxed);86}88int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }90std::string FormatISO8601DateTime(int64_t nTime)91{92const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};93const auto days{std::chrono::floor<std::chrono::days>(secs)};94const std::chrono::year_month_day ymd{days};95const std::chrono::hh_mm_ss hms{secs - days};96return 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());97}99std::string FormatISO8601Date(int64_t nTime)100{101const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};102const auto days{std::chrono::floor<std::chrono::days>(secs)};103const std::chrono::year_month_day ymd{days};104return strprintf("%04i-%02u-%02u", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()});105}107std::optional<int64_t> ParseISO8601DateTime(std::string_view str)108{109constexpr auto FMT_SIZE{std::string_view{"2000-01-01T01:01:01Z"}.size()};110if (str.size() != FMT_SIZE || str[4] != '-' || str[7] != '-' || str[10] != 'T' || str[13] != ':' || str[16] != ':' || str[19] != 'Z') {111return {};112}113const auto year{ToIntegral<uint16_t>(str.substr(0, 4))};114const auto month{ToIntegral<uint8_t>(str.substr(5, 2))};115const auto day{ToIntegral<uint8_t>(str.substr(8, 2))};116const auto hour{ToIntegral<uint8_t>(str.substr(11, 2))};117const auto min{ToIntegral<uint8_t>(str.substr(14, 2))};118const auto sec{ToIntegral<uint8_t>(str.substr(17, 2))};119if (!year || !month || !day || !hour || !min || !sec) {120return {};121}122const std::chrono::year_month_day ymd{std::chrono::year{*year}, std::chrono::month{*month}, std::chrono::day{*day}};123if (!ymd.ok()) {124return {};125}126const auto time{std::chrono::hours{*hour} + std::chrono::minutes{*min} + std::chrono::seconds{*sec}};127const auto tp{std::chrono::sys_days{ymd} + time};128return int64_t{TicksSinceEpoch<std::chrono::seconds>(tp)};129}131std::string FormatRFC1123DateTime(int64_t time)132{133if (time < -62167219200 || 253402300799 < time) {134// 4-digit year, so only support years 0 to 9999135return "";136}137const std::chrono::sys_seconds secs{std::chrono::seconds{time}};138const auto days{std::chrono::floor<std::chrono::days>(secs)};139const auto w{days.time_since_epoch().count() % 7}; // will be in the range [-6, 6]140std::string_view weekday{weekdays.at(w >= 0 ? w : w + 7)};141const std::chrono::year_month_day ymd{days};142std::string_view month{months.at(unsigned{ymd.month()} - 1)};143const std::chrono::hh_mm_ss hms{secs - days};144// examples: Mon, 27 Jul 2009 12:28:53 GMT145// Fri, 31 May 2024 19:18:04 GMT146return 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());147}149struct timeval MillisToTimeval(int64_t nTimeout)150{151struct timeval timeout;152timeout.tv_sec = nTimeout / 1000;153timeout.tv_usec = (nTimeout % 1000) * 1000;154return timeout;155}157struct timeval MillisToTimeval(std::chrono::milliseconds ms)158{159return MillisToTimeval(count_milliseconds(ms));160}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.