Embedded Template Library 1.0
Loading...
Searching...
No Matches
clocks.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2025 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_IN_CHRONO_H
32 #error DO NOT DIRECTLY INCLUDE THIS FILE. USE CHRONO.H
33#endif
34
35#if !defined(ETL_CHRONO_SYSTEM_CLOCK_DURATION)
36 #if (INT_MAX >= INT32_MAX)
37 #define ETL_CHRONO_SYSTEM_CLOCK_DURATION etl::chrono::nanoseconds
38 #else
39 #define ETL_CHRONO_SYSTEM_CLOCK_DURATION etl::chrono::milliseconds
40 #endif
41#endif
42
43#if !defined(ETL_CHRONO_SYSTEM_CLOCK_IS_STEADY)
44 #define ETL_CHRONO_SYSTEM_CLOCK_IS_STEADY true
45#endif
46
47#if !defined(ETL_CHRONO_HIGH_RESOLUTION_CLOCK_DURATION)
48 #if (INT_MAX >= INT32_MAX)
49 #define ETL_CHRONO_HIGH_RESOLUTION_CLOCK_DURATION etl::chrono::nanoseconds
50 #else
51 #define ETL_CHRONO_HIGH_RESOLUTION_CLOCK_DURATION etl::chrono::milliseconds
52 #endif
53#endif
54
55#if !defined(ETL_CHRONO_HIGH_RESOLUTION_CLOCK_IS_STEADY)
56 #define ETL_CHRONO_HIGH_RESOLUTION_CLOCK_IS_STEADY true
57#endif
58
59#if !defined(ETL_CHRONO_STEADY_CLOCK_DURATION)
60 #if (INT_MAX >= INT32_MAX)
61 #define ETL_CHRONO_STEADY_CLOCK_DURATION etl::chrono::nanoseconds
62 #else
63 #define ETL_CHRONO_STEADY_CLOCK_DURATION etl::chrono::milliseconds
64 #endif
65#endif
66
67extern "C" ETL_CHRONO_SYSTEM_CLOCK_DURATION::rep etl_get_system_clock();
68extern "C" ETL_CHRONO_HIGH_RESOLUTION_CLOCK_DURATION::rep etl_get_high_resolution_clock();
69extern "C" ETL_CHRONO_STEADY_CLOCK_DURATION::rep etl_get_steady_clock();
70
71namespace etl
72{
73 namespace chrono
74 {
75 namespace private_chrono
76 {
77 template <bool b>
79 {
80 static ETL_CONSTANT bool is_steady = b;
81 };
82
83 template <bool b>
84 ETL_CONSTANT bool is_steady_trait<b>::is_steady;
85 } // namespace private_chrono
86
87 //*************************************************************************
89 //*************************************************************************
90 class system_clock : public private_chrono::is_steady_trait< ETL_CHRONO_SYSTEM_CLOCK_IS_STEADY>
91 {
92 public:
93
94 using duration = ETL_CHRONO_SYSTEM_CLOCK_DURATION;
95 using rep = duration::rep;
96 using period = duration::period;
98
99 //*************************************************************************
100 static time_point now() ETL_NOEXCEPT
101 {
102 return time_point(duration(etl_get_system_clock()));
103 }
104
105 //*************************************************************************
106 static etl::time_t to_time_t(const time_point& t) ETL_NOEXCEPT
107 {
108 // Get the duration since the epoch
109 duration dur = t.time_since_epoch();
110
111 // Convert the duration to seconds
112 return dur.count() / duration::period::den;
113 }
114
115 //*************************************************************************
116 static time_point from_time_t(etl::time_t t) ETL_NOEXCEPT
117 {
118 // Convert seconds to the appropriate duration
119 duration dur(t * duration::period::den);
120
121 // Construct and return the time_point
122 return time_point(dur);
123 }
124 };
125
126 //*************************************************************************
128 //*************************************************************************
129 class high_resolution_clock : public private_chrono::is_steady_trait< ETL_CHRONO_HIGH_RESOLUTION_CLOCK_IS_STEADY>
130 {
131 public:
132
133 using duration = ETL_CHRONO_HIGH_RESOLUTION_CLOCK_DURATION;
134 using rep = duration::rep;
135 using period = duration::period;
137
138 //*************************************************************************
139 static time_point now() ETL_NOEXCEPT
140 {
141 return time_point(duration(etl_get_high_resolution_clock()));
142 }
143 };
144
145 //*************************************************************************
147 //*************************************************************************
149 {
150 public:
151
152 using duration = ETL_CHRONO_STEADY_CLOCK_DURATION;
153 using rep = duration::rep;
154 using period = duration::period;
156
157 //*************************************************************************
158 static time_point now() ETL_NOEXCEPT
159 {
160 return time_point(duration(etl_get_steady_clock()));
161 }
162 };
163
164 //***************************************************************************
166 //***************************************************************************
167 template <typename Duration>
169
170 using sys_seconds = sys_time<etl::chrono::seconds>;
171 using sys_days = sys_time<etl::chrono::days>;
172
173 //***************************************************************************
175 //***************************************************************************
176 struct local_t
177 {
178 };
179
180 template <typename TDuration>
182
183 using local_seconds = local_time<etl::chrono::seconds>;
184 using local_days = local_time<etl::chrono::days>;
185
186 //*************************************************************************
189 //*************************************************************************
190 template <typename TToClock, typename TFromClock, typename TFromDuration>
193 {
194 // Get the duration since the epoch of the FromClock
195 auto from_duration = from_time_point.time_since_epoch();
196
197 // Convert the duration to the ToClock's duration type
198 auto to_duration = etl::chrono::duration_cast<typename TToClock::duration>(from_duration);
199
200 // Construct and return the time_point for the ToClock
202 }
203 } // namespace chrono
204} // namespace etl
The high resolution clock time.
Definition clocks.h:130
The steady clock time.
Definition clocks.h:149
The system clock time.
Definition clocks.h:91
Definition time_point.h:45
ETL_NODISCARD ETL_CONSTEXPR14 duration time_since_epoch() const ETL_NOEXCEPT
Definition time_point.h:100
ETL_CONSTEXPR14 etl::chrono::time_point< TToClock, typename TToClock::duration > clock_cast(const etl::chrono::time_point< TFromClock, TFromDuration > &from_time_point) ETL_NOEXCEPT
Definition clocks.h:192
etl::chrono::time_point< etl::chrono::system_clock, Duration > sys_time
System time.
Definition clocks.h:168
ETL_CONSTEXPR14 TToDuration duration_cast(const etl::chrono::duration< TRep, TPeriod > &d) ETL_NOEXCEPT
duration_cast
Definition duration.h:343
bitset_ext
Definition absolute.h:40
Local time.
Definition clocks.h:177