95 lines · cpp
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//===----------------------------------------------------------------------===//8 9// <chrono>10 11// time_point12 13// template <class ToDuration, class Clock, class Duration>14// time_point<Clock, ToDuration>15// time_point_cast(const time_point<Clock, Duration>& t);16 17#include <chrono>18#include <cassert>19#include <ratio>20#include <type_traits>21 22#include "test_macros.h"23 24template <class T, class = void>25struct has_time_point_cast : std::false_type {};26 27template <class T>28struct has_time_point_cast<T, decltype((void)std::chrono::time_point_cast<T>(std::chrono::system_clock::now()))>29 : std::true_type {};30 31static_assert(has_time_point_cast<std::chrono::seconds>::value, "");32static_assert(!has_time_point_cast<int>::value, "");33 34template <class FromDuration, class ToDuration>35void36test(const FromDuration& df, const ToDuration& d)37{38 typedef std::chrono::system_clock Clock;39 typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;40 typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;41 {42 FromTimePoint f(df);43 ToTimePoint t(d);44 typedef decltype(std::chrono::time_point_cast<ToDuration>(f)) R;45 static_assert((std::is_same<R, ToTimePoint>::value), "");46 assert(std::chrono::time_point_cast<ToDuration>(f) == t);47 }48}49 50#if TEST_STD_VER > 1151 52template<class FromDuration, long long From, class ToDuration, long long To>53void test_constexpr ()54{55 typedef std::chrono::system_clock Clock;56 typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;57 typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;58 {59 constexpr FromTimePoint f{FromDuration{From}};60 constexpr ToTimePoint t{ToDuration{To}};61 static_assert(std::chrono::time_point_cast<ToDuration>(f) == t, "");62 }63 64}65 66#endif67 68int main(int, char**)69{70 test(std::chrono::milliseconds(7265000), std::chrono::hours(2));71 test(std::chrono::milliseconds(7265000), std::chrono::minutes(121));72 test(std::chrono::milliseconds(7265000), std::chrono::seconds(7265));73 test(std::chrono::milliseconds(7265000), std::chrono::milliseconds(7265000));74 test(std::chrono::milliseconds(7265000), std::chrono::microseconds(7265000000LL));75 test(std::chrono::milliseconds(7265000), std::chrono::nanoseconds(7265000000000LL));76 test(std::chrono::milliseconds(7265000),77 std::chrono::duration<double, std::ratio<3600> >(7265./3600));78 test(std::chrono::duration<int, std::ratio<2, 3> >(9),79 std::chrono::duration<int, std::ratio<3, 5> >(10));80#if TEST_STD_VER > 1181 {82 test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::hours, 2> ();83 test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::minutes,121> ();84 test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::seconds,7265> ();85 test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::milliseconds,7265000> ();86 test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::microseconds,7265000000LL> ();87 test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::nanoseconds,7265000000000LL> ();88 typedef std::chrono::duration<int, std::ratio<3, 5>> T1;89 test_constexpr<std::chrono::duration<int, std::ratio<2, 3>>, 9, T1, 10> ();90 }91#endif92 93 return 0;94}95