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#include "libxr_def.hpp"
7
18inline uint64_t libxr_timebase_max_valid_us = UINT64_MAX; // NOLINT
19
30inline uint32_t libxr_timebase_max_valid_ms = UINT32_MAX; // NOLINT
31
32namespace LibXR
33{
34
39{
40 public:
45
50 MicrosecondTimestamp(uint64_t microsecond) : microsecond_(microsecond) {}
51
56 operator uint64_t() const { return microsecond_; }
57
62 {
63 public:
68 Duration(uint64_t diff) : diff_(diff) {}
69
74 operator uint64_t() const { return diff_; }
75
80 [[nodiscard]] double ToSecond() const
81 {
82 return static_cast<double>(diff_) / 1000000.0;
83 }
84
89 [[nodiscard]] float ToSecondf() const
90 {
91 return static_cast<float>(diff_) / 1000000.0f;
92 }
93
98 [[nodiscard]] uint64_t ToMicrosecond() const { return diff_; }
99
104 [[nodiscard]] uint32_t ToMillisecond() const { return diff_ / 1000u; }
105
106 private:
107 uint64_t diff_ = 0;
108 };
109
121 [[nodiscard]] Duration operator-(const MicrosecondTimestamp& old_timestamp) const
122 {
123 uint64_t elapsed = 0;
124
125 if (microsecond_ >= old_timestamp.microsecond_)
126 {
127 elapsed = microsecond_ - old_timestamp.microsecond_;
128 }
129 else
130 {
131 elapsed = microsecond_ + (libxr_timebase_max_valid_us - old_timestamp.microsecond_) +
132 1ULL;
133 }
134
135 ASSERT(elapsed <= libxr_timebase_max_valid_us);
136
137 return Duration(elapsed);
138 }
139
146
147 private:
148 uint64_t microsecond_ = 0;
149};
150
155{
156 public:
161
166 MillisecondTimestamp(uint32_t millisecond) : millisecond_(millisecond) {}
167
172 operator uint32_t() const { return millisecond_; }
173
178 {
179 public:
184 Duration(uint32_t diff) : diff_(diff) {}
185
190 operator uint32_t() const { return diff_; }
191
196 [[nodiscard]] double ToSecond() const
197 {
198 return static_cast<double>(diff_) / 1000.0;
199 }
200
205 [[nodiscard]] float ToSecondf() const
206 {
207 return static_cast<float>(diff_) / 1000.0f;
208 }
209
214 [[nodiscard]] uint32_t ToMillisecond() const { return diff_; }
215
220 [[nodiscard]] uint64_t ToMicrosecond() const
221 {
222 return static_cast<uint64_t>(diff_) * 1000u;
223 }
224
225 private:
226 uint32_t diff_ = 0;
227 };
228
240 [[nodiscard]] Duration operator-(const MillisecondTimestamp& old_timestamp) const
241 {
242 uint32_t elapsed = 0;
243
244 if (millisecond_ >= old_timestamp.millisecond_)
245 {
246 elapsed = millisecond_ - old_timestamp.millisecond_;
247 }
248 else
249 {
250 elapsed =
251 millisecond_ + (libxr_timebase_max_valid_ms - old_timestamp.millisecond_) + 1U;
252 }
253
254 ASSERT(elapsed <= libxr_timebase_max_valid_ms);
255
256 return Duration(elapsed);
257 }
258
259 private:
260 uint32_t millisecond_ = 0;
261};
262
263} // 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