1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef _LIBCPP___CHRONO_HH_MM_SS_H
11 #define _LIBCPP___CHRONO_HH_MM_SS_H
12
13 #include <__chrono/duration.h>
14 #include <__chrono/time_point.h>
15 #include <__config>
16 #include <ratio>
17 #include <type_traits>
18
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 # pragma GCC system_header
21 #endif
22
23 #if _LIBCPP_STD_VER > 17
24
25 _LIBCPP_BEGIN_NAMESPACE_STD
26
27 namespace chrono
28 {
29
30 template <class _Duration>
31 class hh_mm_ss
32 {
33 private:
34 static_assert(__is_duration<_Duration>::value, "template parameter of hh_mm_ss must be a std::chrono::duration");
35 using __CommonType = common_type_t<_Duration, chrono::seconds>;
36
__pow10(unsigned __exp)37 _LIBCPP_HIDE_FROM_ABI static constexpr uint64_t __pow10(unsigned __exp)
38 {
39 uint64_t __ret = 1;
40 for (unsigned __i = 0; __i < __exp; ++__i)
41 __ret *= 10U;
42 return __ret;
43 }
44
45 _LIBCPP_HIDE_FROM_ABI static constexpr unsigned __width(uint64_t __n, uint64_t __d = 10, unsigned __w = 0)
46 {
47 if (__n >= 2 && __d != 0 && __w < 19)
48 return 1 + __width(__n, __d % __n * 10, __w+1);
49 return 0;
50 }
51
52 public:
53 _LIBCPP_HIDE_FROM_ABI static unsigned constexpr fractional_width = __width(__CommonType::period::den) < 19 ?
54 __width(__CommonType::period::den) : 6u;
55 using precision = duration<typename __CommonType::rep, ratio<1, __pow10(fractional_width)>>;
56
hh_mm_ss()57 _LIBCPP_HIDE_FROM_ABI constexpr hh_mm_ss() noexcept : hh_mm_ss{_Duration::zero()} {}
58
hh_mm_ss(_Duration __d)59 _LIBCPP_HIDE_FROM_ABI constexpr explicit hh_mm_ss(_Duration __d) noexcept :
60 __is_neg_(__d < _Duration(0)),
61 __h_(chrono::duration_cast<chrono::hours> (chrono::abs(__d))),
62 __m_(chrono::duration_cast<chrono::minutes>(chrono::abs(__d) - hours())),
63 __s_(chrono::duration_cast<chrono::seconds>(chrono::abs(__d) - hours() - minutes())),
64 __f_(chrono::duration_cast<precision> (chrono::abs(__d) - hours() - minutes() - seconds()))
65 {}
66
is_negative()67 _LIBCPP_HIDE_FROM_ABI constexpr bool is_negative() const noexcept { return __is_neg_; }
hours()68 _LIBCPP_HIDE_FROM_ABI constexpr chrono::hours hours() const noexcept { return __h_; }
minutes()69 _LIBCPP_HIDE_FROM_ABI constexpr chrono::minutes minutes() const noexcept { return __m_; }
seconds()70 _LIBCPP_HIDE_FROM_ABI constexpr chrono::seconds seconds() const noexcept { return __s_; }
subseconds()71 _LIBCPP_HIDE_FROM_ABI constexpr precision subseconds() const noexcept { return __f_; }
72
to_duration()73 _LIBCPP_HIDE_FROM_ABI constexpr precision to_duration() const noexcept
74 {
75 auto __dur = __h_ + __m_ + __s_ + __f_;
76 return __is_neg_ ? -__dur : __dur;
77 }
78
precision()79 _LIBCPP_HIDE_FROM_ABI constexpr explicit operator precision() const noexcept { return to_duration(); }
80
81 private:
82 bool __is_neg_;
83 chrono::hours __h_;
84 chrono::minutes __m_;
85 chrono::seconds __s_;
86 precision __f_;
87 };
88
is_am(const hours & __h)89 _LIBCPP_HIDE_FROM_ABI constexpr bool is_am(const hours& __h) noexcept { return __h >= hours( 0) && __h < hours(12); }
is_pm(const hours & __h)90 _LIBCPP_HIDE_FROM_ABI constexpr bool is_pm(const hours& __h) noexcept { return __h >= hours(12) && __h < hours(24); }
91
make12(const hours & __h)92 _LIBCPP_HIDE_FROM_ABI constexpr hours make12(const hours& __h) noexcept
93 {
94 if (__h == hours( 0)) return hours(12);
95 else if (__h <= hours(12)) return __h;
96 else return __h - hours(12);
97 }
98
make24(const hours & __h,bool __is_pm)99 _LIBCPP_HIDE_FROM_ABI constexpr hours make24(const hours& __h, bool __is_pm) noexcept
100 {
101 if (__is_pm)
102 return __h == hours(12) ? __h : __h + hours(12);
103 else
104 return __h == hours(12) ? hours(0) : __h;
105 }
106 } // namespace chrono
107
108 _LIBCPP_END_NAMESPACE_STD
109
110 #endif // _LIBCPP_STD_VER > 17
111
112 #endif // _LIBCPP___CHRONO_HH_MM_SS_H
113