Xclox
C++11 header-only cross-platform date and time library with an asynchronous NTP client
timestamp.hpp
1 /*
2  * Copyright (c) 2024 Abdullatif Kalla.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE.txt file in the root directory of this source tree.
6  */
7 
8 #ifndef XCLOX_TIMESTAMP_HPP
9 #define XCLOX_TIMESTAMP_HPP
10 
11 #include <chrono>
12 
13 namespace xclox {
14 
15 namespace ntp {
16 
17  namespace internal {
18  constexpr std::chrono::seconds EpochDeltaSeconds { 0x83AA7E80 };
19  }
20 
45  class Timestamp {
46  public:
48  explicit Timestamp(uint64_t value = 0)
49  : m_data(value)
50  {
51  }
52 
57  explicit Timestamp(uint32_t seconds, uint32_t fraction)
58  : m_data((static_cast<uint64_t>(seconds) << 32) | fraction)
59  {
60  }
61 
63  explicit Timestamp(std::chrono::system_clock::duration duration)
64  : m_data(durationToValue(duration))
65  {
66  }
67 
69  explicit Timestamp(std::chrono::system_clock::time_point timePoint)
70  : m_data(durationToValue(timePoint.time_since_epoch() + internal::EpochDeltaSeconds))
71  {
72  }
73 
75  uint32_t seconds() const
76  {
77  return static_cast<uint32_t>(m_data >> 32);
78  }
79 
81  uint32_t fraction() const
82  {
83  return static_cast<uint32_t>(m_data);
84  }
85 
87  uint64_t value() const
88  {
89  return m_data;
90  }
91 
93  std::chrono::system_clock::duration duration() const
94  {
95  return std::chrono::seconds(seconds()) + std::chrono::system_clock::duration(static_cast<int>(DurationRange * fraction() / FractionRange));
96  }
97 
99  bool operator==(const Timestamp& other) const
100  {
101  return m_data == other.m_data;
102  }
103 
105  bool operator!=(const Timestamp& other) const
106  {
107  return !operator==(other);
108  }
109 
111  std::chrono::system_clock::duration operator-(const Timestamp& other) const
112  {
113  return duration() - other.duration();
114  }
115 
116  private:
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)
120  {
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);
123  }
124 
125  uint64_t m_data;
126  };
127 
128 } // namespace ntp
129 
130 } // namespace xclox
131 
132 #endif // XCLOX_TIMESTAMP_HPP
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