xref: /freebsd/contrib/llvm-project/libcxx/include/__chrono/time_point.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
104eeddc0SDimitry Andric // -*- C++ -*-
204eeddc0SDimitry Andric //===----------------------------------------------------------------------===//
304eeddc0SDimitry Andric //
404eeddc0SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
504eeddc0SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
604eeddc0SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
704eeddc0SDimitry Andric //
804eeddc0SDimitry Andric //===----------------------------------------------------------------------===//
904eeddc0SDimitry Andric 
1004eeddc0SDimitry Andric #ifndef _LIBCPP___CHRONO_TIME_POINT_H
1104eeddc0SDimitry Andric #define _LIBCPP___CHRONO_TIME_POINT_H
1204eeddc0SDimitry Andric 
1304eeddc0SDimitry Andric #include <__chrono/duration.h>
1406c3fb27SDimitry Andric #include <__compare/ordering.h>
1506c3fb27SDimitry Andric #include <__compare/three_way_comparable.h>
1604eeddc0SDimitry Andric #include <__config>
17bdd1243dSDimitry Andric #include <__type_traits/common_type.h>
18bdd1243dSDimitry Andric #include <__type_traits/enable_if.h>
19bdd1243dSDimitry Andric #include <__type_traits/is_convertible.h>
2004eeddc0SDimitry Andric #include <limits>
2104eeddc0SDimitry Andric 
2204eeddc0SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2304eeddc0SDimitry Andric #  pragma GCC system_header
2404eeddc0SDimitry Andric #endif
2504eeddc0SDimitry Andric 
2604eeddc0SDimitry Andric _LIBCPP_PUSH_MACROS
2704eeddc0SDimitry Andric #include <__undef_macros>
2804eeddc0SDimitry Andric 
2904eeddc0SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
3004eeddc0SDimitry Andric 
31cb14a3feSDimitry Andric namespace chrono {
3204eeddc0SDimitry Andric 
3304eeddc0SDimitry Andric template <class _Clock, class _Duration = typename _Clock::duration>
34cb14a3feSDimitry Andric class _LIBCPP_TEMPLATE_VIS time_point {
3504eeddc0SDimitry Andric   static_assert(__is_duration<_Duration>::value,
3604eeddc0SDimitry Andric                 "Second template parameter of time_point must be a std::chrono::duration");
37cb14a3feSDimitry Andric 
3804eeddc0SDimitry Andric public:
3904eeddc0SDimitry Andric   typedef _Clock clock;
4004eeddc0SDimitry Andric   typedef _Duration duration;
4104eeddc0SDimitry Andric   typedef typename duration::rep rep;
4204eeddc0SDimitry Andric   typedef typename duration::period period;
43cb14a3feSDimitry Andric 
4404eeddc0SDimitry Andric private:
4504eeddc0SDimitry Andric   duration __d_;
4604eeddc0SDimitry Andric 
4704eeddc0SDimitry Andric public:
time_point()485f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point() : __d_(duration::zero()) {}
time_point(const duration & __d)495f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit time_point(const duration& __d) : __d_(__d) {}
5004eeddc0SDimitry Andric 
5104eeddc0SDimitry Andric   // conversions
525f757f3fSDimitry Andric   template <class _Duration2, __enable_if_t<is_convertible<_Duration2, duration>::value, int> = 0>
time_point(const time_point<clock,_Duration2> & __t)53cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point(const time_point<clock, _Duration2>& __t)
54753f127fSDimitry Andric       : __d_(__t.time_since_epoch()) {}
5504eeddc0SDimitry Andric 
5604eeddc0SDimitry Andric   // observer
5704eeddc0SDimitry Andric 
time_since_epoch()585f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 duration time_since_epoch() const { return __d_; }
5904eeddc0SDimitry Andric 
6004eeddc0SDimitry Andric   // arithmetic
6104eeddc0SDimitry Andric 
62cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 time_point& operator+=(const duration& __d) {
63cb14a3feSDimitry Andric     __d_ += __d;
64cb14a3feSDimitry Andric     return *this;
65cb14a3feSDimitry Andric   }
66cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 time_point& operator-=(const duration& __d) {
67cb14a3feSDimitry Andric     __d_ -= __d;
68cb14a3feSDimitry Andric     return *this;
69cb14a3feSDimitry Andric   }
7004eeddc0SDimitry Andric 
7104eeddc0SDimitry Andric   // special values
7204eeddc0SDimitry Andric 
min()735f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR time_point min() _NOEXCEPT { return time_point(duration::min()); }
max()745f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR time_point max() _NOEXCEPT { return time_point(duration::max()); }
7504eeddc0SDimitry Andric };
7604eeddc0SDimitry Andric 
7704eeddc0SDimitry Andric } // namespace chrono
7804eeddc0SDimitry Andric 
7904eeddc0SDimitry Andric template <class _Clock, class _Duration1, class _Duration2>
80cb14a3feSDimitry Andric struct _LIBCPP_TEMPLATE_VIS
81cb14a3feSDimitry Andric common_type<chrono::time_point<_Clock, _Duration1>, chrono::time_point<_Clock, _Duration2> > {
8204eeddc0SDimitry Andric   typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;
8304eeddc0SDimitry Andric };
8404eeddc0SDimitry Andric 
8504eeddc0SDimitry Andric namespace chrono {
8604eeddc0SDimitry Andric 
8704eeddc0SDimitry Andric template <class _ToDuration, class _Clock, class _Duration>
88cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, _ToDuration>
89cb14a3feSDimitry Andric time_point_cast(const time_point<_Clock, _Duration>& __t) {
9004eeddc0SDimitry Andric   return time_point<_Clock, _ToDuration>(chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
9104eeddc0SDimitry Andric }
9204eeddc0SDimitry Andric 
9306c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 17
945f757f3fSDimitry Andric template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
95*0fca6ea1SDimitry Andric inline _LIBCPP_HIDE_FROM_ABI constexpr time_point<_Clock, _ToDuration> floor(const time_point<_Clock, _Duration>& __t) {
96bdd1243dSDimitry Andric   return time_point<_Clock, _ToDuration>{chrono::floor<_ToDuration>(__t.time_since_epoch())};
9704eeddc0SDimitry Andric }
9804eeddc0SDimitry Andric 
995f757f3fSDimitry Andric template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
100*0fca6ea1SDimitry Andric inline _LIBCPP_HIDE_FROM_ABI constexpr time_point<_Clock, _ToDuration> ceil(const time_point<_Clock, _Duration>& __t) {
101bdd1243dSDimitry Andric   return time_point<_Clock, _ToDuration>{chrono::ceil<_ToDuration>(__t.time_since_epoch())};
10204eeddc0SDimitry Andric }
10304eeddc0SDimitry Andric 
1045f757f3fSDimitry Andric template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
105*0fca6ea1SDimitry Andric inline _LIBCPP_HIDE_FROM_ABI constexpr time_point<_Clock, _ToDuration> round(const time_point<_Clock, _Duration>& __t) {
106bdd1243dSDimitry Andric   return time_point<_Clock, _ToDuration>{chrono::round<_ToDuration>(__t.time_since_epoch())};
10704eeddc0SDimitry Andric }
10804eeddc0SDimitry Andric 
1095f757f3fSDimitry Andric template <class _Rep, class _Period, enable_if_t<numeric_limits<_Rep>::is_signed, int> = 0>
110*0fca6ea1SDimitry Andric inline _LIBCPP_HIDE_FROM_ABI constexpr duration<_Rep, _Period> abs(duration<_Rep, _Period> __d) {
11104eeddc0SDimitry Andric   return __d >= __d.zero() ? +__d : -__d;
11204eeddc0SDimitry Andric }
11306c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 17
11404eeddc0SDimitry Andric 
11504eeddc0SDimitry Andric // time_point ==
11604eeddc0SDimitry Andric 
11704eeddc0SDimitry Andric template <class _Clock, class _Duration1, class _Duration2>
118cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
119cb14a3feSDimitry Andric operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
12004eeddc0SDimitry Andric   return __lhs.time_since_epoch() == __rhs.time_since_epoch();
12104eeddc0SDimitry Andric }
12204eeddc0SDimitry Andric 
12306c3fb27SDimitry Andric #if _LIBCPP_STD_VER <= 17
12406c3fb27SDimitry Andric 
12504eeddc0SDimitry Andric // time_point !=
12604eeddc0SDimitry Andric 
12704eeddc0SDimitry Andric template <class _Clock, class _Duration1, class _Duration2>
128cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
129cb14a3feSDimitry Andric operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
13004eeddc0SDimitry Andric   return !(__lhs == __rhs);
13104eeddc0SDimitry Andric }
13204eeddc0SDimitry Andric 
13306c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER <= 17
13406c3fb27SDimitry Andric 
13504eeddc0SDimitry Andric // time_point <
13604eeddc0SDimitry Andric 
13704eeddc0SDimitry Andric template <class _Clock, class _Duration1, class _Duration2>
138cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
139cb14a3feSDimitry Andric operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
14004eeddc0SDimitry Andric   return __lhs.time_since_epoch() < __rhs.time_since_epoch();
14104eeddc0SDimitry Andric }
14204eeddc0SDimitry Andric 
14304eeddc0SDimitry Andric // time_point >
14404eeddc0SDimitry Andric 
14504eeddc0SDimitry Andric template <class _Clock, class _Duration1, class _Duration2>
146cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
147cb14a3feSDimitry Andric operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
14804eeddc0SDimitry Andric   return __rhs < __lhs;
14904eeddc0SDimitry Andric }
15004eeddc0SDimitry Andric 
15104eeddc0SDimitry Andric // time_point <=
15204eeddc0SDimitry Andric 
15304eeddc0SDimitry Andric template <class _Clock, class _Duration1, class _Duration2>
154cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
155cb14a3feSDimitry Andric operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
15604eeddc0SDimitry Andric   return !(__rhs < __lhs);
15704eeddc0SDimitry Andric }
15804eeddc0SDimitry Andric 
15904eeddc0SDimitry Andric // time_point >=
16004eeddc0SDimitry Andric 
16104eeddc0SDimitry Andric template <class _Clock, class _Duration1, class _Duration2>
162cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool
163cb14a3feSDimitry Andric operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
16404eeddc0SDimitry Andric   return !(__lhs < __rhs);
16504eeddc0SDimitry Andric }
16604eeddc0SDimitry Andric 
16706c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
16806c3fb27SDimitry Andric 
16906c3fb27SDimitry Andric template <class _Clock, class _Duration1, three_way_comparable_with<_Duration1> _Duration2>
17006c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI constexpr auto
17106c3fb27SDimitry Andric operator<=>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
17206c3fb27SDimitry Andric   return __lhs.time_since_epoch() <=> __rhs.time_since_epoch();
17306c3fb27SDimitry Andric }
17406c3fb27SDimitry Andric 
17506c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
17606c3fb27SDimitry Andric 
17704eeddc0SDimitry Andric // time_point operator+(time_point x, duration y);
17804eeddc0SDimitry Andric 
17904eeddc0SDimitry Andric template <class _Clock, class _Duration1, class _Rep2, class _Period2>
180*0fca6ea1SDimitry Andric inline _LIBCPP_HIDE_FROM_ABI
181*0fca6ea1SDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
182cb14a3feSDimitry Andric operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
18304eeddc0SDimitry Andric   typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
18404eeddc0SDimitry Andric   return _Tr(__lhs.time_since_epoch() + __rhs);
18504eeddc0SDimitry Andric }
18604eeddc0SDimitry Andric 
18704eeddc0SDimitry Andric // time_point operator+(duration x, time_point y);
18804eeddc0SDimitry Andric 
18904eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Clock, class _Duration2>
190*0fca6ea1SDimitry Andric inline _LIBCPP_HIDE_FROM_ABI
191*0fca6ea1SDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
192cb14a3feSDimitry Andric operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
19304eeddc0SDimitry Andric   return __rhs + __lhs;
19404eeddc0SDimitry Andric }
19504eeddc0SDimitry Andric 
19604eeddc0SDimitry Andric // time_point operator-(time_point x, duration y);
19704eeddc0SDimitry Andric 
19804eeddc0SDimitry Andric template <class _Clock, class _Duration1, class _Rep2, class _Period2>
199*0fca6ea1SDimitry Andric inline _LIBCPP_HIDE_FROM_ABI
200*0fca6ea1SDimitry Andric _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
201cb14a3feSDimitry Andric operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
20204eeddc0SDimitry Andric   typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;
20304eeddc0SDimitry Andric   return _Ret(__lhs.time_since_epoch() - __rhs);
20404eeddc0SDimitry Andric }
20504eeddc0SDimitry Andric 
20604eeddc0SDimitry Andric // duration operator-(time_point x, time_point y);
20704eeddc0SDimitry Andric 
20804eeddc0SDimitry Andric template <class _Clock, class _Duration1, class _Duration2>
209cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename common_type<_Duration1, _Duration2>::type
210cb14a3feSDimitry Andric operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
21104eeddc0SDimitry Andric   return __lhs.time_since_epoch() - __rhs.time_since_epoch();
21204eeddc0SDimitry Andric }
21304eeddc0SDimitry Andric 
21404eeddc0SDimitry Andric } // namespace chrono
21504eeddc0SDimitry Andric 
21604eeddc0SDimitry Andric _LIBCPP_END_NAMESPACE_STD
21704eeddc0SDimitry Andric 
21804eeddc0SDimitry Andric _LIBCPP_POP_MACROS
21904eeddc0SDimitry Andric 
22004eeddc0SDimitry Andric #endif // _LIBCPP___CHRONO_TIME_POINT_H
221