8 #ifndef XCLOX_TIMESTAMP_HPP
9 #define XCLOX_TIMESTAMP_HPP
18 constexpr std::chrono::seconds EpochDeltaSeconds { 0x83AA7E80 };
69 explicit Timestamp(std::chrono::system_clock::time_point timePoint)
70 : m_data(durationToValue(timePoint.time_since_epoch() + internal::EpochDeltaSeconds))
77 return static_cast<uint32_t
>(m_data >> 32);
83 return static_cast<uint32_t
>(m_data);
93 std::chrono::system_clock::duration
duration()
const
95 return std::chrono::seconds(
seconds()) + std::chrono::system_clock::duration(
static_cast<int>(DurationRange *
fraction() / FractionRange));
101 return m_data == other.m_data;
117 static constexpr
auto FractionRange { std::numeric_limits<uint32_t>::max() + 1.0 };
118 static constexpr
auto DurationRange {
static_cast<double>(std::chrono::system_clock::period::den) };
119 static constexpr uint64_t durationToValue(
const std::chrono::system_clock::duration&
duration)
121 return static_cast<uint64_t
>(std::chrono::duration_cast<std::chrono::seconds>(
duration).count()) << 32
122 |
static_cast<uint32_t
>(FractionRange *
static_cast<double>((
duration % std::chrono::seconds(1)).count()) / DurationRange);
Timestamp is an immutable class representing a NTP timestamp.
Definition: timestamp.hpp:45
uint32_t seconds() const
Returns the number of seconds of the NTP timestamp.
Definition: timestamp.hpp:75
Timestamp(std::chrono::system_clock::time_point timePoint)
Definition: timestamp.hpp:69
std::chrono::system_clock::duration operator-(const Timestamp &other) const
Returns the result of subtracting other from this timestamp as a system time duration.
Definition: timestamp.hpp:111
uint64_t value() const
Returns the NTP timestamp in long format.
Definition: timestamp.hpp:87
std::chrono::system_clock::duration duration() const
Returns the NTP timestamp as a system time duration.
Definition: timestamp.hpp:93
Timestamp(std::chrono::system_clock::duration duration)
Definition: timestamp.hpp:63
bool operator!=(const Timestamp &other) const
Inequality operator.
Definition: timestamp.hpp:105
bool operator==(const Timestamp &other) const
Equality operator.
Definition: timestamp.hpp:99
uint32_t fraction() const
Returns the fraction of a second of the NTP timestamp.
Definition: timestamp.hpp:81
Timestamp(uint32_t seconds, uint32_t fraction)
Definition: timestamp.hpp:57
Timestamp(uint64_t value=0)
Definition: timestamp.hpp:48