9 #ifndef CPPCOMMON_TIME_TIMESTAMP_H
10 #define CPPCOMMON_TIME_TIMESTAMP_H
43 template <
class Clock,
class Duration>
44 explicit Timestamp(
const std::chrono::time_point<Clock, Duration>& time_point) noexcept :
_timestamp(std::chrono::duration_cast<std::chrono::nanoseconds>(time_point.time_since_epoch()).count()) {}
58 {
_timestamp += offset.total();
return *
this; }
63 {
_timestamp -= offset.total();
return *
this; }
66 {
return Timestamp(timestamp.total() + offset); }
68 {
return Timestamp(offset + timestamp.total()); }
70 {
return Timestamp(timestamp.total() + offset.total()); }
72 {
return Timestamp(offset.total() + timestamp.total()); }
75 {
return Timestamp(timestamp.total() - offset); }
77 {
return Timestamp(offset - timestamp.total()); }
79 {
return Timestamp(timestamp.total() - offset.total()); }
81 {
return Timestamp(offset.total() - timestamp.total()); }
84 {
return Timespan(timestamp1.total() - timestamp2.total()); }
88 {
return timestamp1.
total() == timestamp2; }
90 {
return timestamp1 == timestamp2.
total(); }
92 {
return timestamp1.
total() == timestamp2.total(); }
95 {
return timestamp1.
total() != timestamp2; }
97 {
return timestamp1 != timestamp2.
total(); }
99 {
return timestamp1.
total() != timestamp2.total(); }
102 {
return timestamp1.
total() > timestamp2; }
104 {
return timestamp1 > timestamp2.
total(); }
106 {
return timestamp1.
total() > timestamp2.total(); }
109 {
return timestamp1.
total() < timestamp2; }
111 {
return timestamp1 < timestamp2.
total(); }
113 {
return timestamp1.
total() < timestamp2.total(); }
116 {
return timestamp1.
total() >= timestamp2; }
118 {
return timestamp1 >= timestamp2.
total(); }
120 {
return timestamp1.
total() >= timestamp2.total(); }
123 {
return timestamp1.
total() <= timestamp2; }
125 {
return timestamp1 <= timestamp2.
total(); }
127 {
return timestamp1.
total() <= timestamp2.total(); }
130 std::chrono::system_clock::time_point
chrono() const noexcept
131 {
return std::chrono::time_point_cast<std::chrono::system_clock::duration>((std::chrono::time_point<std::chrono::system_clock>() + std::chrono::nanoseconds(
_timestamp))); }
135 {
return _timestamp / (24 * 60 * 60 * 1000000000ull); }
138 {
return _timestamp / (60 * 60 * 1000000000ull); }
186 static uint64_t
epoch() noexcept {
return 0; }
194 static uint64_t
utc();
202 static uint64_t
local();
210 static uint64_t
nano();
223 static uint64_t
rdts();
EpochTimestamp(const Timestamp ×tamp)
Initialize epoch timestamp with another timestamp value.
EpochTimestamp()
Initialize epoch timestamp.
LocalTimestamp()
Initialize local timestamp with a current local time.
LocalTimestamp(const Timestamp ×tamp)
Initialize local timestamp with another timestamp value.
High resolution timestamp.
NanoTimestamp(const Timestamp ×tamp)
Initialize high resolution timestamp with another timestamp value.
NanoTimestamp()
Initialize high resolution timestamp with a current high resolution time.
RdtsTimestamp()
Initialize RDTS timestamp with a current RDTS time.
RdtsTimestamp(const Timestamp ×tamp)
Initialize RDTS timestamp with another timestamp value.
uint64_t minutes() const noexcept
Get total minutes of the current timestamp.
friend Timestamp operator-(int64_t offset, const Timestamp ×tamp) noexcept
Timestamp & operator-=(const Timespan &offset) noexcept
static uint64_t local()
Get the local timestamp.
friend Timestamp operator-(const Timespan &offset, const Timestamp ×tamp) noexcept
void swap(Timestamp ×tamp) noexcept
Swap two instances.
Timestamp(const Timestamp &) noexcept=default
friend bool operator<=(const Timestamp ×tamp1, uint64_t timestamp2) noexcept
friend bool operator<=(const Timestamp ×tamp1, const Timestamp ×tamp2) noexcept
Timestamp & operator-=(int64_t offset) noexcept
friend bool operator>(uint64_t timestamp1, const Timestamp ×tamp2) noexcept
static Timestamp microseconds(int64_t microseconds) noexcept
Create the timestamp based on the given microseconds value.
friend Timestamp operator-(const Timestamp ×tamp, const Timespan &offset) noexcept
friend bool operator==(uint64_t timestamp1, const Timestamp ×tamp2) noexcept
friend bool operator==(const Timestamp ×tamp1, const Timestamp ×tamp2) noexcept
std::chrono::system_clock::time_point chrono() const noexcept
Convert timestamp to the std::chrono time point.
uint64_t _timestamp
Timestamp value.
friend bool operator>=(uint64_t timestamp1, const Timestamp ×tamp2) noexcept
Timestamp & operator=(Timestamp &&) noexcept=default
friend Timestamp operator+(const Timestamp ×tamp, const Timespan &offset) noexcept
Timestamp & operator=(const Timestamp &) noexcept=default
friend Timestamp operator-(const Timestamp ×tamp, int64_t offset) noexcept
static Timestamp minutes(int64_t minutes) noexcept
Create the timestamp based on the given minutes value.
friend bool operator!=(const Timestamp ×tamp1, const Timestamp ×tamp2) noexcept
friend bool operator<=(uint64_t timestamp1, const Timestamp ×tamp2) noexcept
friend Timestamp operator+(const Timestamp ×tamp, int64_t offset) noexcept
static Timestamp days(int64_t days) noexcept
Create the timestamp based on the given days value.
uint64_t milliseconds() const noexcept
Get total milliseconds of the current timestamp.
friend bool operator<(const Timestamp ×tamp1, uint64_t timestamp2) noexcept
Timestamp & operator+=(const Timespan &offset) noexcept
static uint64_t utc()
Get the UTC timestamp.
uint64_t microseconds() const noexcept
Get total microseconds of the current timestamp.
static uint64_t nano()
Get the high resolution timestamp.
static Timestamp hours(int64_t hours) noexcept
Create the timestamp based on the given hours value.
uint64_t seconds() const noexcept
Get total seconds of the current timestamp.
friend Timestamp operator+(const Timespan &offset, const Timestamp ×tamp) noexcept
friend bool operator>=(const Timestamp ×tamp1, const Timestamp ×tamp2) noexcept
friend bool operator>(const Timestamp ×tamp1, const Timestamp ×tamp2) noexcept
static Timestamp milliseconds(int64_t milliseconds) noexcept
Create the timestamp based on the given milliseconds value.
static uint64_t rdts()
Get the current value of RDTS (Read Time Stamp Counter)
friend bool operator!=(uint64_t timestamp1, const Timestamp ×tamp2) noexcept
Timestamp(const std::chrono::time_point< Clock, Duration > &time_point) noexcept
Initialize timestamp with a given std::chrono time point.
Timestamp(uint64_t timestamp) noexcept
Initialize timestamp with a given time moment in nanoseconds.
uint64_t hours() const noexcept
Get total hours of the current timestamp.
static Timestamp seconds(int64_t seconds) noexcept
Create the timestamp based on the given seconds value.
uint64_t total() const noexcept
Get total value of the current timestamp (total nanoseconds)
static uint64_t epoch() noexcept
Get the epoch timestamp.
friend bool operator<(uint64_t timestamp1, const Timestamp ×tamp2) noexcept
friend bool operator>=(const Timestamp ×tamp1, uint64_t timestamp2) noexcept
uint64_t days() const noexcept
Get total days of the current timestamp.
friend bool operator<(const Timestamp ×tamp1, const Timestamp ×tamp2) noexcept
friend Timestamp operator+(int64_t offset, const Timestamp ×tamp) noexcept
friend bool operator!=(const Timestamp ×tamp1, uint64_t timestamp2) noexcept
friend Timespan operator-(const Timestamp ×tamp1, const Timestamp ×tamp2) noexcept
Timestamp() noexcept
Initialize timestamp with an epoch time.
Timestamp(Timestamp &&) noexcept=default
friend bool operator>(const Timestamp ×tamp1, uint64_t timestamp2) noexcept
friend bool operator==(const Timestamp ×tamp1, uint64_t timestamp2) noexcept
static Timestamp nanoseconds(int64_t nanoseconds) noexcept
Create the timestamp based on the given nanoseconds value.
uint64_t nanoseconds() const noexcept
Get total nanoseconds of the current timestamp.
UtcTimestamp(const Timestamp ×tamp)
Initialize UTC timestamp with another timestamp value.
UtcTimestamp()
Initialize UTC timestamp with a current UTC time.
C++ Common project definitions.
Timestamp inline implementation.