| 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 | // For information see https://libcxx.llvm.org/DesignDocs/TimeZone.html |
| 11 | |
| 12 | #ifndef _LIBCPP___CHRONO_LEAP_SECOND_H |
| 13 | #define _LIBCPP___CHRONO_LEAP_SECOND_H |
| 14 | |
| 15 | #include <version> |
| 16 | // Enable the contents of the header only when libc++ was built with experimental features enabled. |
| 17 | #if _LIBCPP_HAS_EXPERIMENTAL_TZDB |
| 18 | |
| 19 | # include <__chrono/duration.h> |
| 20 | # include <__chrono/system_clock.h> |
| 21 | # include <__chrono/time_point.h> |
| 22 | # include <__compare/ordering.h> |
| 23 | # include <__compare/three_way_comparable.h> |
| 24 | # include <__config> |
| 25 | # include <__cstddef/size_t.h> |
| 26 | # include <__functional/hash.h> |
| 27 | # include <__utility/private_constructor_tag.h> |
| 28 | |
| 29 | # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 30 | # pragma GCC system_header |
| 31 | # endif |
| 32 | |
| 33 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 34 | |
| 35 | # if _LIBCPP_STD_VER >= 20 |
| 36 | |
| 37 | namespace chrono { |
| 38 | |
| 39 | class leap_second { |
| 40 | public: |
| 41 | [[nodiscard]] |
| 42 | _LIBCPP_HIDE_FROM_ABI explicit constexpr leap_second(__private_constructor_tag, sys_seconds __date, seconds __value) |
| 43 | : __date_(__date), __value_(__value) {} |
| 44 | |
| 45 | _LIBCPP_HIDE_FROM_ABI leap_second(const leap_second&) = default; |
| 46 | _LIBCPP_HIDE_FROM_ABI leap_second& operator=(const leap_second&) = default; |
| 47 | |
| 48 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr sys_seconds date() const noexcept { return __date_; } |
| 49 | |
| 50 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr seconds value() const noexcept { return __value_; } |
| 51 | |
| 52 | private: |
| 53 | sys_seconds __date_; |
| 54 | seconds __value_; |
| 55 | |
| 56 | // The function |
| 57 | // template<class Duration> |
| 58 | // requires three_way_comparable_with<sys_seconds, sys_time<Duration>> |
| 59 | // constexpr auto operator<=>(const leap_second& x, const sys_time<Duration>& y) noexcept; |
| 60 | // |
| 61 | // Has constraints that are recursive (LWG4139). The proposed resolution is |
| 62 | // to make the funcion a hidden friend. For consistency make this change for |
| 63 | // all comparison functions. |
| 64 | |
| 65 | _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const leap_second& __x, const leap_second& __y) { |
| 66 | return __x.date() == __y.date(); |
| 67 | } |
| 68 | |
| 69 | _LIBCPP_HIDE_FROM_ABI friend constexpr strong_ordering operator<=>(const leap_second& __x, const leap_second& __y) { |
| 70 | return __x.date() <=> __y.date(); |
| 71 | } |
| 72 | |
| 73 | template <class _Duration> |
| 74 | _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const leap_second& __x, const sys_time<_Duration>& __y) { |
| 75 | return __x.date() == __y; |
| 76 | } |
| 77 | |
| 78 | template <class _Duration> |
| 79 | _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(const leap_second& __x, const sys_time<_Duration>& __y) { |
| 80 | return __x.date() < __y; |
| 81 | } |
| 82 | |
| 83 | template <class _Duration> |
| 84 | _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(const sys_time<_Duration>& __x, const leap_second& __y) { |
| 85 | return __x < __y.date(); |
| 86 | } |
| 87 | |
| 88 | template <class _Duration> |
| 89 | _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(const leap_second& __x, const sys_time<_Duration>& __y) { |
| 90 | return __y < __x; |
| 91 | } |
| 92 | |
| 93 | template <class _Duration> |
| 94 | _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(const sys_time<_Duration>& __x, const leap_second& __y) { |
| 95 | return __y < __x; |
| 96 | } |
| 97 | |
| 98 | template <class _Duration> |
| 99 | _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(const leap_second& __x, const sys_time<_Duration>& __y) { |
| 100 | return !(__y < __x); |
| 101 | } |
| 102 | |
| 103 | template <class _Duration> |
| 104 | _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(const sys_time<_Duration>& __x, const leap_second& __y) { |
| 105 | return !(__y < __x); |
| 106 | } |
| 107 | |
| 108 | template <class _Duration> |
| 109 | _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(const leap_second& __x, const sys_time<_Duration>& __y) { |
| 110 | return !(__x < __y); |
| 111 | } |
| 112 | |
| 113 | template <class _Duration> |
| 114 | _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(const sys_time<_Duration>& __x, const leap_second& __y) { |
| 115 | return !(__x < __y); |
| 116 | } |
| 117 | |
| 118 | template <class _Duration> |
| 119 | requires three_way_comparable_with<sys_seconds, sys_time<_Duration>> |
| 120 | _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(const leap_second& __x, const sys_time<_Duration>& __y) { |
| 121 | return __x.date() <=> __y; |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | } // namespace chrono |
| 126 | |
| 127 | # if _LIBCPP_STD_VER >= 26 |
| 128 | |
| 129 | template <> |
| 130 | struct hash<chrono::leap_second> { |
| 131 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI static size_t operator()(const chrono::leap_second& __lp) noexcept { |
| 132 | return std::__hash_combine(hash<chrono::sys_seconds>{}(__lp.date()), hash<chrono::seconds>{}(__lp.value())); |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | # endif // _LIBCPP_STD_VER >= 26 |
| 137 | |
| 138 | # endif // _LIBCPP_STD_VER >= 20 |
| 139 | |
| 140 | _LIBCPP_END_NAMESPACE_STD |
| 141 | |
| 142 | #endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB |
| 143 | |
| 144 | #endif // _LIBCPP___CHRONO_LEAP_SECOND_H |
| 145 | |