Path: blob/main/contrib/llvm-project/libcxx/include/__chrono/time_point.h
35262 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___CHRONO_TIME_POINT_H10#define _LIBCPP___CHRONO_TIME_POINT_H1112#include <__chrono/duration.h>13#include <__compare/ordering.h>14#include <__compare/three_way_comparable.h>15#include <__config>16#include <__type_traits/common_type.h>17#include <__type_traits/enable_if.h>18#include <__type_traits/is_convertible.h>19#include <limits>2021#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)22# pragma GCC system_header23#endif2425_LIBCPP_PUSH_MACROS26#include <__undef_macros>2728_LIBCPP_BEGIN_NAMESPACE_STD2930namespace chrono {3132template <class _Clock, class _Duration = typename _Clock::duration>33class _LIBCPP_TEMPLATE_VIS time_point {34static_assert(__is_duration<_Duration>::value,35"Second template parameter of time_point must be a std::chrono::duration");3637public:38typedef _Clock clock;39typedef _Duration duration;40typedef typename duration::rep rep;41typedef typename duration::period period;4243private:44duration __d_;4546public:47_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point() : __d_(duration::zero()) {}48_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 explicit time_point(const duration& __d) : __d_(__d) {}4950// conversions51template <class _Duration2, __enable_if_t<is_convertible<_Duration2, duration>::value, int> = 0>52_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point(const time_point<clock, _Duration2>& __t)53: __d_(__t.time_since_epoch()) {}5455// observer5657_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 duration time_since_epoch() const { return __d_; }5859// arithmetic6061_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 time_point& operator+=(const duration& __d) {62__d_ += __d;63return *this;64}65_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 time_point& operator-=(const duration& __d) {66__d_ -= __d;67return *this;68}6970// special values7172_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR time_point min() _NOEXCEPT { return time_point(duration::min()); }73_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR time_point max() _NOEXCEPT { return time_point(duration::max()); }74};7576} // namespace chrono7778template <class _Clock, class _Duration1, class _Duration2>79struct _LIBCPP_TEMPLATE_VIS80common_type<chrono::time_point<_Clock, _Duration1>, chrono::time_point<_Clock, _Duration2> > {81typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;82};8384namespace chrono {8586template <class _ToDuration, class _Clock, class _Duration>87inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, _ToDuration>88time_point_cast(const time_point<_Clock, _Duration>& __t) {89return time_point<_Clock, _ToDuration>(chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));90}9192#if _LIBCPP_STD_VER >= 1793template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>94inline _LIBCPP_HIDE_FROM_ABI constexpr time_point<_Clock, _ToDuration> floor(const time_point<_Clock, _Duration>& __t) {95return time_point<_Clock, _ToDuration>{chrono::floor<_ToDuration>(__t.time_since_epoch())};96}9798template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>99inline _LIBCPP_HIDE_FROM_ABI constexpr time_point<_Clock, _ToDuration> ceil(const time_point<_Clock, _Duration>& __t) {100return time_point<_Clock, _ToDuration>{chrono::ceil<_ToDuration>(__t.time_since_epoch())};101}102103template <class _ToDuration, class _Clock, class _Duration, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>104inline _LIBCPP_HIDE_FROM_ABI constexpr time_point<_Clock, _ToDuration> round(const time_point<_Clock, _Duration>& __t) {105return time_point<_Clock, _ToDuration>{chrono::round<_ToDuration>(__t.time_since_epoch())};106}107108template <class _Rep, class _Period, enable_if_t<numeric_limits<_Rep>::is_signed, int> = 0>109inline _LIBCPP_HIDE_FROM_ABI constexpr duration<_Rep, _Period> abs(duration<_Rep, _Period> __d) {110return __d >= __d.zero() ? +__d : -__d;111}112#endif // _LIBCPP_STD_VER >= 17113114// time_point ==115116template <class _Clock, class _Duration1, class _Duration2>117inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool118operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {119return __lhs.time_since_epoch() == __rhs.time_since_epoch();120}121122#if _LIBCPP_STD_VER <= 17123124// time_point !=125126template <class _Clock, class _Duration1, class _Duration2>127inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool128operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {129return !(__lhs == __rhs);130}131132#endif // _LIBCPP_STD_VER <= 17133134// time_point <135136template <class _Clock, class _Duration1, class _Duration2>137inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool138operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {139return __lhs.time_since_epoch() < __rhs.time_since_epoch();140}141142// time_point >143144template <class _Clock, class _Duration1, class _Duration2>145inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool146operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {147return __rhs < __lhs;148}149150// time_point <=151152template <class _Clock, class _Duration1, class _Duration2>153inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool154operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {155return !(__rhs < __lhs);156}157158// time_point >=159160template <class _Clock, class _Duration1, class _Duration2>161inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 bool162operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {163return !(__lhs < __rhs);164}165166#if _LIBCPP_STD_VER >= 20167168template <class _Clock, class _Duration1, three_way_comparable_with<_Duration1> _Duration2>169_LIBCPP_HIDE_FROM_ABI constexpr auto170operator<=>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {171return __lhs.time_since_epoch() <=> __rhs.time_since_epoch();172}173174#endif // _LIBCPP_STD_VER >= 20175176// time_point operator+(time_point x, duration y);177178template <class _Clock, class _Duration1, class _Rep2, class _Period2>179inline _LIBCPP_HIDE_FROM_ABI180_LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>181operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {182typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;183return _Tr(__lhs.time_since_epoch() + __rhs);184}185186// time_point operator+(duration x, time_point y);187188template <class _Rep1, class _Period1, class _Clock, class _Duration2>189inline _LIBCPP_HIDE_FROM_ABI190_LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>191operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {192return __rhs + __lhs;193}194195// time_point operator-(time_point x, duration y);196197template <class _Clock, class _Duration1, class _Rep2, class _Period2>198inline _LIBCPP_HIDE_FROM_ABI199_LIBCPP_CONSTEXPR_SINCE_CXX14 time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>200operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {201typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;202return _Ret(__lhs.time_since_epoch() - __rhs);203}204205// duration operator-(time_point x, time_point y);206207template <class _Clock, class _Duration1, class _Duration2>208inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename common_type<_Duration1, _Duration2>::type209operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) {210return __lhs.time_since_epoch() - __rhs.time_since_epoch();211}212213} // namespace chrono214215_LIBCPP_END_NAMESPACE_STD216217_LIBCPP_POP_MACROS218219#endif // _LIBCPP___CHRONO_TIME_POINT_H220221222