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 #define ETL_CHRONO_SYSTEM_CLOCK_DURATION etl::chrono::nanoseconds
37#endif
38
39#if !defined(ETL_CHRONO_SYSTEM_CLOCK_IS_STEADY)
40 #define ETL_CHRONO_SYSTEM_CLOCK_IS_STEADY true
41#endif
42
43#if !defined(ETL_CHRONO_HIGH_RESOLUTION_CLOCK_DURATION)
44 #define ETL_CHRONO_HIGH_RESOLUTION_CLOCK_DURATION etl::chrono::nanoseconds
45#endif
46
47#if !defined(ETL_CHRONO_HIGH_RESOLUTION_CLOCK_IS_STEADY)
48 #define ETL_CHRONO_HIGH_RESOLUTION_CLOCK_IS_STEADY true
49#endif
50
51#if !defined(ETL_CHRONO_STEADY_CLOCK_DURATION)
52#define ETL_CHRONO_STEADY_CLOCK_DURATION etl::chrono::nanoseconds
53#endif
54
55extern "C" ETL_CHRONO_SYSTEM_CLOCK_DURATION::rep etl_get_system_clock();
56extern "C" ETL_CHRONO_HIGH_RESOLUTION_CLOCK_DURATION::rep etl_get_high_resolution_clock();
57extern "C" ETL_CHRONO_STEADY_CLOCK_DURATION::rep etl_get_steady_clock();
58
59namespace etl
60{
61 namespace chrono
62 {
63 namespace private_chrono
64 {
65 template <bool b>
67 {
68 static ETL_CONSTANT bool is_steady = b;
69 };
70
71 template <bool b>
72 ETL_CONSTANT bool is_steady_trait<b>::is_steady;
73 }
74
75 //*************************************************************************
77 //*************************************************************************
78 class system_clock : public private_chrono::is_steady_trait<ETL_CHRONO_SYSTEM_CLOCK_IS_STEADY>
79 {
80 public:
81
82 using duration = ETL_CHRONO_SYSTEM_CLOCK_DURATION;
83 using rep = duration::rep;
84 using period = duration::period;
86
87 //*************************************************************************
88 static time_point now() ETL_NOEXCEPT
89 {
90 return time_point(duration(etl_get_system_clock()));
91 }
92
93 //*************************************************************************
94 static etl::time_t to_time_t(const time_point& t) ETL_NOEXCEPT
95 {
96 // Get the duration since the epoch
97 duration dur = t.time_since_epoch();
98
99 // Convert the duration to seconds
100 return dur.count() / duration::period::den;
101 }
102
103 //*************************************************************************
104 static time_point from_time_t(etl::time_t t) ETL_NOEXCEPT
105 {
106 // Convert seconds to the appropriate duration
107 duration dur(t * duration::period::den);
108
109 // Construct and return the time_point
110 return time_point(dur);
111 }
112 };
113
114 //*************************************************************************
116 //*************************************************************************
117 class high_resolution_clock : public private_chrono::is_steady_trait<ETL_CHRONO_HIGH_RESOLUTION_CLOCK_IS_STEADY>
118 {
119 public:
120
121 using duration = ETL_CHRONO_HIGH_RESOLUTION_CLOCK_DURATION;
122 using rep = duration::rep;
123 using period = duration::period;
125
126 //*************************************************************************
127 static time_point now() ETL_NOEXCEPT
128 {
129 return time_point(duration(etl_get_high_resolution_clock()));
130 }
131 };
132
133 //*************************************************************************
135 //*************************************************************************
137 {
138 public:
139
140 using duration = ETL_CHRONO_STEADY_CLOCK_DURATION;
141 using rep = duration::rep;
142 using period = duration::period;
144
145 //*************************************************************************
146 static time_point now() ETL_NOEXCEPT
147 {
148 return time_point(duration(etl_get_steady_clock()));
149 }
150 };
151
152 //***************************************************************************
154 //***************************************************************************
155 template <typename Duration>
157
158 using sys_seconds = sys_time<etl::chrono::seconds>;
159 using sys_days = sys_time<etl::chrono::days>;
160
161 //***************************************************************************
163 //***************************************************************************
164 struct local_t
165 {
166 };
167
168 template <typename TDuration>
170
171 using local_seconds = local_time<etl::chrono::seconds>;
172 using local_days = local_time<etl::chrono::days>;
173
174 //*************************************************************************
177 //*************************************************************************
178 template <typename TToClock, typename TFromClock, typename TFromDuration>
181 {
182 // Get the duration since the epoch of the FromClock
183 auto from_duration = from_time_point.time_since_epoch();
184
185 // Convert the duration to the ToClock's duration type
186 auto to_duration = etl::chrono::duration_cast<typename TToClock::duration>(from_duration);
187
188 // Construct and return the time_point for the ToClock
190 }
191 }
192}
The high resolution clock time.
Definition clocks.h:118
The steady clock time.
Definition clocks.h:137
The system clock time.
Definition clocks.h:79
Definition time_point.h:45
ETL_NODISCARD ETL_CONSTEXPR14 duration time_since_epoch() const ETL_NOEXCEPT
Returns a duration representing the amount of time between this and the clock's epoch.
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:180
etl::chrono::time_point< etl::chrono::system_clock, Duration > sys_time
System time.
Definition clocks.h:156
ETL_CONSTEXPR14 TToDuration duration_cast(const etl::chrono::duration< TRep, TPeriod > &d) ETL_NOEXCEPT
duration_cast
Definition duration.h:339
bitset_ext
Definition absolute.h:39
Local time.
Definition clocks.h:165