libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
libxr_time.hpp
1#pragma once
2
3#include <cstdint>
4
5#include "libxr_assert.hpp"
6
7namespace LibXR
8{
9namespace Detail
10{
11
12[[nodiscard]] inline uint64_t& TimebaseMaxValidUsStorage() noexcept
13{
14 static uint64_t value = UINT64_MAX;
15 return value;
16}
17
18[[nodiscard]] inline uint32_t& TimebaseMaxValidMsStorage() noexcept
19{
20 static uint32_t value = UINT32_MAX;
21 return value;
22}
23
24[[nodiscard]] inline uint64_t TimebaseMaxValidUs() noexcept
25{
26 return TimebaseMaxValidUsStorage();
27}
28
29[[nodiscard]] inline uint32_t TimebaseMaxValidMs() noexcept
30{
31 return TimebaseMaxValidMsStorage();
32}
33
34inline void ConfigureTimebaseWrapRange(uint64_t max_valid_us,
35 uint32_t max_valid_ms) noexcept
36{
37 TimebaseMaxValidUsStorage() = max_valid_us;
38 TimebaseMaxValidMsStorage() = max_valid_ms;
39}
40
41} // namespace Detail
42
47{
48 public:
53
58 MicrosecondTimestamp(uint64_t microsecond) : microsecond_(microsecond) {}
59
64 operator uint64_t() const { return microsecond_; }
65
70 {
71 public:
76 Duration(uint64_t diff) : diff_(diff) {}
77
82 operator uint64_t() const { return diff_; }
83
88 [[nodiscard]] double ToSecond() const
89 {
90 return static_cast<double>(diff_) / 1000000.0;
91 }
92
97 [[nodiscard]] float ToSecondf() const
98 {
99 return static_cast<float>(diff_) / 1000000.0f;
100 }
101
106 [[nodiscard]] uint64_t ToMicrosecond() const { return diff_; }
107
112 [[nodiscard]] uint32_t ToMillisecond() const { return diff_ / 1000u; }
113
114 private:
115 uint64_t diff_ = 0;
116 };
117
129 [[nodiscard]] Duration operator-(const MicrosecondTimestamp& old_timestamp) const
130 {
131 uint64_t elapsed = 0;
132 const uint64_t max_valid = Detail::TimebaseMaxValidUs();
133
134 if (microsecond_ >= old_timestamp.microsecond_)
135 {
136 elapsed = microsecond_ - old_timestamp.microsecond_;
137 }
138 else
139 {
140 elapsed = microsecond_ + (max_valid - old_timestamp.microsecond_) + 1ULL;
141 }
142
143 ASSERT(elapsed <= max_valid);
144
145 return Duration(elapsed);
146 }
147
154
155 private:
156 uint64_t microsecond_ = 0;
157};
158
163{
164 public:
169
174 MillisecondTimestamp(uint32_t millisecond) : millisecond_(millisecond) {}
175
180 operator uint32_t() const { return millisecond_; }
181
186 {
187 public:
192 Duration(uint32_t diff) : diff_(diff) {}
193
198 operator uint32_t() const { return diff_; }
199
204 [[nodiscard]] double ToSecond() const { return static_cast<double>(diff_) / 1000.0; }
205
210 [[nodiscard]] float ToSecondf() const { return static_cast<float>(diff_) / 1000.0f; }
211
216 [[nodiscard]] uint32_t ToMillisecond() const { return diff_; }
217
222 [[nodiscard]] uint64_t ToMicrosecond() const
223 {
224 return static_cast<uint64_t>(diff_) * 1000u;
225 }
226
227 private:
228 uint32_t diff_ = 0;
229 };
230
242 [[nodiscard]] Duration operator-(const MillisecondTimestamp& old_timestamp) const
243 {
244 uint32_t elapsed = 0;
245 const uint32_t max_valid = Detail::TimebaseMaxValidMs();
246
247 if (millisecond_ >= old_timestamp.millisecond_)
248 {
249 elapsed = millisecond_ - old_timestamp.millisecond_;
250 }
251 else
252 {
253 elapsed = millisecond_ + (max_valid - old_timestamp.millisecond_) + 1U;
254 }
255
256 ASSERT(elapsed <= max_valid);
257
258 return Duration(elapsed);
259 }
260
261 private:
262 uint32_t millisecond_ = 0;
263};
264
265} // namespace LibXR
微秒时长 / Duration in microseconds
uint64_t ToMicrosecond() const
转换为微秒 / Convert the duration to microseconds
float ToSecondf() const
转换为单精度秒 / Convert the duration to seconds in single precision
double ToSecond() const
转换为秒 / Convert the duration to seconds
Duration(uint64_t diff)
从微秒差值构造时长 / Construct a duration from a microsecond delta
uint32_t ToMillisecond() const
转换为毫秒 / Convert the duration to milliseconds
微秒时间戳 / Microsecond timestamp
MicrosecondTimestamp()=default
创建零值微秒时间戳 / Construct a zero microsecond timestamp
Duration operator-(const MicrosecondTimestamp &old_timestamp) const
计算时间差,支持时间基准回绕 / Compute elapsed time with timebase wraparound
MicrosecondTimestamp(uint64_t microsecond)
从微秒计数构造时间戳 / Construct a timestamp from microsecond ticks
MicrosecondTimestamp & operator=(const MicrosecondTimestamp &other)=default
复制赋值 / Copy-assign from another timestamp
毫秒时长 / Duration in milliseconds
uint64_t ToMicrosecond() const
转换为微秒 / Convert the duration to microseconds
Duration(uint32_t diff)
从毫秒差值构造时长 / Construct a duration from a millisecond delta
float ToSecondf() const
转换为单精度秒 / Convert the duration to seconds in single precision
uint32_t ToMillisecond() const
转换为毫秒 / Convert the duration to milliseconds
double ToSecond() const
转换为秒 / Convert the duration to seconds
毫秒时间戳 / Millisecond timestamp
MillisecondTimestamp()=default
创建零值毫秒时间戳 / Construct a zero millisecond timestamp
MillisecondTimestamp(uint32_t millisecond)
从毫秒计数构造时间戳 / Construct a timestamp from millisecond ticks
Duration operator-(const MillisecondTimestamp &old_timestamp) const
计算时间差,支持时间基准回绕 / Compute elapsed time with timebase wraparound
LibXR 命名空间
Definition ch32_can.hpp:14