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{};
23std::atomic<bool> g_used_system_time{false};
24
26{
27 const auto mocktime{g_mock_time.load(std::memory_order_relaxed)};
28 if (!mocktime.count()) {
29 g_used_system_time = true;
30 }
31 const auto ret{
32 mocktime.count() ?
33 mocktime :
34 std::chrono::system_clock::now().time_since_epoch()};
35 assert(ret > 0s);
36 return time_point{ret};
37};
38
39void SetMockTime(int64_t nMockTimeIn) { SetMockTime(std::chrono::seconds{nMockTimeIn}); }
40void SetMockTime(std::chrono::seconds mock_time_in)
41{
42 Assert(mock_time_in >= 0s);
43 g_mock_time.store(mock_time_in, std::memory_order_relaxed);
44}
45
46std::chrono::seconds GetMockTime()
47{
48 return g_mock_time.load(std::memory_order_relaxed);
49}
50
51int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
52
53std::string FormatISO8601DateTime(int64_t nTime)
54{
55 const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
56 const auto days{std::chrono::floor<std::chrono::days>(secs)};
57 const std::chrono::year_month_day ymd{days};
58 const std::chrono::hh_mm_ss hms{secs - days};
59 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());
60}
61
62std::string FormatISO8601Date(int64_t nTime)
63{
64 const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
65 const auto days{std::chrono::floor<std::chrono::days>(secs)};
66 const std::chrono::year_month_day ymd{days};
67 return strprintf("%04i-%02u-%02u", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()});
68}
69
70std::optional<int64_t> ParseISO8601DateTime(std::string_view str)
71{
72 constexpr auto FMT_SIZE{std::string_view{"2000-01-01T01:01:01Z"}.size()};
73 if (str.size() != FMT_SIZE || str[4] != '-' || str[7] != '-' || str[10] != 'T' || str[13] != ':' || str[16] != ':' || str[19] != 'Z') {
74 return {};
75 }
76 const auto year{ToIntegral<uint16_t>(str.substr(0, 4))};
77 const auto month{ToIntegral<uint8_t>(str.substr(5, 2))};
78 const auto day{ToIntegral<uint8_t>(str.substr(8, 2))};
79 const auto hour{ToIntegral<uint8_t>(str.substr(11, 2))};
80 const auto min{ToIntegral<uint8_t>(str.substr(14, 2))};
81 const auto sec{ToIntegral<uint8_t>(str.substr(17, 2))};
82 if (!year || !month || !day || !hour || !min || !sec) {
83 return {};
84 }
85 const std::chrono::year_month_day ymd{std::chrono::year{*year}, std::chrono::month{*month}, std::chrono::day{*day}};
86 if (!ymd.ok()) {
87 return {};
88 }
89 const auto time{std::chrono::hours{*hour} + std::chrono::minutes{*min} + std::chrono::seconds{*sec}};
90 const auto tp{std::chrono::sys_days{ymd} + time};
91 return int64_t{TicksSinceEpoch<std::chrono::seconds>(tp)};
92}
93
94struct timeval MillisToTimeval(int64_t nTimeout)
95{
96 struct timeval timeout;
97 timeout.tv_sec = nTimeout / 1000;
98 timeout.tv_usec = (nTimeout % 1000) * 1000;
99 return timeout;
100}
101
102struct timeval MillisToTimeval(std::chrono::milliseconds ms)
103{
105}
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:25
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:94
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:20
std::chrono::seconds GetMockTime()
For testing.
Definition: time.cpp:46
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition: time.cpp:51
std::string FormatISO8601Date(int64_t nTime)
Definition: time.cpp:62
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:39
std::optional< int64_t > ParseISO8601DateTime(std::string_view str)
Definition: time.cpp:70
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:53
std::atomic< bool > g_used_system_time
Definition: time.cpp:23
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:1172
assert(!tx.IsCoinBase())