Path: blob/main/contrib/llvm-project/libcxx/include/__chrono/hh_mm_ss.h
35262 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___CHRONO_HH_MM_SS_H10#define _LIBCPP___CHRONO_HH_MM_SS_H1112#include <__chrono/duration.h>13#include <__chrono/time_point.h>14#include <__config>15#include <__type_traits/common_type.h>16#include <ratio>1718#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)19# pragma GCC system_header20#endif2122#if _LIBCPP_STD_VER >= 202324_LIBCPP_BEGIN_NAMESPACE_STD2526namespace chrono {2728template <class _Duration>29class hh_mm_ss {30private:31static_assert(__is_duration<_Duration>::value, "template parameter of hh_mm_ss must be a std::chrono::duration");32using __CommonType = common_type_t<_Duration, chrono::seconds>;3334_LIBCPP_HIDE_FROM_ABI static constexpr uint64_t __pow10(unsigned __exp) {35uint64_t __ret = 1;36for (unsigned __i = 0; __i < __exp; ++__i)37__ret *= 10U;38return __ret;39}4041_LIBCPP_HIDE_FROM_ABI static constexpr unsigned __width(uint64_t __n, uint64_t __d = 10, unsigned __w = 0) {42if (__n >= 2 && __d != 0 && __w < 19)43return 1 + __width(__n, __d % __n * 10, __w + 1);44return 0;45}4647public:48_LIBCPP_HIDE_FROM_ABI static unsigned constexpr fractional_width =49__width(__CommonType::period::den) < 19 ? __width(__CommonType::period::den) : 6u;50using precision = duration<typename __CommonType::rep, ratio<1, __pow10(fractional_width)>>;5152_LIBCPP_HIDE_FROM_ABI constexpr hh_mm_ss() noexcept : hh_mm_ss{_Duration::zero()} {}5354_LIBCPP_HIDE_FROM_ABI constexpr explicit hh_mm_ss(_Duration __d) noexcept55: __is_neg_(__d < _Duration(0)),56__h_(chrono::duration_cast<chrono::hours>(chrono::abs(__d))),57__m_(chrono::duration_cast<chrono::minutes>(chrono::abs(__d) - hours())),58__s_(chrono::duration_cast<chrono::seconds>(chrono::abs(__d) - hours() - minutes())),59__f_(chrono::duration_cast<precision>(chrono::abs(__d) - hours() - minutes() - seconds())) {}6061_LIBCPP_HIDE_FROM_ABI constexpr bool is_negative() const noexcept { return __is_neg_; }62_LIBCPP_HIDE_FROM_ABI constexpr chrono::hours hours() const noexcept { return __h_; }63_LIBCPP_HIDE_FROM_ABI constexpr chrono::minutes minutes() const noexcept { return __m_; }64_LIBCPP_HIDE_FROM_ABI constexpr chrono::seconds seconds() const noexcept { return __s_; }65_LIBCPP_HIDE_FROM_ABI constexpr precision subseconds() const noexcept { return __f_; }6667_LIBCPP_HIDE_FROM_ABI constexpr precision to_duration() const noexcept {68auto __dur = __h_ + __m_ + __s_ + __f_;69return __is_neg_ ? -__dur : __dur;70}7172_LIBCPP_HIDE_FROM_ABI constexpr explicit operator precision() const noexcept { return to_duration(); }7374private:75bool __is_neg_;76chrono::hours __h_;77chrono::minutes __m_;78chrono::seconds __s_;79precision __f_;80};81_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(hh_mm_ss);8283_LIBCPP_HIDE_FROM_ABI inline constexpr bool is_am(const hours& __h) noexcept {84return __h >= hours(0) && __h < hours(12);85}86_LIBCPP_HIDE_FROM_ABI inline constexpr bool is_pm(const hours& __h) noexcept {87return __h >= hours(12) && __h < hours(24);88}8990_LIBCPP_HIDE_FROM_ABI inline constexpr hours make12(const hours& __h) noexcept {91if (__h == hours(0))92return hours(12);93else if (__h <= hours(12))94return __h;95else96return __h - hours(12);97}9899_LIBCPP_HIDE_FROM_ABI inline constexpr hours make24(const hours& __h, bool __is_pm) noexcept {100if (__is_pm)101return __h == hours(12) ? __h : __h + hours(12);102else103return __h == hours(12) ? hours(0) : __h;104}105} // namespace chrono106107_LIBCPP_END_NAMESPACE_STD108109#endif // _LIBCPP_STD_VER >= 20110111#endif // _LIBCPP___CHRONO_HH_MM_SS_H112113114