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 template <class FromDuration, class ToDuration>
     23 void
     24 test(const FromDuration& df, const ToDuration& d)
     25 {
     26     typedef std::chrono::system_clock Clock;
     27     typedef std::chrono::time_point<Clock, FromDuration> FromTimePoint;
     28     typedef std::chrono::time_point<Clock, ToDuration> ToTimePoint;
     29     {
     30     FromTimePoint f(df);
     31     ToTimePoint t(d);
     32     typedef decltype(std::chrono::time_point_cast<ToDuration>(f)) R;
     33     static_assert((std::is_same<R, ToTimePoint>::value), "");
     34     assert(std::chrono::time_point_cast<ToDuration>(f) == t);
     35     }
     36 }
     37 
     38 #if _LIBCPP_STD_VER > 11
     39 
     40 template<class FromDuration, long long From, class ToDuration, long long To>
     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::time_point_cast<ToDuration>(f) == t, "");
     50     }
     51 
     52 }
     53 
     54 #endif
     55 
     56 int main()
     57 {
     58     test(std::chrono::milliseconds(7265000), std::chrono::hours(2));
     59     test(std::chrono::milliseconds(7265000), std::chrono::minutes(121));
     60     test(std::chrono::milliseconds(7265000), std::chrono::seconds(7265));
     61     test(std::chrono::milliseconds(7265000), std::chrono::milliseconds(7265000));
     62     test(std::chrono::milliseconds(7265000), std::chrono::microseconds(7265000000LL));
     63     test(std::chrono::milliseconds(7265000), std::chrono::nanoseconds(7265000000000LL));
     64     test(std::chrono::milliseconds(7265000),
     65          std::chrono::duration<double, std::ratio<3600> >(7265./3600));
     66     test(std::chrono::duration<int, std::ratio<2, 3> >(9),
     67          std::chrono::duration<int, std::ratio<3, 5> >(10));
     68 #if _LIBCPP_STD_VER > 11
     69     {
     70     test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::hours,    2> ();
     71     test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::minutes,121> ();
     72     test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::seconds,7265> ();
     73     test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::milliseconds,7265000> ();
     74     test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::microseconds,7265000000LL> ();
     75     test_constexpr<std::chrono::milliseconds, 7265000, std::chrono::nanoseconds,7265000000000LL> ();
     76     typedef std::chrono::duration<int, std::ratio<3, 5>> T1;
     77     test_constexpr<std::chrono::duration<int, std::ratio<2, 3>>, 9, T1, 10> ();
     78     }
     79 #endif
     80 }
     81