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#ifndef BITCOIN_UTIL_TIME_H7#define BITCOIN_UTIL_TIME_H9// The `util/time.h` header is designed to be a drop-in replacement for `chrono`.10#include <chrono> // IWYU pragma: export11#include <cstdint>12#include <ctime>13#include <optional>14#include <string>15#include <string_view>17#ifdef WIN3218#include <winsock2.h>19#else20#include <sys/time.h>21#endif23using namespace std::chrono_literals;25/// Version of the system clock that is mockable in the context of tests (via26/// FakeNodeClock or ::SetMockTime), otherwise the system clock.27struct NodeClock : public std::chrono::system_clock {28using time_point = std::chrono::time_point<NodeClock>;29/** Return current system time or mocked time, if set */30static time_point now() noexcept;31static std::time_t to_time_t(const time_point&) = delete; // unused32static time_point from_time_t(std::time_t) = delete; // unused33static constexpr time_point epoch{};34};35using NodeSeconds = std::chrono::time_point<NodeClock, std::chrono::seconds>;37using SteadyClock = std::chrono::steady_clock;38using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>;39using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>;40using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>;42using SystemClock = std::chrono::system_clock;44/// Version of SteadyClock that is mockable in the context of tests (via45/// FakeSteadyClock, or Self::SetMockTime), otherwise the system steady46/// clock.47struct MockableSteadyClock : public std::chrono::steady_clock {48using time_point = std::chrono::time_point<MockableSteadyClock>;50using mock_time_point = std::chrono::time_point<MockableSteadyClock, std::chrono::milliseconds>;51static constexpr mock_time_point::duration INITIAL_MOCK_TIME{1};53/** Return current system time or mocked time, if set */54static time_point now() noexcept;55static std::time_t to_time_t(const time_point&) = delete; // unused56static time_point from_time_t(std::time_t) = delete; // unused58/** Set mock time for testing.59* When mocking the steady clock, start at INITIAL_MOCK_TIME and add durations to elapse time as necessary60* for testing.61* To stop mocking, call ClearMockTime().62*/63static void SetMockTime(mock_time_point::duration mock_time_in);65/** Clear mock time, go back to system steady clock. */66static void ClearMockTime();67};69void UninterruptibleSleep(const std::chrono::microseconds& n);71/**72* Helper to count the seconds of a duration/time_point.73*74* All durations/time_points should be using std::chrono and calling this should generally75* be avoided in code. Though, it is still preferred to an inline t.count() to76* protect against a reliance on the exact type of t.77*78* This helper is used to convert durations/time_points before passing them over an79* interface that doesn't support std::chrono (e.g. RPC, debug log, or the GUI)80*/81template <typename Dur1, typename Dur2>82constexpr auto Ticks(Dur2 d)83{84return std::chrono::duration_cast<Dur1>(d).count();85}87template <typename Duration>88constexpr int64_t TicksSeconds(Duration d)89{90return int64_t{Ticks<std::chrono::seconds>(d)};91}92template <typename Duration, typename Timepoint>93constexpr auto TicksSinceEpoch(Timepoint t)94{95return Ticks<Duration>(t.time_since_epoch());96}97constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); }98constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); }99constexpr int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); }101using HoursDouble = std::chrono::duration<double, std::chrono::hours::period>;102using SecondsDouble = std::chrono::duration<double, std::chrono::seconds::period>;103using MillisecondsDouble = std::chrono::duration<double, std::chrono::milliseconds::period>;105/**106* DEPRECATED107* Use either ClockType::now() or Now<TimePointType>() if a cast is needed.108* ClockType is109* - SteadyClock/std::chrono::steady_clock for steady time110* - SystemClock/std::chrono::system_clock for system time111* - NodeClock for mockable system time112*/113int64_t GetTime();115/** For testing. Set e.g. with the setmocktime rpc, or -mocktime argument */116void SetMockTime(std::chrono::seconds mock_time_in);117void SetMockTime(std::chrono::time_point<NodeClock, std::chrono::seconds> mock);119/** For testing */120std::chrono::seconds GetMockTime();122/**123* Return the current time point cast to the given precision. Only use this124* when an exact precision is needed, otherwise use T::clock::now() directly.125*/126template <typename T>127T Now()128{129return std::chrono::time_point_cast<typename T::duration>(T::clock::now());130}131/** DEPRECATED, see GetTime */132template <typename T>133T GetTime()134{135return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch();136}138/**139* ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date}140* helper functions if possible.141*/142std::string FormatISO8601DateTime(int64_t nTime);143std::string FormatISO8601Date(int64_t nTime);144std::optional<int64_t> ParseISO8601DateTime(std::string_view str);146/**147* RFC1123 formatting https://www.rfc-editor.org/rfc/rfc1123#section-5.2.14148* Used in HTTP/1.1 responses149*/150std::string FormatRFC1123DateTime(int64_t nTime);152/**153* Convert milliseconds to a struct timeval for e.g. select.154*/155struct timeval MillisToTimeval(int64_t nTimeout);157/**158* Convert milliseconds to a struct timeval for e.g. select.159*/160struct timeval MillisToTimeval(std::chrono::milliseconds ms);162#endif // BITCOIN_UTIL_TIME_H
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.