1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++98, c++03, c++11, c++14
11 // <chrono>
12 
13 // floor
14 
15 // template <class ToDuration, class Clock, class Duration>
16 //   time_point<Clock, ToDuration>
17 //   floor(const time_point<Clock, Duration>& t);
18 
19 #include <chrono>
20 #include <type_traits>
21 #include <cassert>
22 
23 template <class FromDuration, class ToDuration>
24 void
test(const FromDuration & df,const ToDuration & d)25 test(const FromDuration& df, const ToDuration& d)
26 {
27     typedef std::chrono::system_clock Clock;
28     typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;
29     typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;
30     {
31     FromTimePoint f(df);
32     ToTimePoint t(d);
33     typedef decltype(std::chrono::floor<ToDuration>(f)) R;
34     static_assert((std::is_same<R, ToTimePoint>::value), "");
35     assert(std::chrono::floor<ToDuration>(f) == t);
36     }
37 }
38 
39 template<class FromDuration, long long From, class ToDuration, long long To>
test_constexpr()40 void test_constexpr ()
41 {
42     typedef std::chrono::system_clock Clock;
43     typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;
44     typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;
45     {
46     constexpr FromTimePoint f{FromDuration{From}};
47     constexpr ToTimePoint   t{ToDuration{To}};
48     static_assert(std::chrono::floor<ToDuration>(f) == t, "");
49     }
50 }
51 
main()52 int main()
53 {
54 //  7290000ms is 2 hours, 1 minute, and 30 seconds
55     test(std::chrono::milliseconds( 7290000), std::chrono::hours( 2));
56     test(std::chrono::milliseconds(-7290000), std::chrono::hours(-3));
57     test(std::chrono::milliseconds( 7290000), std::chrono::minutes( 121));
58     test(std::chrono::milliseconds(-7290000), std::chrono::minutes(-122));
59 
60 //  9000000ms is 2 hours and 30 minutes
61     test_constexpr<std::chrono::milliseconds, 9000000, std::chrono::hours,    2> ();
62     test_constexpr<std::chrono::milliseconds,-9000000, std::chrono::hours,   -3> ();
63     test_constexpr<std::chrono::milliseconds, 9000001, std::chrono::minutes, 150> ();
64     test_constexpr<std::chrono::milliseconds,-9000001, std::chrono::minutes,-151> ();
65 
66     test_constexpr<std::chrono::milliseconds, 9000000, std::chrono::seconds, 9000> ();
67     test_constexpr<std::chrono::milliseconds,-9000000, std::chrono::seconds,-9000> ();
68 }
69