Embedded Template Library 1.0
Loading...
Searching...
No Matches
hh_mm_ss.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#include "../../absolute.h"
36#include "../../power.h"
37
38namespace etl
39{
40 //***********************************************************************
43 //***********************************************************************
44 template <typename TDuration>
45 ETL_NODISCARD
46 ETL_CONSTEXPR14
47 typename etl::enable_if<etl::is_specialization<TDuration, etl::chrono::duration>::value, TDuration>::type
48 absolute(TDuration dur) ETL_NOEXCEPT
49 {
50 return TDuration((dur.count() < 0) ? -dur.count() : dur.count());
51 }
52
53 namespace chrono
54 {
55 //***********************************************************************
57 //***********************************************************************
58 template <typename TDuration>
60 {
61 private:
62
63 // Helper template to calculate the number of digits in a denominator at compile time
64 template <uintmax_t Den, int Width = 0>
65 struct fractional_width_helper
66 {
67 static constexpr int value = fractional_width_helper<Den / 10, Width + 1>::value;
68 };
69
70 // Base case: when Den == 1, stop recursion
71 template <int Width>
72 struct fractional_width_helper<1, Width>
73 {
74 static constexpr int value = Width;
75 };
76
77 // Special case: when Den == 0 (invalid denominator), return 0
78 template <int Width>
79 struct fractional_width_helper<0, Width>
80 {
81 static constexpr int value = 0;
82 };
83
84 // Calculate fractional width for TDuration
85 template <typename TDur>
86 struct calculate_fractional_width
87 {
88 static constexpr int value = (TDur::period::den == 1)
89 ? 0
90 : fractional_width_helper<TDur::period::den>::value;
91 };
92
93 public:
94
95 ETL_STATIC_ASSERT((etl::is_specialization<TDuration, etl::chrono::duration>::value), "TDuration is not a etl::chrono::duration type");
96
97 static constexpr int fractional_width = calculate_fractional_width<TDuration>::value;
98
99 //***********************************************************************
101 //***********************************************************************
104
105 //***********************************************************************
107 //***********************************************************************
108 ETL_CONSTEXPR
109 hh_mm_ss() ETL_NOEXCEPT
110 : d(TDuration::zero())
111 {
112 }
113
114 //***********************************************************************
116 //***********************************************************************
117 ETL_CONSTEXPR14
118 explicit hh_mm_ss(TDuration d_) ETL_NOEXCEPT
119 : d(d_)
120 {
121 }
122
123 //***********************************************************************
125 //***********************************************************************
126 ETL_NODISCARD
127 ETL_CONSTEXPR14
128 bool is_negative() const ETL_NOEXCEPT
129 {
130 return d < TDuration::zero();
131 }
132
133 //***********************************************************************
135 //***********************************************************************
136 ETL_NODISCARD
137 ETL_CONSTEXPR14
138 etl::chrono::hours hours() const ETL_NOEXCEPT
139 {
140 auto dur = etl::absolute(d);
141
143 }
144
145 //***********************************************************************
147 //***********************************************************************
148 ETL_NODISCARD
149 ETL_CONSTEXPR14 etl::chrono::minutes minutes() const ETL_NOEXCEPT
150 {
151 auto dur = etl::absolute(d) - hours();
152
154 }
155
156 //***********************************************************************
158 //***********************************************************************
159 ETL_NODISCARD
160 ETL_CONSTEXPR14
161 etl::chrono::seconds seconds() const ETL_NOEXCEPT
162 {
163 auto dur = etl::absolute(d) - hours() - minutes();
164
166 }
167
168 //***********************************************************************
170 //***********************************************************************
171 ETL_NODISCARD
172 ETL_CONSTEXPR14 precision subseconds() const ETL_NOEXCEPT
173 {
174 return etl::absolute(d) - etl::chrono::duration_cast<etl::chrono::seconds>(etl::absolute(d));
175 }
176
177 //***********************************************************************
179 //***********************************************************************
180 ETL_NODISCARD
181 ETL_CONSTEXPR14 explicit operator precision() const ETL_NOEXCEPT
182 {
183 return to_duration();
184 }
185
186 //***********************************************************************
188 //***********************************************************************
189 ETL_NODISCARD
190 ETL_CONSTEXPR14
191 precision to_duration() const ETL_NOEXCEPT
192 {
193 return d;
194 }
195
196 private:
197
198 TDuration d;
199 };
200
201 template <typename TDuration>
202 constexpr int etl::chrono::hh_mm_ss<TDuration>::fractional_width;
203 }
204}
duration
Definition duration.h:108
ETL_NODISCARD ETL_CONSTEXPR14 bool is_negative() const ETL_NOEXCEPT
Checks for negative duration.
Definition hh_mm_ss.h:128
ETL_NODISCARD ETL_CONSTEXPR14 precision subseconds() const ETL_NOEXCEPT
Returns the subseconds.
Definition hh_mm_ss.h:172
ETL_CONSTEXPR hh_mm_ss() ETL_NOEXCEPT
Default constructor.
Definition hh_mm_ss.h:109
ETL_NODISCARD ETL_CONSTEXPR14 precision to_duration() const ETL_NOEXCEPT
Returns the duration.
Definition hh_mm_ss.h:191
ETL_CONSTEXPR14 hh_mm_ss(TDuration d_) ETL_NOEXCEPT
Construct from duration.
Definition hh_mm_ss.h:118
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::hours hours() const ETL_NOEXCEPT
Returns the hours.
Definition hh_mm_ss.h:138
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::minutes minutes() const ETL_NOEXCEPT
Returns the minutes.
Definition hh_mm_ss.h:149
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::seconds seconds() const ETL_NOEXCEPT
Returns the seconds.
Definition hh_mm_ss.h:161
etl::chrono::duration< common_type_t< typename TDuration::rep, etl::chrono::seconds::rep >, ratio< 1, etl::power< 10, fractional_width >::value > > precision
The return type for to_duration.
Definition hh_mm_ss.h:102
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
ratio
Definition ratio.h:53