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
37namespace chrono {
38
39class leap_second {
40public:
41 [[nodiscard]] explicit constexpr leap_second(__private_constructor_tag, sys_seconds __date, seconds __value)
42 : __date_(__date), __value_(__value) {}
43
44 leap_second(const leap_second&) = default;
45 leap_second& operator=(const leap_second&) = default;
46
47 [[nodiscard]] constexpr sys_seconds date() const noexcept { return __date_; }
48
49 [[nodiscard]] constexpr seconds value() const noexcept { return __value_; }
50
51private:
52 sys_seconds __date_;
53 seconds __value_;
54
55 // The function
56 // template<class Duration>
57 // requires three_way_comparable_with<sys_seconds, sys_time<Duration>>
58 // constexpr auto operator<=>(const leap_second& x, const sys_time<Duration>& y) noexcept;
59 //
60 // Has constraints that are recursive (LWG4139). The proposed resolution is
61 // to make the funcion a hidden friend. For consistency make this change for
62 // all comparison functions.
63
64 friend constexpr bool operator==(const leap_second& __x, const leap_second& __y) { return __x.date() == __y.date(); }
65
66 friend constexpr strong_ordering operator<=>(const leap_second& __x, const leap_second& __y) {
67 return __x.date() <=> __y.date();
68 }
69
70 template <class _Duration>
71 friend constexpr bool operator==(const leap_second& __x, const sys_time<_Duration>& __y) {
72 return __x.date() == __y;
73 }
74
75 template <class _Duration>
76 friend constexpr bool operator<(const leap_second& __x, const sys_time<_Duration>& __y) {
77 return __x.date() < __y;
78 }
79
80 template <class _Duration>
81 friend constexpr bool operator<(const sys_time<_Duration>& __x, const leap_second& __y) {
82 return __x < __y.date();
83 }
84
85 template <class _Duration>
86 friend constexpr bool operator>(const leap_second& __x, const sys_time<_Duration>& __y) {
87 return __y < __x;
88 }
89
90 template <class _Duration>
91 friend constexpr bool operator>(const sys_time<_Duration>& __x, const leap_second& __y) {
92 return __y < __x;
93 }
94
95 template <class _Duration>
96 friend constexpr bool operator<=(const leap_second& __x, const sys_time<_Duration>& __y) {
97 return !(__y < __x);
98 }
99
100 template <class _Duration>
101 friend constexpr bool operator<=(const sys_time<_Duration>& __x, const leap_second& __y) {
102 return !(__y < __x);
103 }
104
105 template <class _Duration>
106 friend constexpr bool operator>=(const leap_second& __x, const sys_time<_Duration>& __y) {
107 return !(__x < __y);
108 }
109
110 template <class _Duration>
111 friend constexpr bool operator>=(const sys_time<_Duration>& __x, const leap_second& __y) {
112 return !(__x < __y);
113 }
114
115 template <class _Duration>
116 requires three_way_comparable_with<sys_seconds, sys_time<_Duration>>
117 friend constexpr auto operator<=>(const leap_second& __x, const sys_time<_Duration>& __y) {
118 return __x.date() <=> __y;
119 }
120};
121
122} // namespace chrono
123
124# if _LIBCPP_STD_VER >= 26
125
126template <>
127struct hash<chrono::leap_second> {
128 [[nodiscard]] static size_t operator()(const chrono::leap_second& __lp) noexcept {
129 return std::__hash_combine(lhs: hash<chrono::sys_seconds>{}(__lp.date()), rhs: hash<chrono::seconds>{}(__lp.value()));
130 }
131};
132
133# endif // _LIBCPP_STD_VER >= 26
134
135# endif // _LIBCPP_STD_VER >= 20
136
137_LIBCPP_END_NAMESPACE_STD
138
139#endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB
140
141#endif // _LIBCPP___CHRONO_LEAP_SECOND_H
142