1bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//===----------------------------------------------------------------------===//
2bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//
3f5256e16dfc425c1d466f6308d4026d529ce9e0bHoward Hinnant//                     The LLVM Compiler Infrastructure
4bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//
5b64f8b07c104c6cc986570ac8ee0ed16a9f23976Howard Hinnant// This file is dual licensed under the MIT and the University of Illinois Open
6b64f8b07c104c6cc986570ac8ee0ed16a9f23976Howard Hinnant// Source Licenses. See LICENSE.TXT for details.
7bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//
8bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//===----------------------------------------------------------------------===//
9bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
10bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant// <chrono>
11bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
12bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant// time_point
13bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
14c52f43e72dfcea03037729649da84c23b3beb04aHoward Hinnant// template <class ToDuration, class Clock, class Duration>
15bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//   time_point<Clock, ToDuration>
16bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant//   time_point_cast(const time_point<Clock, Duration>& t);
17bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
18bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include <chrono>
19bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include <type_traits>
20bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant#include <cassert>
21bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
22bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnanttemplate <class FromDuration, class ToDuration>
23bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnantvoid
24bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnanttest(const FromDuration& df, const ToDuration& d)
25bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant{
26bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    typedef std::chrono::system_clock Clock;
27bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;
28bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;
29832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    {
30bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    FromTimePoint f(df);
31bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    ToTimePoint t(d);
32bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    typedef decltype(std::chrono::time_point_cast<ToDuration>(f)) R;
33bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    static_assert((std::is_same<R, ToTimePoint>::value), "");
34bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    assert(std::chrono::time_point_cast<ToDuration>(f) == t);
35832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    }
36832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow}
37832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow
38713f4db2e5b37ef84c5506fa1b65f3b76798d594Howard Hinnant#if _LIBCPP_STD_VER > 11
39713f4db2e5b37ef84c5506fa1b65f3b76798d594Howard Hinnant
40832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clowtemplate<class FromDuration, long long From, class ToDuration, long long To>
41832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clowvoid test_constexpr ()
42832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow{
43832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    typedef std::chrono::system_clock Clock;
44832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;
45832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;
46832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    {
47832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    constexpr FromTimePoint f{FromDuration{From}};
48832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    constexpr ToTimePoint   t{ToDuration{To}};
49832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    static_assert(std::chrono::time_point_cast<ToDuration>(f) == t, "");
50832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    }
51832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow
52bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant}
53bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant
54713f4db2e5b37ef84c5506fa1b65f3b76798d594Howard Hinnant#endif
55713f4db2e5b37ef84c5506fa1b65f3b76798d594Howard Hinnant
56bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnantint main()
57bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant{
58bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    test(std::chrono::milliseconds(7265000), std::chrono::hours(2));
59bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    test(std::chrono::milliseconds(7265000), std::chrono::minutes(121));
60bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    test(std::chrono::milliseconds(7265000), std::chrono::seconds(7265));
61bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    test(std::chrono::milliseconds(7265000), std::chrono::milliseconds(7265000));
62bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    test(std::chrono::milliseconds(7265000), std::chrono::microseconds(7265000000LL));
63bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    test(std::chrono::milliseconds(7265000), std::chrono::nanoseconds(7265000000000LL));
64bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    test(std::chrono::milliseconds(7265000),
65bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant         std::chrono::duration<double, std::ratio<3600> >(7265./3600));
66bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant    test(std::chrono::duration<int, std::ratio<2, 3> >(9),
67bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant         std::chrono::duration<int, std::ratio<3, 5> >(10));
68832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow#if _LIBCPP_STD_VER > 11
69832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    {
70832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::hours,    2> ();
71832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::minutes,121> ();
72832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::seconds,7265> ();
73832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::milliseconds,7265000> ();
74832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::microseconds,7265000000LL> ();
75832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::nanoseconds,7265000000000LL> ();
76832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    typedef std::chrono::duration<int, std::ratio<3, 5>> T1;
77832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    test_constexpr<std::chrono::duration<int, std::ratio<2, 3>>, 9, T1, 10> ();
78832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow    }
79832b304076afcd832a5ffe95281b46a7abdaf869Marshall Clow#endif
80bc8d3f97eb5c958007f2713238472e0c1c8fe02Howard Hinnant}
81