Bitcoin Core 28.99.0
P2P Digital Currency
time.cpp
Go to the documentation of this file.
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 <compat/compat.h>
9#include <tinyformat.h>
10#include <util/check.h>
11#include <util/strencodings.h>
12
13#include <atomic>
14#include <chrono>
15#include <optional>
16#include <string>
17#include <string_view>
18#include <thread>
19
20void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
21
22static std::atomic<std::chrono::seconds> g_mock_time{};
23
25{
26 const auto mocktime{g_mock_time.load(std::memory_order_relaxed)};
27 const auto ret{
28 mocktime.count() ?
29 mocktime :
30 std::chrono::system_clock::now().time_since_epoch()};
31 assert(ret > 0s);
32 return time_point{ret};
33};
34
35void SetMockTime(int64_t nMockTimeIn) { SetMockTime(std::chrono::seconds{nMockTimeIn}); }
36void SetMockTime(std::chrono::seconds mock_time_in)
37{
38 Assert(mock_time_in >= 0s);
39 g_mock_time.store(mock_time_in, std::memory_order_relaxed);
40}
41
42std::chrono::seconds GetMockTime()
43{
44 return g_mock_time.load(std::memory_order_relaxed);
45}
46
47int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
48
49std::string FormatISO8601DateTime(int64_t nTime)
50{
51 const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
52 const auto days{std::chrono::floor<std::chrono::days>(secs)};
53 const std::chrono::year_month_day ymd{days};
54 const std::chrono::hh_mm_ss hms{secs - days};
55 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());
56}
57
58std::string FormatISO8601Date(int64_t nTime)
59{
60 const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
61 const auto days{std::chrono::floor<std::chrono::days>(secs)};
62 const std::chrono::year_month_day ymd{days};
63 return strprintf("%04i-%02u-%02u", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()});
64}
65
66std::optional<int64_t> ParseISO8601DateTime(std::string_view str)
67{
68 constexpr auto FMT_SIZE{std::string_view{"2000-01-01T01:01:01Z"}.size()};
69 if (str.size() != FMT_SIZE || str[4] != '-' || str[7] != '-' || str[10] != 'T' || str[13] != ':' || str[16] != ':' || str[19] != 'Z') {
70 return {};
71 }
72 const auto year{ToIntegral<uint16_t>(str.substr(0, 4))};
73 const auto month{ToIntegral<uint8_t>(str.substr(5, 2))};
74 const auto day{ToIntegral<uint8_t>(str.substr(8, 2))};
75 const auto hour{ToIntegral<uint8_t>(str.substr(11, 2))};
76 const auto min{ToIntegral<uint8_t>(str.substr(14, 2))};
77 const auto sec{ToIntegral<uint8_t>(str.substr(17, 2))};
78 if (!year || !month || !day || !hour || !min || !sec) {
79 return {};
80 }
81 const std::chrono::year_month_day ymd{std::chrono::year{*year}, std::chrono::month{*month}, std::chrono::day{*day}};
82 if (!ymd.ok()) {
83 return {};
84 }
85 const auto time{std::chrono::hours{*hour} + std::chrono::minutes{*min} + std::chrono::seconds{*sec}};
86 const auto tp{std::chrono::sys_days{ymd} + time};
87 return int64_t{TicksSinceEpoch<std::chrono::seconds>(tp)};
88}
89
90struct timeval MillisToTimeval(int64_t nTimeout)
91{
92 struct timeval timeout;
93 timeout.tv_sec = nTimeout / 1000;
94 timeout.tv_usec = (nTimeout % 1000) * 1000;
95 return timeout;
96}
97
98struct timeval MillisToTimeval(std::chrono::milliseconds ms)
99{
101}
int ret
#define Assert(val)
Identity function.
Definition: check.h:85
static time_point now() noexcept
Return current system time or mocked time, if set.
Definition: time.cpp:24
std::chrono::time_point< NodeClock > time_point
Definition: time.h:19
static int count
struct timeval MillisToTimeval(int64_t nTimeout)
Convert milliseconds to a struct timeval for e.g.
Definition: time.cpp:90
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:20
std::chrono::seconds GetMockTime()
For testing.
Definition: time.cpp:42
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition: time.cpp:47
std::string FormatISO8601Date(int64_t nTime)
Definition: time.cpp:58
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:35
std::optional< int64_t > ParseISO8601DateTime(std::string_view str)
Definition: time.cpp:66
static std::atomic< std::chrono::seconds > g_mock_time
For testing.
Definition: time.cpp:22
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: time.cpp:49
constexpr int64_t count_milliseconds(std::chrono::milliseconds t)
Definition: time.h:57
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1165
assert(!tx.IsCoinBase())