hh_mm_ss.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #ifndef _LIBCPP___CHRONO_HH_MM_SS_H
  10. #define _LIBCPP___CHRONO_HH_MM_SS_H
  11. #include <__chrono/duration.h>
  12. #include <__chrono/time_point.h>
  13. #include <__config>
  14. #include <ratio>
  15. #include <type_traits>
  16. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  17. # pragma GCC system_header
  18. #endif
  19. #if _LIBCPP_STD_VER > 17
  20. _LIBCPP_BEGIN_NAMESPACE_STD
  21. namespace chrono
  22. {
  23. template <class _Duration>
  24. class hh_mm_ss
  25. {
  26. private:
  27. static_assert(__is_duration<_Duration>::value, "template parameter of hh_mm_ss must be a std::chrono::duration");
  28. using __CommonType = common_type_t<_Duration, chrono::seconds>;
  29. _LIBCPP_HIDE_FROM_ABI static constexpr uint64_t __pow10(unsigned __exp)
  30. {
  31. uint64_t __ret = 1;
  32. for (unsigned __i = 0; __i < __exp; ++__i)
  33. __ret *= 10U;
  34. return __ret;
  35. }
  36. _LIBCPP_HIDE_FROM_ABI static constexpr unsigned __width(uint64_t __n, uint64_t __d = 10, unsigned __w = 0)
  37. {
  38. if (__n >= 2 && __d != 0 && __w < 19)
  39. return 1 + __width(__n, __d % __n * 10, __w+1);
  40. return 0;
  41. }
  42. public:
  43. _LIBCPP_HIDE_FROM_ABI static unsigned constexpr fractional_width = __width(__CommonType::period::den) < 19 ?
  44. __width(__CommonType::period::den) : 6u;
  45. using precision = duration<typename __CommonType::rep, ratio<1, __pow10(fractional_width)>>;
  46. _LIBCPP_HIDE_FROM_ABI constexpr hh_mm_ss() noexcept : hh_mm_ss{_Duration::zero()} {}
  47. _LIBCPP_HIDE_FROM_ABI constexpr explicit hh_mm_ss(_Duration __d) noexcept :
  48. __is_neg_(__d < _Duration(0)),
  49. __h_(chrono::duration_cast<chrono::hours> (chrono::abs(__d))),
  50. __m_(chrono::duration_cast<chrono::minutes>(chrono::abs(__d) - hours())),
  51. __s_(chrono::duration_cast<chrono::seconds>(chrono::abs(__d) - hours() - minutes())),
  52. __f_(chrono::duration_cast<precision> (chrono::abs(__d) - hours() - minutes() - seconds()))
  53. {}
  54. _LIBCPP_HIDE_FROM_ABI constexpr bool is_negative() const noexcept { return __is_neg_; }
  55. _LIBCPP_HIDE_FROM_ABI constexpr chrono::hours hours() const noexcept { return __h_; }
  56. _LIBCPP_HIDE_FROM_ABI constexpr chrono::minutes minutes() const noexcept { return __m_; }
  57. _LIBCPP_HIDE_FROM_ABI constexpr chrono::seconds seconds() const noexcept { return __s_; }
  58. _LIBCPP_HIDE_FROM_ABI constexpr precision subseconds() const noexcept { return __f_; }
  59. _LIBCPP_HIDE_FROM_ABI constexpr precision to_duration() const noexcept
  60. {
  61. auto __dur = __h_ + __m_ + __s_ + __f_;
  62. return __is_neg_ ? -__dur : __dur;
  63. }
  64. _LIBCPP_HIDE_FROM_ABI constexpr explicit operator precision() const noexcept { return to_duration(); }
  65. private:
  66. bool __is_neg_;
  67. chrono::hours __h_;
  68. chrono::minutes __m_;
  69. chrono::seconds __s_;
  70. precision __f_;
  71. };
  72. _LIBCPP_HIDE_FROM_ABI constexpr bool is_am(const hours& __h) noexcept { return __h >= hours( 0) && __h < hours(12); }
  73. _LIBCPP_HIDE_FROM_ABI constexpr bool is_pm(const hours& __h) noexcept { return __h >= hours(12) && __h < hours(24); }
  74. _LIBCPP_HIDE_FROM_ABI constexpr hours make12(const hours& __h) noexcept
  75. {
  76. if (__h == hours( 0)) return hours(12);
  77. else if (__h <= hours(12)) return __h;
  78. else return __h - hours(12);
  79. }
  80. _LIBCPP_HIDE_FROM_ABI constexpr hours make24(const hours& __h, bool __is_pm) noexcept
  81. {
  82. if (__is_pm)
  83. return __h == hours(12) ? __h : __h + hours(12);
  84. else
  85. return __h == hours(12) ? hours(0) : __h;
  86. }
  87. } // namespace chrono
  88. _LIBCPP_END_NAMESPACE_STD
  89. #endif // _LIBCPP_STD_VER > 17
  90. #endif // _LIBCPP___CHRONO_HH_MM_SS_H