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 ETL_CONSTEXPR14 typename etl::enable_if< etl::is_specialization<TDuration, etl::chrono::duration>::value, TDuration>::type absolute(TDuration dur)
46 ETL_NOEXCEPT
47 {
48 return TDuration((dur.count() < 0) ? -dur.count() : dur.count());
49 }
50
51 namespace chrono
52 {
53 //***********************************************************************
55 //***********************************************************************
56 template <typename TDuration>
58 {
59 private:
60
61 // Helper template to calculate the number of digits in a denominator at
62 // compile time
63 template <uintmax_t Den, int Width = 0>
64 struct fractional_width_helper
65 {
66 static constexpr int value = fractional_width_helper<Den / 10, Width + 1>::value;
67 };
68
69 // Base case: when Den == 1, stop recursion
70 template <int Width>
71 struct fractional_width_helper<1, Width>
72 {
73 static constexpr int value = Width;
74 };
75
76 // Special case: when Den == 0 (invalid denominator), return 0
77 template <int Width>
78 struct fractional_width_helper<0, Width>
79 {
80 static constexpr int value = 0;
81 };
82
83 // Calculate fractional width for TDuration
84 template <typename TDur>
85 struct calculate_fractional_width
86 {
87 static constexpr int value = (TDur::period::den == 1) ? 0 : fractional_width_helper<TDur::period::den>::value;
88 };
89
90 public:
91
92 ETL_STATIC_ASSERT((etl::is_specialization<TDuration, etl::chrono::duration>::value), "TDuration is not a etl::chrono::duration type");
93
94 static constexpr int fractional_width = calculate_fractional_width<TDuration>::value;
95
96 //***********************************************************************
98 //***********************************************************************
100 ratio<1, etl::power<10, size_t(fractional_width)>::value>>;
101
102 //***********************************************************************
104 //***********************************************************************
105 ETL_CONSTEXPR hh_mm_ss() ETL_NOEXCEPT
106 : d(TDuration::zero())
107 {
108 }
109
110 //***********************************************************************
112 //***********************************************************************
113 ETL_CONSTEXPR14 explicit hh_mm_ss(TDuration d_) ETL_NOEXCEPT
114 : d(d_)
115 {
116 }
117
118 //***********************************************************************
120 //***********************************************************************
121 ETL_NODISCARD ETL_CONSTEXPR14 bool is_negative() const ETL_NOEXCEPT
122 {
123 return d < TDuration::zero();
124 }
125
126 //***********************************************************************
128 //***********************************************************************
129 ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::hours hours() const ETL_NOEXCEPT
130 {
131 auto dur = etl::absolute(d);
132
134 }
135
136 //***********************************************************************
138 //***********************************************************************
139 ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::minutes minutes() const ETL_NOEXCEPT
140 {
141 auto dur = etl::absolute(d) - hours();
142
144 }
145
146 //***********************************************************************
148 //***********************************************************************
149 ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::seconds seconds() const ETL_NOEXCEPT
150 {
151 auto dur = etl::absolute(d) - hours() - minutes();
152
154 }
155
156 //***********************************************************************
158 //***********************************************************************
159 ETL_NODISCARD ETL_CONSTEXPR14 precision subseconds() const ETL_NOEXCEPT
160 {
161 return etl::absolute(d) - etl::chrono::duration_cast<etl::chrono::seconds>(etl::absolute(d));
162 }
163
164 //***********************************************************************
166 //***********************************************************************
167 ETL_NODISCARD ETL_CONSTEXPR14 explicit operator precision() const ETL_NOEXCEPT
168 {
169 return to_duration();
170 }
171
172 //***********************************************************************
174 //***********************************************************************
175 ETL_NODISCARD ETL_CONSTEXPR14 precision to_duration() const ETL_NOEXCEPT
176 {
177 return d;
178 }
179
180 private:
181
182 TDuration d;
183 };
184
185 template <typename TDuration>
186 constexpr int etl::chrono::hh_mm_ss<TDuration>::fractional_width;
187 } // namespace chrono
188} // namespace etl
duration
Definition duration.h:108
ETL_NODISCARD ETL_CONSTEXPR14 bool is_negative() const ETL_NOEXCEPT
Checks for negative duration.
Definition hh_mm_ss.h:121
ETL_NODISCARD ETL_CONSTEXPR14 precision subseconds() const ETL_NOEXCEPT
Returns the subseconds.
Definition hh_mm_ss.h:159
etl::chrono::duration< common_type_t< typename TDuration::rep, etl::chrono::seconds::rep >, ratio< 1, etl::power< 10, size_t(fractional_width)>::value > > precision
The return type for to_duration.
Definition hh_mm_ss.h:99
ETL_CONSTEXPR hh_mm_ss() ETL_NOEXCEPT
Default constructor.
Definition hh_mm_ss.h:105
ETL_NODISCARD ETL_CONSTEXPR14 precision to_duration() const ETL_NOEXCEPT
Returns the duration.
Definition hh_mm_ss.h:175
ETL_CONSTEXPR14 hh_mm_ss(TDuration d_) ETL_NOEXCEPT
Construct from duration.
Definition hh_mm_ss.h:113
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::hours hours() const ETL_NOEXCEPT
Returns the hours.
Definition hh_mm_ss.h:129
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::minutes minutes() const ETL_NOEXCEPT
Returns the minutes.
Definition hh_mm_ss.h:139
ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::seconds seconds() const ETL_NOEXCEPT
Returns the seconds.
Definition hh_mm_ss.h:149
ETL_CONSTEXPR14 TToDuration duration_cast(const etl::chrono::duration< TRep, TPeriod > &d) ETL_NOEXCEPT
duration_cast
Definition duration.h:343
Definition power.h:62
bitset_ext
Definition absolute.h:40
ratio
Definition ratio.h:53