Home | History | Annotate | Download | only in time.point.cast
      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 // <chrono>
     11 
     12 // time_point
     13 
     14 // template <class ToDuration, class Clock, class Duration>
     15 //   time_point<Clock, ToDuration>
     16 //   time_point_cast(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
     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::time_point_cast<ToDuration>(f)) R;
     35     static_assert((std::is_same<R, ToTimePoint>::value), "");
     36     assert(std::chrono::time_point_cast<ToDuration>(f) == t);
     37     }
     38 }
     39 
     40 #if TEST_STD_VER > 11
     41 
     42 template<class FromDuration, long long From, class ToDuration, long long To>
     43 void test_constexpr ()
     44 {
     45     typedef std::chrono::system_clock Clock;
     46     typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;
     47     typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;
     48     {
     49     constexpr FromTimePoint f{FromDuration{From}};
     50     constexpr ToTimePoint   t{ToDuration{To}};
     51     static_assert(std::chrono::time_point_cast<ToDuration>(f) == t, "");
     52     }
     53 
     54 }
     55 
     56 #endif
     57 
     58 int main()
     59 {
     60     test(std::chrono::milliseconds(7265000), std::chrono::hours(2));
     61     test(std::chrono::milliseconds(7265000), std::chrono::minutes(121));
     62     test(std::chrono::milliseconds(7265000), std::chrono::seconds(7265));
     63     test(std::chrono::milliseconds(7265000), std::chrono::milliseconds(7265000));
     64     test(std::chrono::milliseconds(7265000), std::chrono::microseconds(7265000000LL));
     65     test(std::chrono::milliseconds(7265000), std::chrono::nanoseconds(7265000000000LL));
     66     test(std::chrono::milliseconds(7265000),
     67          std::chrono::duration<double, std::ratio<3600> >(7265./3600));
     68     test(std::chrono::duration<int, std::ratio<2, 3> >(9),
     69          std::chrono::duration<int, std::ratio<3, 5> >(10));
     70 #if TEST_STD_VER > 11
     71     {
     72     test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::hours,    2> ();
     73     test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::minutes,121> ();
     74     test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::seconds,7265> ();
     75     test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::milliseconds,7265000> ();
     76     test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::microseconds,7265000000LL> ();
     77     test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::nanoseconds,7265000000000LL> ();
     78     typedef std::chrono::duration<int, std::ratio<3, 5>> T1;
     79     test_constexpr<std::chrono::duration<int, std::ratio<2, 3>>, 9, T1, 10> ();
     80     }
     81 #endif
     82 }
     83