Embedded Template Library
1.0
Toggle main menu visibility
Loading...
Searching...
No Matches
clocks.h
Go to the documentation of this file.
1
2
3
/******************************************************************************
4
The MIT License(MIT)
5
6
Embedded Template Library.
7
https://github.com/ETLCPP/etl
8
https://www.etlcpp.com
9
10
Copyright(c) 2025 John Wellbelove
11
12
Permission is hereby granted, free of charge, to any person obtaining a copy
13
of this software and associated documentation files(the "Software"), to deal
14
in the Software without restriction, including without limitation the rights
15
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16
copies of the Software, and to permit persons to whom the Software is
17
furnished to do so, subject to the following conditions :
18
19
The above copyright notice and this permission notice shall be included in all
20
copies or substantial portions of the Software.
21
22
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
SOFTWARE.
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
67
extern
"C"
ETL_CHRONO_SYSTEM_CLOCK_DURATION::rep etl_get_system_clock();
68
extern
"C"
ETL_CHRONO_HIGH_RESOLUTION_CLOCK_DURATION::rep etl_get_high_resolution_clock();
69
extern
"C"
ETL_CHRONO_STEADY_CLOCK_DURATION::rep etl_get_steady_clock();
70
71
namespace
etl
72
{
73
namespace
chrono
74
{
75
namespace
private_chrono
76
{
77
template
<
bool
b>
78
struct
is_steady_trait
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;
97
using
time_point =
etl::chrono::time_point<system_clock, duration>
;
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;
136
using
time_point =
etl::chrono::time_point<high_resolution_clock, duration>
;
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
//*************************************************************************
148
class
steady_clock
:
public
private_chrono::is_steady_trait
<true>
149
{
150
public
:
151
152
using
duration = ETL_CHRONO_STEADY_CLOCK_DURATION;
153
using
rep = duration::rep;
154
using
period = duration::period;
155
using
time_point =
etl::chrono::time_point<steady_clock, duration>
;
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>
168
using
sys_time
=
etl::chrono::time_point<etl::chrono::system_clock, 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>
181
using
local_time =
etl::chrono::time_point<etl::chrono::local_t, 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>
191
ETL_CONSTEXPR14
etl::chrono::time_point<TToClock, typename TToClock::duration>
192
clock_cast
(
const
etl::chrono::time_point<TFromClock, TFromDuration>
& from_time_point) ETL_NOEXCEPT
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
201
return
etl::chrono::time_point<TToClock, typename TToClock::duration>
(to_duration);
202
}
203
}
// namespace chrono
204
}
// namespace etl
etl::chrono::high_resolution_clock
The high resolution clock time.
Definition
clocks.h:130
etl::chrono::steady_clock
The steady clock time.
Definition
clocks.h:149
etl::chrono::system_clock
The system clock time.
Definition
clocks.h:91
etl::chrono::time_point
Definition
time_point.h:45
etl::chrono::time_point::time_since_epoch
ETL_NODISCARD ETL_CONSTEXPR14 duration time_since_epoch() const ETL_NOEXCEPT
Definition
time_point.h:100
etl::chrono::clock_cast
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::sys_time
etl::chrono::time_point< etl::chrono::system_clock, Duration > sys_time
System time.
Definition
clocks.h:168
etl::chrono::duration_cast
ETL_CONSTEXPR14 TToDuration duration_cast(const etl::chrono::duration< TRep, TPeriod > &d) ETL_NOEXCEPT
duration_cast
Definition
duration.h:343
etl
Definition
absolute.h:40
etl::chrono::local_t
Local time.
Definition
clocks.h:177
etl::chrono::private_chrono::is_steady_trait
Definition
clocks.h:79
include
etl
private
chrono
clocks.h
Generated on
for Embedded Template Library by
1.17.0