Embedded Template Library 1.0
Loading...
Searching...
No Matches
duration.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) 2023 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 "../../limits.h"
36#include "../../ratio.h"
37#include "../../static_assert.h"
38#include "../../type_traits.h"
39
40#include <string.h>
41
42namespace etl
43{
44 namespace chrono
45 {
46 namespace private_chrono
47 {
48 // Helper to find the greatest common divisor
49 template <intmax_t Value1, intmax_t Value2>
50 struct gcd
51 {
52 static ETL_CONSTANT intmax_t value = gcd<Value2, Value1 % Value2>::value;
53 };
54
55 template <intmax_t Value1>
56 struct gcd<Value1, 0>
57 {
58 static ETL_CONSTANT intmax_t value = Value1;
59 };
60
61 // Helper to find the least common multiple
62 template <intmax_t Value1, intmax_t Value2>
63 struct lcm
64 {
65 static ETL_CONSTANT intmax_t value = (Value1 / gcd<Value1, Value2>::value) * Value2;
66 };
67 } // namespace private_chrono
68
69 //***********************************************************************
71 //***********************************************************************
72 template <typename TRep>
74 {
75 //***********************************************************************
76 ETL_NODISCARD
77 static ETL_CONSTEXPR TRep zero() ETL_NOEXCEPT
78 {
79 return TRep(0);
80 }
81
82 //***********************************************************************
83 ETL_NODISCARD
84 static ETL_CONSTEXPR14 TRep min() ETL_NOEXCEPT
85 {
87 }
88
89 //***********************************************************************
90 ETL_NODISCARD
91 static ETL_CONSTEXPR14 TRep max() ETL_NOEXCEPT
92 {
94 }
95 };
96
97 template <typename TRep, typename TPeriod>
98 class duration;
99
100 template <typename TToDuration, typename TRep, typename TPeriod>
101 ETL_CONSTEXPR14 TToDuration duration_cast(const etl::chrono::duration<TRep, TPeriod>& d) ETL_NOEXCEPT;
102
103 //***********************************************************************
105 //***********************************************************************
106 template <typename TRep, typename TPeriod = etl::ratio<1> >
107 class duration
108 {
109 public:
110
111 using rep = TRep;
112 using period = typename TPeriod::type;
113
114 //***********************************************************************
115 ETL_CONSTEXPR duration() ETL_NOEXCEPT
116 : value(0)
117 {
118 }
119
120 //***********************************************************************
121 ETL_CONSTEXPR14 duration(const etl::chrono::duration<TRep, TPeriod>& other) ETL_NOEXCEPT
122 : value(other.value)
123 {
124 }
125
126 //***********************************************************************
127 template <typename TRep2>
128 ETL_CONSTEXPR14 explicit duration(const TRep2& value_) ETL_NOEXCEPT
129 : value(static_cast<TRep>(value_))
130 {
131 }
132
133 //***********************************************************************
134 template < typename TRep2, typename TPeriod2, typename etl::enable_if<etl::ratio_divide<TPeriod2, TPeriod>::den == 1, int>::type = 0>
135 ETL_CONSTEXPR14 duration(const etl::chrono::duration<TRep2, TPeriod2>& other) ETL_NOEXCEPT
137 {
138 ETL_STATIC_ASSERT(!(etl::is_integral<TRep>::value && etl::is_floating_point<TRep2>::value),
139 "Cannot convert duration from floating point to integral");
140 }
141
142 //***********************************************************************
143 ETL_CONSTEXPR14 etl::chrono::duration<TRep, TPeriod> operator=(const etl::chrono::duration<TRep, TPeriod>& other) ETL_NOEXCEPT
144 {
145 value = other.count();
146
147 return *this;
148 }
149
150 //***********************************************************************
151 template <typename TRep2, typename TPeriod2>
152 ETL_CONSTEXPR14 etl::chrono::duration<TRep, TPeriod> operator=(const etl::chrono::duration<TRep2, TPeriod2>& other) ETL_NOEXCEPT
153 {
155
156 return *this;
157 }
158
159 //***********************************************************************
160 ETL_CONSTEXPR14 TRep count() const ETL_NOEXCEPT
161 {
162 return value;
163 }
164
165 //***********************************************************************
166 ETL_CONSTEXPR14 etl::common_type_t<duration> operator+() const ETL_NOEXCEPT
167 {
168 return etl::common_type_t<duration>(*this);
169 }
170
171 //***********************************************************************
172 ETL_CONSTEXPR14 etl::common_type_t<duration> operator-() const ETL_NOEXCEPT
173 {
174 return etl::common_type_t<duration>(-value);
175 }
176
177 //***********************************************************************
178 ETL_NODISCARD
179 static ETL_CONSTEXPR14 etl::chrono::duration<TRep, TPeriod> zero() ETL_NOEXCEPT
180 {
181 return etl::chrono::duration<TRep, TPeriod>(etl::chrono::duration_values<TRep>::zero());
182 }
183
184 //***********************************************************************
185 ETL_NODISCARD
186 static ETL_CONSTEXPR14 etl::chrono::duration<TRep, TPeriod> min() ETL_NOEXCEPT
187 {
188 return etl::chrono::duration<TRep, TPeriod>(etl::chrono::duration_values<TRep>::min());
189 }
190
191 //***********************************************************************
192 ETL_NODISCARD
193 static ETL_CONSTEXPR14 etl::chrono::duration<TRep, TPeriod> max() ETL_NOEXCEPT
194 {
195 return etl::chrono::duration<TRep, TPeriod>(etl::chrono::duration_values<TRep>::max());
196 }
197
198 //***********************************************************************
199 ETL_NODISCARD ETL_CONSTEXPR14 etl::chrono::duration<TRep, TPeriod> absolute() const ETL_NOEXCEPT
200 {
201 return etl::chrono::duration<TRep, TPeriod>(value < 0 ? -value : value);
202 }
203
204 //***********************************************************************
205 ETL_CONSTEXPR14 duration& operator++() ETL_NOEXCEPT
206 {
207 ++value;
208
209 return *this;
210 }
211
212 //***********************************************************************
213 ETL_CONSTEXPR14 duration operator++(int) ETL_NOEXCEPT
214 {
215 duration temp(*this);
216 ++value;
217
218 return temp;
219 }
220
221 //***********************************************************************
222 ETL_CONSTEXPR14 duration& operator--() ETL_NOEXCEPT
223 {
224 --value;
225
226 return *this;
227 }
228
229 //***********************************************************************
230 ETL_CONSTEXPR14 duration operator--(int) ETL_NOEXCEPT
231 {
232 duration temp(*this);
233 --value;
234
235 return temp;
236 }
237
238 //***********************************************************************
239 ETL_CONSTEXPR14 duration& operator+=(const duration<TRep, TPeriod>& d) ETL_NOEXCEPT
240 {
241 value += d.count();
242
243 return *this;
244 }
245
246 //***********************************************************************
247 ETL_CONSTEXPR14 duration& operator-=(const duration<TRep, TPeriod>& d) ETL_NOEXCEPT
248 {
249 value -= d.count();
250
251 return *this;
252 }
253
254 //***********************************************************************
255 ETL_CONSTEXPR14 duration& operator*=(const TRep& r) ETL_NOEXCEPT
256 {
257 value *= r;
258
259 return *this;
260 }
261
262 //***********************************************************************
263 ETL_CONSTEXPR14 duration& operator/=(const TRep& r) ETL_NOEXCEPT
264 {
265 value /= r;
266
267 return *this;
268 }
269
270 //***********************************************************************
271 ETL_CONSTEXPR14 duration& operator%=(const TRep& r) ETL_NOEXCEPT
272 {
273 value %= r;
274
275 return *this;
276 }
277
278 //***********************************************************************
279 ETL_CONSTEXPR14 duration& operator%=(const duration<TRep, TPeriod>& d) ETL_NOEXCEPT
280 {
281 value %= d.count();
282
283 return *this;
284 }
285
286 //***********************************************************************
291 //***********************************************************************
292 template <typename TRep2, typename TPeriod2>
293 ETL_CONSTEXPR14 int compare(const duration<TRep2, TPeriod2>& other) const ETL_NOEXCEPT
294 {
295 // Determine the common type of the two durations.
296 using common_duration = typename etl::common_type< etl::chrono::duration<TRep, TPeriod>, etl::chrono::duration<TRep2, TPeriod2>>::type;
297
298 common_duration lhs_converted = etl::chrono::duration_cast<common_duration>(*this);
299 common_duration rhs_converted = etl::chrono::duration_cast<common_duration>(other);
300
301 if (lhs_converted.count() < rhs_converted.count())
302 return -1;
303 if (lhs_converted.count() > rhs_converted.count())
304 return 1;
305
306 return 0;
307 }
308
309 private:
310
311 TRep value;
312 };
313
314 //***********************************************************************
316 //***********************************************************************
317#if (ETL_USING_64BIT_TYPES)
318 #if (INT_MAX >= INT32_MAX)
321 #endif
324#else
325 #if (INT_MAX >= INT32_MAX)
328 #endif
331#endif
338
339 //***********************************************************************
341 //***********************************************************************
342 template <typename TToDuration, typename TRep, typename TPeriod>
343 ETL_CONSTEXPR14 TToDuration duration_cast(const etl::chrono::duration<TRep, TPeriod>& d) ETL_NOEXCEPT
344 {
345 using from_rep = TRep;
346 using from_period = TPeriod;
347
348 using to_rep = typename TToDuration::rep;
349 using to_period = typename TToDuration::period;
350
351 using ratio_divide_t = typename etl::ratio_divide<from_period, to_period>;
352 using common_t = typename etl::common_type<from_rep, to_rep, intmax_t>::type;
353
354 common_t ct_count = static_cast<common_t>(d.count());
355 common_t ct_num = static_cast<common_t>(ratio_divide_t::type::num);
356 common_t ct_den = static_cast<common_t>(ratio_divide_t::type::den);
357
358 if ETL_IF_CONSTEXPR ((from_period::num == to_period::num) && (from_period::den == to_period::den))
359 {
360 return TToDuration(static_cast<to_rep>(d.count()));
361 }
362 else if ETL_IF_CONSTEXPR (ratio_divide_t::num == 1)
363 {
364 return TToDuration(static_cast<to_rep>(ct_count / ct_den));
365 }
366 else if ETL_IF_CONSTEXPR (ratio_divide_t::den == 1)
367 {
368 return TToDuration(static_cast<to_rep>(ct_count * ct_num));
369 }
370 else
371 {
372 return TToDuration(static_cast<to_rep>((ct_count * ct_num) / ct_den));
373 }
374 }
375 } // namespace chrono
376
377 //*************************************************************************
379 //*************************************************************************
380#if ETL_USING_8BIT_TYPES
381 template <typename TRep, typename TPeriod>
382 struct hash<etl::chrono::duration<TRep, TPeriod> >
383 {
384 ETL_CONSTEXPR14 size_t operator()(const etl::chrono::duration<TRep, TPeriod>& d) const ETL_NOEXCEPT
385 {
386 uint8_t buffer[sizeof(TRep) + sizeof(intmax_t) + sizeof(intmax_t)];
387
388 TRep value = d.count();
389 intmax_t num = TPeriod::num;
390 intmax_t den = TPeriod::den;
391
392 memcpy(buffer, &value, sizeof(TRep));
393 memcpy(buffer + sizeof(TRep), &num, sizeof(intmax_t));
394 memcpy(buffer + sizeof(TRep) + sizeof(intmax_t), &den, sizeof(intmax_t));
395
396 return etl::private_hash::generic_hash<size_t>(buffer, buffer + sizeof(TRep) + sizeof(intmax_t) + sizeof(intmax_t));
397 }
398 };
399#endif
400
401 //***********************************************************************
403 //***********************************************************************
404 template <typename TRep1, typename TPeriod1, typename TRep2, typename TPeriod2>
405 struct common_type<etl::chrono::duration<TRep1, TPeriod1>, etl::chrono::duration<TRep2, TPeriod2>>
406 {
407 private:
408
409 using value_type = typename etl::common_type<TRep1, TRep2>::type;
411 etl::chrono::private_chrono::lcm<TPeriod1::den, TPeriod2::den>::value>;
412
413 public:
414
416 };
417
418 //***********************************************************************
420 //***********************************************************************
421 template <typename TRep1, typename TPeriod1, typename TRep2, typename TPeriod2>
422 ETL_CONSTEXPR14 bool operator==(const etl::chrono::duration<TRep1, TPeriod1>& lhs, const etl::chrono::duration<TRep2, TPeriod2>& rhs) ETL_NOEXCEPT
423 {
424 using common_t = typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, etl::chrono::duration<TRep2, TPeriod2> >::type;
425
426 common_t l = etl::chrono::duration_cast<common_t>(lhs);
427 common_t r = etl::chrono::duration_cast<common_t>(rhs);
428
429 return l.count() == r.count();
430 }
431
432 //***********************************************************************
434 //***********************************************************************
435 template <typename TRep1, typename TPeriod1, typename TRep2, typename TPeriod2>
436 ETL_CONSTEXPR14 bool operator!=(const etl::chrono::duration<TRep1, TPeriod1>& lhs, const etl::chrono::duration<TRep2, TPeriod2>& rhs) ETL_NOEXCEPT
437 {
438 return !(lhs == rhs);
439 }
440
441 //***********************************************************************
443 //***********************************************************************
444 template <typename TRep1, typename TPeriod1, typename TRep2, typename TPeriod2>
445 ETL_CONSTEXPR14 bool operator<(const etl::chrono::duration<TRep1, TPeriod1>& lhs, const etl::chrono::duration<TRep2, TPeriod2>& rhs) ETL_NOEXCEPT
446 {
447 using common_t = typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, etl::chrono::duration<TRep2, TPeriod2> >::type;
448
449 common_t l = etl::chrono::duration_cast<common_t>(lhs);
450 common_t r = etl::chrono::duration_cast<common_t>(rhs);
451
452 return l.count() < r.count();
453 }
454
455 //***********************************************************************
457 //***********************************************************************
458 template <typename TRep1, typename TPeriod1, typename TRep2, typename TPeriod2>
459 ETL_CONSTEXPR14 bool operator<=(const etl::chrono::duration<TRep1, TPeriod1>& lhs, const etl::chrono::duration<TRep2, TPeriod2>& rhs) ETL_NOEXCEPT
460 {
461 return !(rhs < lhs);
462 }
463
464 //***********************************************************************
466 //***********************************************************************
467 template <typename TRep1, typename TPeriod1, typename TRep2, typename TPeriod2>
468 ETL_CONSTEXPR14 bool operator>(const etl::chrono::duration<TRep1, TPeriod1>& lhs, const etl::chrono::duration<TRep2, TPeriod2>& rhs) ETL_NOEXCEPT
469 {
470 return rhs < lhs;
471 }
472
473 //***********************************************************************
475 //***********************************************************************
476 template <typename TRep1, typename TPeriod1, typename TRep2, typename TPeriod2>
477 ETL_CONSTEXPR14 bool operator>=(const etl::chrono::duration<TRep1, TPeriod1>& lhs, const etl::chrono::duration<TRep2, TPeriod2>& rhs) ETL_NOEXCEPT
478 {
479 return !(lhs < rhs);
480 }
481
482 //***********************************************************************
484 //***********************************************************************
485#if ETL_USING_CPP20
486 template <typename TRep1, typename TPeriod1, typename TRep2, typename TPeriod2>
487 [[nodiscard]]
488 constexpr auto operator<=>(const etl::chrono::duration<TRep1, TPeriod1>& lhs, const etl::chrono::duration<TRep2, TPeriod2>& rhs) ETL_NOEXCEPT
489 {
490 using common_t = typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, etl::chrono::duration<TRep2, TPeriod2> >::type;
491
492 common_t l = etl::chrono::duration_cast<common_t>(lhs);
493 common_t r = etl::chrono::duration_cast<common_t>(rhs);
494
495 return (l.count() <=> r.count());
496 }
497#endif
498
499 //***********************************************************************
501 //***********************************************************************
502 template <typename TRep1, typename TPeriod1, typename TRep2, typename TPeriod2>
503 ETL_CONSTEXPR14 typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, etl::chrono::duration<TRep2, TPeriod2> >::type
505 {
506 // Determine the common type of the two durations.
507 using common_duration = typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, etl::chrono::duration<TRep2, TPeriod2>>::type;
508
509 // Convert both durations to the common type.
510 common_duration lhs_converted = etl::chrono::duration_cast<common_duration>(lhs);
511 common_duration rhs_converted = etl::chrono::duration_cast<common_duration>(rhs);
512
513 // Return the sum of the two converted durations.
514 return common_duration(lhs_converted.count() + rhs_converted.count());
515 }
516
517 //***********************************************************************
519 //***********************************************************************
520 template <typename TRep1, typename TPeriod1, typename TRep2, typename TPeriod2>
521 ETL_CONSTEXPR14 typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, etl::chrono::duration<TRep2, TPeriod2> >::type
523 {
524 // Determine the common type of the two durations.
525 using common_duration = typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, etl::chrono::duration<TRep2, TPeriod2>>::type;
526
527 // Convert both durations to the common type.
528 common_duration lhs_converted = etl::chrono::duration_cast<common_duration>(lhs);
529 common_duration rhs_converted = etl::chrono::duration_cast<common_duration>(rhs);
530
531 // Return the difference of the two converted durations.
532 return common_duration(lhs_converted.count() - rhs_converted.count());
533 }
534
535 //***********************************************************************
537 //***********************************************************************
538 template <typename TRep1, typename TPeriod1, typename TRep2>
539 ETL_CONSTEXPR14 typename enable_if< !etl::is_specialization<TRep2, etl::chrono::duration>::value,
541 operator*(const etl::chrono::duration<TRep1, TPeriod1>& lhs, const TRep2& rhs) ETL_NOEXCEPT
542 {
543 using common_rep = typename etl::common_type<TRep1, TRep2>::type;
544 using result_duration = etl::chrono::duration<common_rep, TPeriod1>;
545
546 // Multiply the count of the duration by the scalar value
547 return result_duration(static_cast<common_rep>(lhs.count()) * static_cast<common_rep>(rhs));
548 }
549
550 //***********************************************************************
552 //***********************************************************************
553 template <typename TRep1, typename TRep2, typename TPeriod2>
555 operator*(const TRep1& lhs, const etl::chrono::duration<TRep2, TPeriod2>& rhs) ETL_NOEXCEPT
556 {
557 using common_rep = typename etl::common_type<TRep1, TRep2>::type;
558 using result_duration = etl::chrono::duration<common_rep, TPeriod2>;
559
560 // Multiply the count of the duration by the scalar value
561 return result_duration(static_cast<common_rep>(rhs.count()) * static_cast<common_rep>(lhs));
562 }
563
564 //***********************************************************************
566 //***********************************************************************
567 template <typename TRep1, typename TPeriod1, typename TRep2>
568 ETL_CONSTEXPR14 typename enable_if< !etl::is_specialization<TRep2, etl::chrono::duration>::value,
570 operator/(const etl::chrono::duration<TRep1, TPeriod1>& lhs, const TRep2& rhs) ETL_NOEXCEPT
571 {
572 using common_rep = typename etl::common_type<TRep1, TRep2>::type;
573 using result_duration = etl::chrono::duration<common_rep, TPeriod1>;
574
575 // Divide the count of the duration by the scalar value
576 return result_duration(static_cast<common_rep>(lhs.count()) / static_cast<common_rep>(rhs));
577 }
578
579 //***********************************************************************
581 //***********************************************************************
582 template <typename TRep1, typename TPeriod1, typename TRep2, typename TPeriod2>
583 ETL_CONSTEXPR14 typename etl::common_type<TRep1, TRep2>::type operator/(const etl::chrono::duration<TRep1, TPeriod1>& lhs,
584 const etl::chrono::duration<TRep2, TPeriod2>& rhs) ETL_NOEXCEPT
585 {
586 // Determine the common type of the two durations.
587 using common_duration = typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, etl::chrono::duration<TRep2, TPeriod2>>::type;
588
589 common_duration lhs_converted = etl::chrono::duration_cast<common_duration>(lhs);
590 common_duration rhs_converted = etl::chrono::duration_cast<common_duration>(rhs);
591
592 return typename etl::common_type<TRep1, TRep2>::type(lhs_converted.count() / rhs_converted.count());
593 }
594
595 //***********************************************************************
597 //***********************************************************************
598 template <typename TRep1, typename TPeriod1, typename TRep2>
600 operator%(const etl::chrono::duration<TRep1, TPeriod1>& lhs, const TRep2& rhs) ETL_NOEXCEPT
601 {
602 using common_rep = typename etl::common_type<TRep1, TRep2>::type;
604
605 // Mod the count of the duration by the scalar value
606 return common_dur(static_cast<common_rep>(lhs.count()) % rhs);
607 }
608
609 //***********************************************************************
611 //***********************************************************************
612 template <typename TRep1, typename TPeriod1, typename TRep2, typename TPeriod2>
613 ETL_CONSTEXPR14 typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, etl::chrono::duration<TRep2, TPeriod2>>::type
615 {
616 // Determine the common type of the two durations.
617 using common_duration = typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, etl::chrono::duration<TRep2, TPeriod2>>::type;
618
619 common_duration lhs_converted = etl::chrono::duration_cast<common_duration>(lhs);
620 common_duration rhs_converted = etl::chrono::duration_cast<common_duration>(rhs);
621
622 return common_duration(lhs_converted.count() % rhs_converted.count());
623 }
624
625 //***********************************************************************
627 //***********************************************************************
628 template <typename TToDuration, typename TRep, typename TPeriod>
629 ETL_CONSTEXPR14 typename etl::enable_if< etl::is_specialization<TToDuration, etl::chrono::duration>::value, TToDuration>::type
631 {
632 TToDuration result = etl::chrono::duration_cast<TToDuration>(d);
633
634 if (result > d)
635 {
636 --result;
637 }
638
639 return result;
640 }
641
642 //***********************************************************************
644 //***********************************************************************
645 template <typename TToDuration, typename TRep, typename TPeriod>
646 ETL_CONSTEXPR14 typename etl::enable_if< etl::is_specialization<TToDuration, etl::chrono::duration>::value, TToDuration>::type
648 {
649 TToDuration result = etl::chrono::duration_cast<TToDuration>(d);
650
651 if (result < d)
652 {
653 ++result;
654 }
655
656 return result;
657 }
658
659 //***********************************************************************
662 //***********************************************************************
663 template <typename TToDuration, typename TRep, typename TPeriod>
664 ETL_CONSTEXPR14 typename etl::enable_if< etl::is_specialization<TToDuration, etl::chrono::duration>::value, TToDuration>::type
666 {
667 // Convert the input duration to the target duration type
668 TToDuration lower = floor<TToDuration>(d);
669 TToDuration upper = ceil<TToDuration>(lower + TToDuration(1));
670
671 auto lower_diff = d - lower;
672 auto upper_diff = upper - d;
673
674 if ((lower_diff < upper_diff) || ((lower_diff == upper_diff) && etl::is_even(lower.count())))
675 {
676 return lower;
677 }
678 else
679 {
680 return upper;
681 }
682 }
683
684 //***********************************************************************
686 //***********************************************************************
687 template <class TRep, class TPeriod, typename = etl::enable_if_t<etl::numeric_limits<TRep>::is_signed>>
689 {
690 return d.count() >= 0 ? +d : -d;
691 }
692} // namespace etl
693
694#if ETL_HAS_CHRONO_LITERALS_DURATION
695namespace etl
696{
697 inline namespace literals
698 {
699 inline namespace chrono_literals
700 {
701 //***********************************************************************
703 //***********************************************************************
704 #if ETL_USING_VERBOSE_CHRONO_LITERALS
705 inline ETL_CONSTEXPR14 etl::chrono::hours operator""_hours(unsigned long long h) ETL_NOEXCEPT
706 #else
707 inline ETL_CONSTEXPR14 etl::chrono::hours operator""_h(unsigned long long h) ETL_NOEXCEPT
708 #endif
709 {
710 return etl::chrono::hours(static_cast<etl::chrono::hours::rep>(h));
711 }
712
713 //***********************************************************************
715 //***********************************************************************
716 #if ETL_USING_VERBOSE_CHRONO_LITERALS
717 inline ETL_CONSTEXPR14 etl::chrono::duration<double, ratio<3600>> operator""_hours(long double h) ETL_NOEXCEPT
718 #else
719 inline ETL_CONSTEXPR14 etl::chrono::duration<double, ratio<3600>> operator""_h(long double h) ETL_NOEXCEPT
720 #endif
721 {
723 }
724
725 //***********************************************************************
727 //***********************************************************************
728 #if ETL_USING_VERBOSE_CHRONO_LITERALS
729 inline ETL_CONSTEXPR14 etl::chrono::minutes operator""_minutes(unsigned long long m) ETL_NOEXCEPT
730 #else
731 inline ETL_CONSTEXPR14 etl::chrono::minutes operator""_min(unsigned long long m) ETL_NOEXCEPT
732 #endif
733 {
734 return etl::chrono::minutes(static_cast<etl::chrono::minutes::rep>(m));
735 }
736
737 //***********************************************************************
739 //***********************************************************************
740 #if ETL_USING_VERBOSE_CHRONO_LITERALS
741 inline ETL_CONSTEXPR14 etl::chrono::duration<double, ratio<60>> operator""_minutes(long double m) ETL_NOEXCEPT
742 #else
743 inline ETL_CONSTEXPR14 etl::chrono::duration<double, ratio<60>> operator""_min(long double m) ETL_NOEXCEPT
744 #endif
745 {
747 }
748
749 //***********************************************************************
751 //***********************************************************************
752 #if ETL_USING_VERBOSE_CHRONO_LITERALS
753 inline ETL_CONSTEXPR14 etl::chrono::seconds operator""_seconds(unsigned long long s) ETL_NOEXCEPT
754 #else
755 inline ETL_CONSTEXPR14 etl::chrono::seconds operator""_s(unsigned long long s) ETL_NOEXCEPT
756 #endif
757 {
758 return etl::chrono::seconds(static_cast<etl::chrono::seconds::rep>(s));
759 }
760
761 //***********************************************************************
763 //***********************************************************************
764 #if ETL_USING_VERBOSE_CHRONO_LITERALS
765 inline ETL_CONSTEXPR14 etl::chrono::duration<double> operator""_seconds(long double s) ETL_NOEXCEPT
766 #else
767 inline ETL_CONSTEXPR14 etl::chrono::duration<double> operator""_s(long double s) ETL_NOEXCEPT
768 #endif
769 {
771 }
772
773 //***********************************************************************
775 //***********************************************************************
776 #if ETL_USING_VERBOSE_CHRONO_LITERALS
777 inline ETL_CONSTEXPR14 etl::chrono::milliseconds operator""_milliseconds(unsigned long long s) ETL_NOEXCEPT
778 #else
779 inline ETL_CONSTEXPR14 etl::chrono::milliseconds operator""_ms(unsigned long long s) ETL_NOEXCEPT
780 #endif
781 {
782 return etl::chrono::milliseconds(static_cast<etl::chrono::milliseconds::rep>(s));
783 }
784
785 //***********************************************************************
787 //***********************************************************************
788 #if ETL_USING_VERBOSE_CHRONO_LITERALS
789 inline ETL_CONSTEXPR14 etl::chrono::duration<double, milli> operator""_milliseconds(long double s) ETL_NOEXCEPT
790 #else
791 inline ETL_CONSTEXPR14 etl::chrono::duration<double, milli> operator""_ms(long double s) ETL_NOEXCEPT
792 #endif
793 {
795 }
796
797 #if (INT_MAX >= INT32_MAX)
798 //***********************************************************************
800 //***********************************************************************
801 #if ETL_USING_VERBOSE_CHRONO_LITERALS
802 inline ETL_CONSTEXPR14 etl::chrono::microseconds operator""_microseconds(unsigned long long s) ETL_NOEXCEPT
803 #else
804 inline ETL_CONSTEXPR14 etl::chrono::microseconds operator""_us(unsigned long long s) ETL_NOEXCEPT
805 #endif
806 {
807 return etl::chrono::microseconds(static_cast<etl::chrono::microseconds::rep>(s));
808 }
809
810 //***********************************************************************
812 //***********************************************************************
813 #if ETL_USING_VERBOSE_CHRONO_LITERALS
814 inline ETL_CONSTEXPR14 etl::chrono::duration<double, micro> operator""_microseconds(long double s) ETL_NOEXCEPT
815 #else
816 inline ETL_CONSTEXPR14 etl::chrono::duration<double, micro> operator""_us(long double s) ETL_NOEXCEPT
817 #endif
818 {
820 }
821
822 //***********************************************************************
824 //***********************************************************************
825 #if ETL_USING_VERBOSE_CHRONO_LITERALS
826 inline ETL_CONSTEXPR14 etl::chrono::nanoseconds operator""_nanoseconds(unsigned long long s) ETL_NOEXCEPT
827 #else
828 inline ETL_CONSTEXPR14 etl::chrono::nanoseconds operator""_ns(unsigned long long s) ETL_NOEXCEPT
829 #endif
830 {
831 return etl::chrono::nanoseconds(static_cast<etl::chrono::nanoseconds::rep>(s));
832 }
833
834 //***********************************************************************
836 //***********************************************************************
837 #if ETL_USING_VERBOSE_CHRONO_LITERALS
838 inline ETL_CONSTEXPR14 etl::chrono::duration<double, nano> operator""_nanoseconds(long double s) ETL_NOEXCEPT
839 #else
840 inline ETL_CONSTEXPR14 etl::chrono::duration<double, nano> operator""_ns(long double s) ETL_NOEXCEPT
841 #endif
842 {
844 }
845 #endif
846 } // namespace chrono_literals
847 } // namespace literals
848} // namespace etl
849#endif
duration
Definition duration.h:108
ETL_CONSTEXPR14 int compare(const duration< TRep2, TPeriod2 > &other) const ETL_NOEXCEPT
Definition duration.h:293
Definition limits.h:1715
etl::chrono::duration< int64_t, etl::nano > nanoseconds
Duration types.
Definition duration.h:319
ETL_CONSTEXPR14 TToDuration duration_cast(const etl::chrono::duration< TRep, TPeriod > &d) ETL_NOEXCEPT
duration_cast
Definition duration.h:343
ETL_CONSTEXPR etl::enable_if< etl::is_integral< T >::value, bool >::type is_even(T value)
Definition binary.h:2188
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR14 bool operator==(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1081
ETL_CONSTEXPR14 etl::enable_if< etl::is_specialization< TToDuration, etl::chrono::duration >::value, TToDuration >::type ceil(const etl::chrono::duration< TRep, TPeriod > &d) ETL_NOEXCEPT
Rounds up a duration to the nearest higher precision.
Definition duration.h:647
ETL_CONSTEXPR14 etl::circular_iterator< TIterator > operator-(etl::circular_iterator< TIterator > &lhs, typename etl::iterator_traits< TIterator >::difference_type offset)
Definition circular_iterator.h:675
bool operator>(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1133
ETL_CONSTEXPR14 enable_if<!etl::is_specialization< TRep2, etl::chrono::duration >::value, etl::chrono::duration< typenameetl::common_type< TRep1, TRep2 >::type, TPeriod1 > >::type operator/(const etl::chrono::duration< TRep1, TPeriod1 > &lhs, const TRep2 &rhs) ETL_NOEXCEPT
Operator /.
Definition duration.h:570
ETL_CONSTEXPR14 etl::chrono::duration< typename etl::common_type< TRep1, TRep2 >::type, TPeriod1 > operator%(const etl::chrono::duration< TRep1, TPeriod1 > &lhs, const TRep2 &rhs) ETL_NOEXCEPT
Operator %.
Definition duration.h:600
ETL_CONSTEXPR14 etl::chrono::duration< TRep, TPeriod > abs(etl::chrono::duration< TRep, TPeriod > d) ETL_NOEXCEPT
Returns the absolute value of a duration.
Definition duration.h:688
bool operator>=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1147
ETL_CONSTEXPR14 etl::enable_if< etl::is_specialization< TToDuration, etl::chrono::duration >::value, TToDuration >::type floor(const etl::chrono::duration< TRep, TPeriod > &d) ETL_NOEXCEPT
Rounds down a duration to the nearest lower precision.
Definition duration.h:630
ETL_CONSTEXPR14 bool operator!=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1093
ETL_CONSTEXPR14 etl::enable_if< etl::is_specialization< TToDuration, etl::chrono::duration >::value, TToDuration >::type round(const etl::chrono::duration< TRep, TPeriod > &d) ETL_NOEXCEPT
Definition duration.h:665
ETL_CONSTEXPR14 enable_if<!etl::is_specialization< TRep2, etl::chrono::duration >::value, etl::chrono::duration< typenameetl::common_type< TRep1, TRep2 >::type, TPeriod1 > >::type operator*(const etl::chrono::duration< TRep1, TPeriod1 > &lhs, const TRep2 &rhs) ETL_NOEXCEPT
Operator *.
Definition duration.h:541
ETL_CONSTEXPR14 etl::circular_iterator< TIterator > operator+(etl::circular_iterator< TIterator > &lhs, typename etl::iterator_traits< TIterator >::difference_type offset)
Definition circular_iterator.h:662
bool operator<(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1106
bool operator<=(const etl::array< T, SIZE > &lhs, const etl::array< T, SIZE > &rhs)
Definition array.h:1120
duration_values
Definition duration.h:74
Definition duration.h:51
Definition duration.h:64
ratio
Definition ratio.h:53