Bitcoin Core 22.99.0
P2P Digital Currency
time.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2020 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#ifndef BITCOIN_UTIL_TIME_H
7#define BITCOIN_UTIL_TIME_H
8
9#include <compat.h>
10
11#include <chrono>
12#include <stdint.h>
13#include <string>
14
15using namespace std::chrono_literals;
16
17void UninterruptibleSleep(const std::chrono::microseconds& n);
18
29constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); }
30constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); }
31constexpr int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); }
32
33using SecondsDouble = std::chrono::duration<double, std::chrono::seconds::period>;
34
38inline double CountSecondsDouble(SecondsDouble t) { return t.count(); }
39
44int64_t GetTime();
45
47int64_t GetTimeMillis();
49int64_t GetTimeMicros();
51int64_t GetTimeSeconds(); // Like GetTime(), but not mockable
52
59void SetMockTime(int64_t nMockTimeIn);
60
62void SetMockTime(std::chrono::seconds mock_time_in);
63
65std::chrono::seconds GetMockTime();
66
68template <typename T>
69T GetTime();
70
75std::string FormatISO8601DateTime(int64_t nTime);
76std::string FormatISO8601Date(int64_t nTime);
77int64_t ParseISO8601DateTime(const std::string& str);
78
82struct timeval MillisToTimeval(int64_t nTimeout);
83
87struct timeval MillisToTimeval(std::chrono::milliseconds ms);
88
91
92#endif // BITCOIN_UTIL_TIME_H
#define T(expected, seed, data)
constexpr int64_t count_milliseconds(std::chrono::milliseconds t)
Definition: time.h:30
struct timeval MillisToTimeval(int64_t nTimeout)
Convert milliseconds to a struct timeval for e.g.
Definition: time.cpp:172
int64_t GetTimeMicros()
Returns the system time (not mockable)
Definition: time.cpp:122
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: time.cpp:117
std::chrono::duration< double, std::chrono::seconds::period > SecondsDouble
Definition: time.h:33
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:22
std::chrono::seconds GetMockTime()
For testing.
Definition: time.cpp:112
int64_t GetTime()
DEPRECATED Use either GetTimeSeconds (not mockable) or GetTime<T> (mockable)
Definition: time.cpp:26
int64_t GetTimeSeconds()
Returns the system time (not mockable)
Definition: time.cpp:127
std::string FormatISO8601Date(int64_t nTime)
Definition: time.cpp:145
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:101
constexpr int64_t count_microseconds(std::chrono::microseconds t)
Definition: time.h:31
constexpr int64_t count_seconds(std::chrono::seconds t)
Helper to count the seconds of a duration.
Definition: time.h:29
int64_t ParseISO8601DateTime(const std::string &str)
Definition: time.cpp:158
double CountSecondsDouble(SecondsDouble t)
Helper to count the seconds in any std::chrono::duration type.
Definition: time.h:38
bool ChronoSanityCheck()
Sanity check epoch match normal Unix epoch.
Definition: time.cpp:36
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: time.cpp:132