xref: /llvm-project/libcxx/include/__chrono/leap_second.h (revision 24e70e3930724ce499ad05d669bfbc4423c542e0)
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 <__utility/private_constructor_tag.h>
26 
27 #  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 #    pragma GCC system_header
29 #  endif
30 
31 _LIBCPP_BEGIN_NAMESPACE_STD
32 
33 #  if _LIBCPP_STD_VER >= 20
34 
35 namespace chrono {
36 
37 class leap_second {
38 public:
39   [[nodiscard]]
40   _LIBCPP_HIDE_FROM_ABI explicit constexpr leap_second(__private_constructor_tag, sys_seconds __date, seconds __value)
41       : __date_(__date), __value_(__value) {}
42 
43   _LIBCPP_HIDE_FROM_ABI leap_second(const leap_second&)            = default;
44   _LIBCPP_HIDE_FROM_ABI leap_second& operator=(const leap_second&) = default;
45 
46   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr sys_seconds date() const noexcept { return __date_; }
47 
48   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr seconds value() const noexcept { return __value_; }
49 
50 private:
51   sys_seconds __date_;
52   seconds __value_;
53 
54   // The function
55   //   template<class Duration>
56   //    requires three_way_comparable_with<sys_seconds, sys_time<Duration>>
57   //    constexpr auto operator<=>(const leap_second& x, const sys_time<Duration>& y) noexcept;
58   //
59   // Has constraints that are recursive (LWG4139). The proposed resolution is
60   // to make the funcion a hidden friend. For consistency make this change for
61   // all comparison functions.
62 
63   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const leap_second& __x, const leap_second& __y) {
64     return __x.date() == __y.date();
65   }
66 
67   _LIBCPP_HIDE_FROM_ABI friend constexpr strong_ordering operator<=>(const leap_second& __x, const leap_second& __y) {
68     return __x.date() <=> __y.date();
69   }
70 
71   template <class _Duration>
72   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const leap_second& __x, const sys_time<_Duration>& __y) {
73     return __x.date() == __y;
74   }
75 
76   template <class _Duration>
77   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(const leap_second& __x, const sys_time<_Duration>& __y) {
78     return __x.date() < __y;
79   }
80 
81   template <class _Duration>
82   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(const sys_time<_Duration>& __x, const leap_second& __y) {
83     return __x < __y.date();
84   }
85 
86   template <class _Duration>
87   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(const leap_second& __x, const sys_time<_Duration>& __y) {
88     return __y < __x;
89   }
90 
91   template <class _Duration>
92   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(const sys_time<_Duration>& __x, const leap_second& __y) {
93     return __y < __x;
94   }
95 
96   template <class _Duration>
97   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(const leap_second& __x, const sys_time<_Duration>& __y) {
98     return !(__y < __x);
99   }
100 
101   template <class _Duration>
102   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(const sys_time<_Duration>& __x, const leap_second& __y) {
103     return !(__y < __x);
104   }
105 
106   template <class _Duration>
107   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(const leap_second& __x, const sys_time<_Duration>& __y) {
108     return !(__x < __y);
109   }
110 
111   template <class _Duration>
112   _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(const sys_time<_Duration>& __x, const leap_second& __y) {
113     return !(__x < __y);
114   }
115 
116   template <class _Duration>
117     requires three_way_comparable_with<sys_seconds, sys_time<_Duration>>
118   _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(const leap_second& __x, const sys_time<_Duration>& __y) {
119     return __x.date() <=> __y;
120   }
121 };
122 
123 } // namespace chrono
124 
125 #  endif // _LIBCPP_STD_VER >= 20
126 
127 _LIBCPP_END_NAMESPACE_STD
128 
129 #endif // _LIBCPP_HAS_EXPERIMENTAL_TZDB
130 
131 #endif // _LIBCPP___CHRONO_LEAP_SECOND_H
132