File indexing completed on 2026-05-03 08:13:15
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___CHRONO_HH_MM_SS_H
0011 #define _LIBCPP___CHRONO_HH_MM_SS_H
0012
0013 #include <__chrono/duration.h>
0014 #include <__chrono/time_point.h>
0015 #include <__config>
0016 #include <__type_traits/common_type.h>
0017 #include <ratio>
0018
0019 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0020 # pragma GCC system_header
0021 #endif
0022
0023 #if _LIBCPP_STD_VER >= 20
0024
0025 _LIBCPP_BEGIN_NAMESPACE_STD
0026
0027 namespace chrono {
0028
0029 template <class _Duration>
0030 class hh_mm_ss {
0031 private:
0032 static_assert(__is_duration_v<_Duration>, "template parameter of hh_mm_ss must be a std::chrono::duration");
0033 using __CommonType _LIBCPP_NODEBUG = common_type_t<_Duration, chrono::seconds>;
0034
0035 _LIBCPP_HIDE_FROM_ABI static constexpr uint64_t __pow10(unsigned __exp) {
0036 uint64_t __ret = 1;
0037 for (unsigned __i = 0; __i < __exp; ++__i)
0038 __ret *= 10U;
0039 return __ret;
0040 }
0041
0042 _LIBCPP_HIDE_FROM_ABI static constexpr unsigned __width(uint64_t __n, uint64_t __d = 10, unsigned __w = 0) {
0043 if (__n >= 2 && __d != 0 && __w < 19)
0044 return 1 + __width(__n, __d % __n * 10, __w + 1);
0045 return 0;
0046 }
0047
0048 public:
0049 _LIBCPP_HIDE_FROM_ABI static unsigned constexpr fractional_width =
0050 __width(__CommonType::period::den) < 19 ? __width(__CommonType::period::den) : 6u;
0051 using precision = duration<typename __CommonType::rep, ratio<1, __pow10(fractional_width)>>;
0052
0053 _LIBCPP_HIDE_FROM_ABI constexpr hh_mm_ss() noexcept : hh_mm_ss{_Duration::zero()} {}
0054
0055 _LIBCPP_HIDE_FROM_ABI constexpr explicit hh_mm_ss(_Duration __d) noexcept
0056 : __is_neg_(__d < _Duration(0)),
0057 __h_(chrono::duration_cast<chrono::hours>(chrono::abs(__d))),
0058 __m_(chrono::duration_cast<chrono::minutes>(chrono::abs(__d) - hours())),
0059 __s_(chrono::duration_cast<chrono::seconds>(chrono::abs(__d) - hours() - minutes())),
0060 __f_(chrono::duration_cast<precision>(chrono::abs(__d) - hours() - minutes() - seconds())) {}
0061
0062 _LIBCPP_HIDE_FROM_ABI constexpr bool is_negative() const noexcept { return __is_neg_; }
0063 _LIBCPP_HIDE_FROM_ABI constexpr chrono::hours hours() const noexcept { return __h_; }
0064 _LIBCPP_HIDE_FROM_ABI constexpr chrono::minutes minutes() const noexcept { return __m_; }
0065 _LIBCPP_HIDE_FROM_ABI constexpr chrono::seconds seconds() const noexcept { return __s_; }
0066 _LIBCPP_HIDE_FROM_ABI constexpr precision subseconds() const noexcept { return __f_; }
0067
0068 _LIBCPP_HIDE_FROM_ABI constexpr precision to_duration() const noexcept {
0069 auto __dur = __h_ + __m_ + __s_ + __f_;
0070 return __is_neg_ ? -__dur : __dur;
0071 }
0072
0073 _LIBCPP_HIDE_FROM_ABI constexpr explicit operator precision() const noexcept { return to_duration(); }
0074
0075 private:
0076 bool __is_neg_;
0077 chrono::hours __h_;
0078 chrono::minutes __m_;
0079 chrono::seconds __s_;
0080 precision __f_;
0081 };
0082 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(hh_mm_ss);
0083
0084 _LIBCPP_HIDE_FROM_ABI inline constexpr bool is_am(const hours& __h) noexcept {
0085 return __h >= hours(0) && __h < hours(12);
0086 }
0087 _LIBCPP_HIDE_FROM_ABI inline constexpr bool is_pm(const hours& __h) noexcept {
0088 return __h >= hours(12) && __h < hours(24);
0089 }
0090
0091 _LIBCPP_HIDE_FROM_ABI inline constexpr hours make12(const hours& __h) noexcept {
0092 if (__h == hours(0))
0093 return hours(12);
0094 else if (__h <= hours(12))
0095 return __h;
0096 else
0097 return __h - hours(12);
0098 }
0099
0100 _LIBCPP_HIDE_FROM_ABI inline constexpr hours make24(const hours& __h, bool __is_pm) noexcept {
0101 if (__is_pm)
0102 return __h == hours(12) ? __h : __h + hours(12);
0103 else
0104 return __h == hours(12) ? hours(0) : __h;
0105 }
0106 }
0107
0108 _LIBCPP_END_NAMESPACE_STD
0109
0110 #endif
0111
0112 #endif