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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14
10 // <chrono>
11 
12 // floor
13 
14 // template <class ToDuration, class Clock, class Duration>
15 //   time_point<Clock, ToDuration>
16 //   floor(const time_point<Clock, Duration>& t);
17 
18 #include <chrono>
19 #include <type_traits>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 
24 template <class FromDuration, class ToDuration>
25 void
test(const FromDuration & df,const ToDuration & d)26 test(const FromDuration& df, const ToDuration& d)
27 {
28     typedef std::chrono::system_clock Clock;
29     typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;
30     typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;
31     {
32     FromTimePoint f(df);
33     ToTimePoint t(d);
34     typedef decltype(std::chrono::floor<ToDuration>(f)) R;
35     static_assert((std::is_same<R, ToTimePoint>::value), "");
36     assert(std::chrono::floor<ToDuration>(f) == t);
37     }
38 }
39 
40 template<class FromDuration, long long From, class ToDuration, long long To>
test_constexpr()41 void test_constexpr ()
42 {
43     typedef std::chrono::system_clock Clock;
44     typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;
45     typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;
46     {
47     constexpr FromTimePoint f{FromDuration{From}};
48     constexpr ToTimePoint   t{ToDuration{To}};
49     static_assert(std::chrono::floor<ToDuration>(f) == t, "");
50     }
51 }
52 
main(int,char **)53 int main(int, char**)
54 {
55 //  7290000ms is 2 hours, 1 minute, and 30 seconds
56     test(std::chrono::milliseconds( 7290000), std::chrono::hours( 2));
57     test(std::chrono::milliseconds(-7290000), std::chrono::hours(-3));
58     test(std::chrono::milliseconds( 7290000), std::chrono::minutes( 121));
59     test(std::chrono::milliseconds(-7290000), std::chrono::minutes(-122));
60 
61 //  9000000ms is 2 hours and 30 minutes
62     test_constexpr<std::chrono::milliseconds, 9000000, std::chrono::hours,    2> ();
63     test_constexpr<std::chrono::milliseconds,-9000000, std::chrono::hours,   -3> ();
64     test_constexpr<std::chrono::milliseconds, 9000001, std::chrono::minutes, 150> ();
65     test_constexpr<std::chrono::milliseconds,-9000001, std::chrono::minutes,-151> ();
66 
67     test_constexpr<std::chrono::milliseconds, 9000000, std::chrono::seconds, 9000> ();
68     test_constexpr<std::chrono::milliseconds,-9000000, std::chrono::seconds,-9000> ();
69 
70   return 0;
71 }
72