File indexing completed on 2026-05-03 08:13:25
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___CXX03___CHRONO_TIME_POINT_H
0011 #define _LIBCPP___CXX03___CHRONO_TIME_POINT_H
0012
0013 #include <__cxx03/__chrono/duration.h>
0014 #include <__cxx03/__compare/ordering.h>
0015 #include <__cxx03/__compare/three_way_comparable.h>
0016 #include <__cxx03/__config>
0017 #include <__cxx03/__type_traits/common_type.h>
0018 #include <__cxx03/__type_traits/enable_if.h>
0019 #include <__cxx03/__type_traits/is_convertible.h>
0020 #include <__cxx03/limits>
0021
0022 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0023 # pragma GCC system_header
0024 #endif
0025
0026 _LIBCPP_PUSH_MACROS
0027 #include <__cxx03/__undef_macros>
0028
0029 _LIBCPP_BEGIN_NAMESPACE_STD
0030
0031 namespace chrono {
0032
0033 template <class _Clock, class _Duration = typename _Clock::duration>
0034 class _LIBCPP_TEMPLATE_VIS time_point {
0035 static_assert(__is_duration<_Duration>::value,
0036 "Second template parameter of time_point must be a std::chrono::duration");
0037
0038 public:
0039 typedef _Clock clock;
0040 typedef _Duration duration;
0041 typedef typename duration::rep rep;
0042 typedef typename duration::period period;
0043
0044 private:
0045 duration __d_;
0046
0047 public:
0048 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point() : __d_(duration::zero()) {}
0049 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit time_point(const duration& __d) : __d_(__d) {}
0050
0051
0052 template <class _Duration2, __enable_if_t<is_convertible<_Duration2, duration>::value, int> = 0>
0053 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point(const time_point<clock, _Duration2>& __t)
0054 : __d_(__t.time_since_epoch()) {}
0055
0056
0057
0058 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 duration time_since_epoch() const { return __d_; }
0059
0060
0061
0062 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 time_point& operator+=(const duration& __d) {
0063 __d_ += __d;
0064 return *this;
0065 }
0066 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 time_point& operator-=(const duration& __d) {
0067 __d_ -= __d;
0068 return *this;
0069 }
0070
0071
0072
0073 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR time_point min() _NOEXCEPT { return time_point(duration::min()); }
0074 _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR time_point max() _NOEXCEPT { return time_point(duration::max()); }
0075 };
0076
0077 }
0078
0079 template <class _Clock, class _Duration1, class _Duration2>
0080 struct _LIBCPP_TEMPLATE_VIS
0081 common_type<chrono::time_point<_Clock, _Duration1>, chrono::time_point<_Clock, _Duration2> > {
0082 typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;
0083 };
0084
0085 namespace chrono {
0086
0087 template <class _ToDuration, class _Clock, class _Duration>
0088 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, _ToDuration>
0089 time_point_cast(const time_point<_Clock, _Duration>& __t) {
0090 return time_point<_Clock, _ToDuration>(chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
0091 }
0092
0093 #if _LIBCPP_STD_VER >= 17
0094 template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
0095 inline _LIBCPP_HIDE_FROM_ABI constexpr time_point<_Clock, _ToDuration> floor(const time_point<_Clock, _Duration>& __t) {
0096 return time_point<_Clock, _ToDuration>{chrono::floor<_ToDuration>(__t.time_since_epoch())};
0097 }
0098
0099 template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
0100 inline _LIBCPP_HIDE_FROM_ABI constexpr time_point<_Clock, _ToDuration> ceil(const time_point<_Clock, _Duration>& __t) {
0101 return time_point<_Clock, _ToDuration>{chrono::ceil<_ToDuration>(__t.time_since_epoch())};
0102 }
0103
0104 template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
0105 inline _LIBCPP_HIDE_FROM_ABI constexpr time_point<_Clock, _ToDuration> round(const time_point<_Clock, _Duration>& __t) {
0106 return time_point<_Clock, _ToDuration>{chrono::round<_ToDuration>(__t.time_since_epoch())};
0107 }
0108
0109 template <class _Rep, class _Period, enable_if_t<numeric_limits<_Rep>::is_signed, int> = 0>
0110 inline _LIBCPP_HIDE_FROM_ABI constexpr duration<_Rep, _Period> abs(duration<_Rep, _Period> __d) {
0111 return __d >= __d.zero() ? +__d : -__d;
0112 }
0113 #endif
0114
0115
0116
0117 template <class _Clock, class _Duration1, class _Duration2>
0118 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
0119 operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
0120 return __lhs.time_since_epoch() == __rhs.time_since_epoch();
0121 }
0122
0123 #if _LIBCPP_STD_VER <= 17
0124
0125
0126
0127 template <class _Clock, class _Duration1, class _Duration2>
0128 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
0129 operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
0130 return !(__lhs == __rhs);
0131 }
0132
0133 #endif
0134
0135
0136
0137 template <class _Clock, class _Duration1, class _Duration2>
0138 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
0139 operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
0140 return __lhs.time_since_epoch() < __rhs.time_since_epoch();
0141 }
0142
0143
0144
0145 template <class _Clock, class _Duration1, class _Duration2>
0146 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
0147 operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
0148 return __rhs < __lhs;
0149 }
0150
0151
0152
0153 template <class _Clock, class _Duration1, class _Duration2>
0154 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
0155 operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
0156 return !(__rhs < __lhs);
0157 }
0158
0159
0160
0161 template <class _Clock, class _Duration1, class _Duration2>
0162 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
0163 operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
0164 return !(__lhs < __rhs);
0165 }
0166
0167 #if _LIBCPP_STD_VER >= 20
0168
0169 template <class _Clock, class _Duration1, three_way_comparable_with<_Duration1> _Duration2>
0170 _LIBCPP_HIDE_FROM_ABI constexpr auto
0171 operator<=>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
0172 return __lhs.time_since_epoch() <=> __rhs.time_since_epoch();
0173 }
0174
0175 #endif
0176
0177
0178
0179 template <class _Clock, class _Duration1, class _Rep2, class _Period2>
0180 inline _LIBCPP_HIDE_FROM_ABI
0181 _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
0182 operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
0183 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
0184 return _Tr(__lhs.time_since_epoch() + __rhs);
0185 }
0186
0187
0188
0189 template <class _Rep1, class _Period1, class _Clock, class _Duration2>
0190 inline _LIBCPP_HIDE_FROM_ABI
0191 _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
0192 operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
0193 return __rhs + __lhs;
0194 }
0195
0196
0197
0198 template <class _Clock, class _Duration1, class _Rep2, class _Period2>
0199 inline _LIBCPP_HIDE_FROM_ABI
0200 _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
0201 operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
0202 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;
0203 return _Ret(__lhs.time_since_epoch() - __rhs);
0204 }
0205
0206
0207
0208 template <class _Clock, class _Duration1, class _Duration2>
0209 inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename common_type<_Duration1, _Duration2>::type
0210 operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
0211 return __lhs.time_since_epoch() - __rhs.time_since_epoch();
0212 }
0213
0214 }
0215
0216 _LIBCPP_END_NAMESPACE_STD
0217
0218 _LIBCPP_POP_MACROS
0219
0220 #endif