CppCommon  1.0.4.1
C++ Common Library
timestamp.h
Go to the documentation of this file.
1 
9 #ifndef CPPCOMMON_TIME_TIMESTAMP_H
10 #define CPPCOMMON_TIME_TIMESTAMP_H
11 
12 #include "time/timespan.h"
13 
14 namespace CppCommon {
15 
17 
29 class Timestamp
30 {
31 public:
33  Timestamp() noexcept : _timestamp(epoch()) {}
35 
38  explicit Timestamp(uint64_t timestamp) noexcept : _timestamp(timestamp) {}
40 
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()) {}
45  Timestamp(const Timestamp&) noexcept = default;
46  Timestamp(Timestamp&&) noexcept = default;
47  ~Timestamp() noexcept = default;
48 
49  Timestamp& operator=(uint64_t timestamp) noexcept
50  { _timestamp = timestamp; return *this; }
51  Timestamp& operator=(const Timestamp&) noexcept = default;
52  Timestamp& operator=(Timestamp&&) noexcept = default;
53 
54  // Timestamp offset operations
55  Timestamp& operator+=(int64_t offset) noexcept
56  { _timestamp += offset; return *this; }
57  Timestamp& operator+=(const Timespan& offset) noexcept
58  { _timestamp += offset.total(); return *this; }
59 
60  Timestamp& operator-=(int64_t offset) noexcept
61  { _timestamp -= offset; return *this; }
62  Timestamp& operator-=(const Timespan& offset) noexcept
63  { _timestamp -= offset.total(); return *this; }
64 
65  friend Timestamp operator+(const Timestamp& timestamp, int64_t offset) noexcept
66  { return Timestamp(timestamp.total() + offset); }
67  friend Timestamp operator+(int64_t offset, const Timestamp& timestamp) noexcept
68  { return Timestamp(offset + timestamp.total()); }
69  friend Timestamp operator+(const Timestamp& timestamp, const Timespan& offset) noexcept
70  { return Timestamp(timestamp.total() + offset.total()); }
71  friend Timestamp operator+(const Timespan& offset, const Timestamp& timestamp) noexcept
72  { return Timestamp(offset.total() + timestamp.total()); }
73 
74  friend Timestamp operator-(const Timestamp& timestamp, int64_t offset) noexcept
75  { return Timestamp(timestamp.total() - offset); }
76  friend Timestamp operator-(int64_t offset, const Timestamp& timestamp) noexcept
77  { return Timestamp(offset - timestamp.total()); }
78  friend Timestamp operator-(const Timestamp& timestamp, const Timespan& offset) noexcept
79  { return Timestamp(timestamp.total() - offset.total()); }
80  friend Timestamp operator-(const Timespan& offset, const Timestamp& timestamp) noexcept
81  { return Timestamp(offset.total() - timestamp.total()); }
82 
83  friend Timespan operator-(const Timestamp& timestamp1, const Timestamp& timestamp2) noexcept
84  { return Timespan(timestamp1.total() - timestamp2.total()); }
85 
86  // Timestamp comparison
87  friend bool operator==(const Timestamp& timestamp1, uint64_t timestamp2) noexcept
88  { return timestamp1.total() == timestamp2; }
89  friend bool operator==(uint64_t timestamp1, const Timestamp& timestamp2) noexcept
90  { return timestamp1 == timestamp2.total(); }
91  friend bool operator==(const Timestamp& timestamp1, const Timestamp& timestamp2) noexcept
92  { return timestamp1.total() == timestamp2.total(); }
93 
94  friend bool operator!=(const Timestamp& timestamp1, uint64_t timestamp2) noexcept
95  { return timestamp1.total() != timestamp2; }
96  friend bool operator!=(uint64_t timestamp1, const Timestamp& timestamp2) noexcept
97  { return timestamp1 != timestamp2.total(); }
98  friend bool operator!=(const Timestamp& timestamp1, const Timestamp& timestamp2) noexcept
99  { return timestamp1.total() != timestamp2.total(); }
100 
101  friend bool operator>(const Timestamp& timestamp1, uint64_t timestamp2) noexcept
102  { return timestamp1.total() > timestamp2; }
103  friend bool operator>(uint64_t timestamp1, const Timestamp& timestamp2) noexcept
104  { return timestamp1 > timestamp2.total(); }
105  friend bool operator>(const Timestamp& timestamp1, const Timestamp& timestamp2) noexcept
106  { return timestamp1.total() > timestamp2.total(); }
107 
108  friend bool operator<(const Timestamp& timestamp1, uint64_t timestamp2) noexcept
109  { return timestamp1.total() < timestamp2; }
110  friend bool operator<(uint64_t timestamp1, const Timestamp& timestamp2) noexcept
111  { return timestamp1 < timestamp2.total(); }
112  friend bool operator<(const Timestamp& timestamp1, const Timestamp& timestamp2) noexcept
113  { return timestamp1.total() < timestamp2.total(); }
114 
115  friend bool operator>=(const Timestamp& timestamp1, uint64_t timestamp2) noexcept
116  { return timestamp1.total() >= timestamp2; }
117  friend bool operator>=(uint64_t timestamp1, const Timestamp& timestamp2) noexcept
118  { return timestamp1 >= timestamp2.total(); }
119  friend bool operator>=(const Timestamp& timestamp1, const Timestamp& timestamp2) noexcept
120  { return timestamp1.total() >= timestamp2.total(); }
121 
122  friend bool operator<=(const Timestamp& timestamp1, uint64_t timestamp2) noexcept
123  { return timestamp1.total() <= timestamp2; }
124  friend bool operator<=(uint64_t timestamp1, const Timestamp& timestamp2) noexcept
125  { return timestamp1 <= timestamp2.total(); }
126  friend bool operator<=(const Timestamp& timestamp1, const Timestamp& timestamp2) noexcept
127  { return timestamp1.total() <= timestamp2.total(); }
128 
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))); }
132 
134  uint64_t days() const noexcept
135  { return _timestamp / (24 * 60 * 60 * 1000000000ull); }
137  uint64_t hours() const noexcept
138  { return _timestamp / (60 * 60 * 1000000000ull); }
140  uint64_t minutes() const noexcept
141  { return _timestamp / (60 * 1000000000ull); }
143  uint64_t seconds() const noexcept
144  { return _timestamp / 1000000000; }
146  uint64_t milliseconds() const noexcept
147  { return _timestamp / 1000000; }
149  uint64_t microseconds() const noexcept
150  { return _timestamp / 1000; }
152  uint64_t nanoseconds() const noexcept
153  { return _timestamp; }
154 
156  uint64_t total() const noexcept { return _timestamp; }
157 
159  static Timestamp days(int64_t days) noexcept
160  { return Timestamp(days * 24 * 60 * 60 * 1000000000ull); }
162  static Timestamp hours(int64_t hours) noexcept
163  { return Timestamp(hours * 60 * 60 * 1000000000ull); }
165  static Timestamp minutes(int64_t minutes) noexcept
166  { return Timestamp(minutes * 60 * 1000000000ull); }
168  static Timestamp seconds(int64_t seconds) noexcept
169  { return Timestamp(seconds * 1000000000); }
171  static Timestamp milliseconds(int64_t milliseconds) noexcept
172  { return Timestamp(milliseconds * 1000000); }
174  static Timestamp microseconds(int64_t microseconds) noexcept
175  { return Timestamp(microseconds * 1000); }
177  static Timestamp nanoseconds(int64_t nanoseconds) noexcept
178  { return Timestamp(nanoseconds); }
179 
181 
186  static uint64_t epoch() noexcept { return 0; }
187 
189 
194  static uint64_t utc();
195 
197 
202  static uint64_t local();
203 
205 
210  static uint64_t nano();
211 
213 
223  static uint64_t rdts();
224 
226  void swap(Timestamp& timestamp) noexcept;
227  friend void swap(Timestamp& timestamp1, Timestamp& timestamp2) noexcept;
228 
229 protected:
231  uint64_t _timestamp;
232 };
233 
235 class EpochTimestamp : public Timestamp
236 {
237 public:
238  using Timestamp::Timestamp;
239 
243  EpochTimestamp(const Timestamp& timestamp) : Timestamp(timestamp) {}
244 };
245 
247 class UtcTimestamp : public Timestamp
248 {
249 public:
250  using Timestamp::Timestamp;
251 
255  UtcTimestamp(const Timestamp& timestamp) : Timestamp(timestamp) {}
256 };
257 
259 class LocalTimestamp : public Timestamp
260 {
261 public:
262  using Timestamp::Timestamp;
263 
267  LocalTimestamp(const Timestamp& timestamp) : Timestamp(timestamp) {}
268 };
269 
271 class NanoTimestamp : public Timestamp
272 {
273 public:
274  using Timestamp::Timestamp;
275 
279  NanoTimestamp(const Timestamp& timestamp) : Timestamp(timestamp) {}
280 };
281 
283 class RdtsTimestamp : public Timestamp
284 {
285 public:
286  using Timestamp::Timestamp;
287 
291  RdtsTimestamp(const Timestamp& timestamp) : Timestamp(timestamp) {}
292 };
293 
296 } // namespace CppCommon
297 
298 #include "timestamp.inl"
299 
300 #endif // CPPCOMMON_TIME_TIMESTAMP_H
Epoch timestamp.
Definition: timestamp.h:236
EpochTimestamp(const Timestamp &timestamp)
Initialize epoch timestamp with another timestamp value.
Definition: timestamp.h:243
EpochTimestamp()
Initialize epoch timestamp.
Definition: timestamp.h:241
Local timestamp.
Definition: timestamp.h:260
LocalTimestamp()
Initialize local timestamp with a current local time.
Definition: timestamp.h:265
LocalTimestamp(const Timestamp &timestamp)
Initialize local timestamp with another timestamp value.
Definition: timestamp.h:267
High resolution timestamp.
Definition: timestamp.h:272
NanoTimestamp(const Timestamp &timestamp)
Initialize high resolution timestamp with another timestamp value.
Definition: timestamp.h:279
NanoTimestamp()
Initialize high resolution timestamp with a current high resolution time.
Definition: timestamp.h:277
RDTS timestamp.
Definition: timestamp.h:284
RdtsTimestamp()
Initialize RDTS timestamp with a current RDTS time.
Definition: timestamp.h:289
RdtsTimestamp(const Timestamp &timestamp)
Initialize RDTS timestamp with another timestamp value.
Definition: timestamp.h:291
uint64_t minutes() const noexcept
Get total minutes of the current timestamp.
Definition: timestamp.h:140
friend Timestamp operator-(int64_t offset, const Timestamp &timestamp) noexcept
Definition: timestamp.h:76
Timestamp & operator-=(const Timespan &offset) noexcept
Definition: timestamp.h:62
static uint64_t local()
Get the local timestamp.
Definition: timestamp.cpp:123
friend Timestamp operator-(const Timespan &offset, const Timestamp &timestamp) noexcept
Definition: timestamp.h:80
void swap(Timestamp &timestamp) noexcept
Swap two instances.
Definition: timestamp.inl:11
Timestamp(const Timestamp &) noexcept=default
friend bool operator<=(const Timestamp &timestamp1, uint64_t timestamp2) noexcept
Definition: timestamp.h:122
friend bool operator<=(const Timestamp &timestamp1, const Timestamp &timestamp2) noexcept
Definition: timestamp.h:126
Timestamp & operator-=(int64_t offset) noexcept
Definition: timestamp.h:60
friend bool operator>(uint64_t timestamp1, const Timestamp &timestamp2) noexcept
Definition: timestamp.h:103
static Timestamp microseconds(int64_t microseconds) noexcept
Create the timestamp based on the given microseconds value.
Definition: timestamp.h:174
friend Timestamp operator-(const Timestamp &timestamp, const Timespan &offset) noexcept
Definition: timestamp.h:78
friend bool operator==(uint64_t timestamp1, const Timestamp &timestamp2) noexcept
Definition: timestamp.h:89
friend bool operator==(const Timestamp &timestamp1, const Timestamp &timestamp2) noexcept
Definition: timestamp.h:91
std::chrono::system_clock::time_point chrono() const noexcept
Convert timestamp to the std::chrono time point.
Definition: timestamp.h:130
uint64_t _timestamp
Timestamp value.
Definition: timestamp.h:231
friend bool operator>=(uint64_t timestamp1, const Timestamp &timestamp2) noexcept
Definition: timestamp.h:117
Timestamp & operator=(Timestamp &&) noexcept=default
friend Timestamp operator+(const Timestamp &timestamp, const Timespan &offset) noexcept
Definition: timestamp.h:69
Timestamp & operator=(const Timestamp &) noexcept=default
friend Timestamp operator-(const Timestamp &timestamp, int64_t offset) noexcept
Definition: timestamp.h:74
static Timestamp minutes(int64_t minutes) noexcept
Create the timestamp based on the given minutes value.
Definition: timestamp.h:165
friend bool operator!=(const Timestamp &timestamp1, const Timestamp &timestamp2) noexcept
Definition: timestamp.h:98
friend bool operator<=(uint64_t timestamp1, const Timestamp &timestamp2) noexcept
Definition: timestamp.h:124
friend Timestamp operator+(const Timestamp &timestamp, int64_t offset) noexcept
Definition: timestamp.h:65
static Timestamp days(int64_t days) noexcept
Create the timestamp based on the given days value.
Definition: timestamp.h:159
uint64_t milliseconds() const noexcept
Get total milliseconds of the current timestamp.
Definition: timestamp.h:146
friend bool operator<(const Timestamp &timestamp1, uint64_t timestamp2) noexcept
Definition: timestamp.h:108
Timestamp & operator+=(const Timespan &offset) noexcept
Definition: timestamp.h:57
static uint64_t utc()
Get the UTC timestamp.
Definition: timestamp.cpp:105
uint64_t microseconds() const noexcept
Get total microseconds of the current timestamp.
Definition: timestamp.h:149
static uint64_t nano()
Get the high resolution timestamp.
Definition: timestamp.cpp:153
static Timestamp hours(int64_t hours) noexcept
Create the timestamp based on the given hours value.
Definition: timestamp.h:162
uint64_t seconds() const noexcept
Get total seconds of the current timestamp.
Definition: timestamp.h:143
friend Timestamp operator+(const Timespan &offset, const Timestamp &timestamp) noexcept
Definition: timestamp.h:71
friend bool operator>=(const Timestamp &timestamp1, const Timestamp &timestamp2) noexcept
Definition: timestamp.h:119
friend bool operator>(const Timestamp &timestamp1, const Timestamp &timestamp2) noexcept
Definition: timestamp.h:105
static Timestamp milliseconds(int64_t milliseconds) noexcept
Create the timestamp based on the given milliseconds value.
Definition: timestamp.h:171
static uint64_t rdts()
Get the current value of RDTS (Read Time Stamp Counter)
Definition: timestamp.cpp:205
friend bool operator!=(uint64_t timestamp1, const Timestamp &timestamp2) noexcept
Definition: timestamp.h:96
Timestamp(const std::chrono::time_point< Clock, Duration > &time_point) noexcept
Initialize timestamp with a given std::chrono time point.
Definition: timestamp.h:44
Timestamp(uint64_t timestamp) noexcept
Initialize timestamp with a given time moment in nanoseconds.
Definition: timestamp.h:38
uint64_t hours() const noexcept
Get total hours of the current timestamp.
Definition: timestamp.h:137
static Timestamp seconds(int64_t seconds) noexcept
Create the timestamp based on the given seconds value.
Definition: timestamp.h:168
uint64_t total() const noexcept
Get total value of the current timestamp (total nanoseconds)
Definition: timestamp.h:156
static uint64_t epoch() noexcept
Get the epoch timestamp.
Definition: timestamp.h:186
friend bool operator<(uint64_t timestamp1, const Timestamp &timestamp2) noexcept
Definition: timestamp.h:110
friend bool operator>=(const Timestamp &timestamp1, uint64_t timestamp2) noexcept
Definition: timestamp.h:115
uint64_t days() const noexcept
Get total days of the current timestamp.
Definition: timestamp.h:134
friend bool operator<(const Timestamp &timestamp1, const Timestamp &timestamp2) noexcept
Definition: timestamp.h:112
friend Timestamp operator+(int64_t offset, const Timestamp &timestamp) noexcept
Definition: timestamp.h:67
friend bool operator!=(const Timestamp &timestamp1, uint64_t timestamp2) noexcept
Definition: timestamp.h:94
friend Timespan operator-(const Timestamp &timestamp1, const Timestamp &timestamp2) noexcept
Definition: timestamp.h:83
Timestamp() noexcept
Initialize timestamp with an epoch time.
Definition: timestamp.h:33
Timestamp(Timestamp &&) noexcept=default
friend bool operator>(const Timestamp &timestamp1, uint64_t timestamp2) noexcept
Definition: timestamp.h:101
friend bool operator==(const Timestamp &timestamp1, uint64_t timestamp2) noexcept
Definition: timestamp.h:87
static Timestamp nanoseconds(int64_t nanoseconds) noexcept
Create the timestamp based on the given nanoseconds value.
Definition: timestamp.h:177
uint64_t nanoseconds() const noexcept
Get total nanoseconds of the current timestamp.
Definition: timestamp.h:152
UTC timestamp.
Definition: timestamp.h:248
UtcTimestamp(const Timestamp &timestamp)
Initialize UTC timestamp with another timestamp value.
Definition: timestamp.h:255
UtcTimestamp()
Initialize UTC timestamp with a current UTC time.
Definition: timestamp.h:253
C++ Common project definitions.
Definition: token_bucket.h:15
Timespan definition.
Timestamp inline implementation.