xref: /freebsd/contrib/llvm-project/libcxx/include/__cxx03/__chrono/time_point.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 #ifndef _LIBCPP___CXX03___CHRONO_TIME_POINT_H
11 #define _LIBCPP___CXX03___CHRONO_TIME_POINT_H
12 
13 #include <__cxx03/__chrono/duration.h>
14 #include <__cxx03/__config>
15 #include <__cxx03/__type_traits/common_type.h>
16 #include <__cxx03/__type_traits/enable_if.h>
17 #include <__cxx03/__type_traits/is_convertible.h>
18 #include <__cxx03/limits>
19 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 _LIBCPP_PUSH_MACROS
25 #include <__cxx03/__undef_macros>
26 
27 _LIBCPP_BEGIN_NAMESPACE_STD
28 
29 namespace chrono {
30 
31 template <class _Clock, class _Duration = typename _Clock::duration>
32 class _LIBCPP_TEMPLATE_VIS time_point {
33   static_assert(__is_duration<_Duration>::value,
34                 "Second template parameter of time_point must be a std::chrono::duration");
35 
36 public:
37   typedef _Clock clock;
38   typedef _Duration duration;
39   typedef typename duration::rep rep;
40   typedef typename duration::period period;
41 
42 private:
43   duration __d_;
44 
45 public:
time_point()46   _LIBCPP_HIDE_FROM_ABI time_point() : __d_(duration::zero()) {}
time_point(const duration & __d)47   _LIBCPP_HIDE_FROM_ABI explicit time_point(const duration& __d) : __d_(__d) {}
48 
49   // conversions
50   template <class _Duration2, __enable_if_t<is_convertible<_Duration2, duration>::value, int> = 0>
time_point(const time_point<clock,_Duration2> & __t)51   _LIBCPP_HIDE_FROM_ABI time_point(const time_point<clock, _Duration2>& __t) : __d_(__t.time_since_epoch()) {}
52 
53   // observer
54 
time_since_epoch()55   _LIBCPP_HIDE_FROM_ABI duration time_since_epoch() const { return __d_; }
56 
57   // arithmetic
58 
59   _LIBCPP_HIDE_FROM_ABI time_point& operator+=(const duration& __d) {
60     __d_ += __d;
61     return *this;
62   }
63   _LIBCPP_HIDE_FROM_ABI time_point& operator-=(const duration& __d) {
64     __d_ -= __d;
65     return *this;
66   }
67 
68   // special values
69 
min()70   _LIBCPP_HIDE_FROM_ABI static time_point min() _NOEXCEPT { return time_point(duration::min()); }
max()71   _LIBCPP_HIDE_FROM_ABI static time_point max() _NOEXCEPT { return time_point(duration::max()); }
72 };
73 
74 } // namespace chrono
75 
76 template <class _Clock, class _Duration1, class _Duration2>
77 struct _LIBCPP_TEMPLATE_VIS
78 common_type<chrono::time_point<_Clock, _Duration1>, chrono::time_point<_Clock, _Duration2> > {
79   typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;
80 };
81 
82 namespace chrono {
83 
84 template <class _ToDuration, class _Clock, class _Duration>
85 inline _LIBCPP_HIDE_FROM_ABI time_point<_Clock, _ToDuration> time_point_cast(const time_point<_Clock, _Duration>& __t) {
86   return time_point<_Clock, _ToDuration>(chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
87 }
88 
89 // time_point ==
90 
91 template <class _Clock, class _Duration1, class _Duration2>
92 inline _LIBCPP_HIDE_FROM_ABI bool
93 operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
94   return __lhs.time_since_epoch() == __rhs.time_since_epoch();
95 }
96 
97 // time_point !=
98 
99 template <class _Clock, class _Duration1, class _Duration2>
100 inline _LIBCPP_HIDE_FROM_ABI bool
101 operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
102   return !(__lhs == __rhs);
103 }
104 
105 // time_point <
106 
107 template <class _Clock, class _Duration1, class _Duration2>
108 inline _LIBCPP_HIDE_FROM_ABI bool
109 operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
110   return __lhs.time_since_epoch() < __rhs.time_since_epoch();
111 }
112 
113 // time_point >
114 
115 template <class _Clock, class _Duration1, class _Duration2>
116 inline _LIBCPP_HIDE_FROM_ABI bool
117 operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
118   return __rhs < __lhs;
119 }
120 
121 // time_point <=
122 
123 template <class _Clock, class _Duration1, class _Duration2>
124 inline _LIBCPP_HIDE_FROM_ABI bool
125 operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
126   return !(__rhs < __lhs);
127 }
128 
129 // time_point >=
130 
131 template <class _Clock, class _Duration1, class _Duration2>
132 inline _LIBCPP_HIDE_FROM_ABI bool
133 operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
134   return !(__lhs < __rhs);
135 }
136 
137 // time_point operator+(time_point x, duration y);
138 
139 template <class _Clock, class _Duration1, class _Rep2, class _Period2>
140 inline _LIBCPP_HIDE_FROM_ABI time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
141 operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
142   typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
143   return _Tr(__lhs.time_since_epoch() + __rhs);
144 }
145 
146 // time_point operator+(duration x, time_point y);
147 
148 template <class _Rep1, class _Period1, class _Clock, class _Duration2>
149 inline _LIBCPP_HIDE_FROM_ABI time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
150 operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
151   return __rhs + __lhs;
152 }
153 
154 // time_point operator-(time_point x, duration y);
155 
156 template <class _Clock, class _Duration1, class _Rep2, class _Period2>
157 inline _LIBCPP_HIDE_FROM_ABI time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
158 operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
159   typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;
160   return _Ret(__lhs.time_since_epoch() - __rhs);
161 }
162 
163 // duration operator-(time_point x, time_point y);
164 
165 template <class _Clock, class _Duration1, class _Duration2>
166 inline _LIBCPP_HIDE_FROM_ABI typename common_type<_Duration1, _Duration2>::type
167 operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {
168   return __lhs.time_since_epoch() - __rhs.time_since_epoch();
169 }
170 
171 } // namespace chrono
172 
173 _LIBCPP_END_NAMESPACE_STD
174 
175 _LIBCPP_POP_MACROS
176 
177 #endif // _LIBCPP___CXX03___CHRONO_TIME_POINT_H
178