time_point_cast_pass.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // Adaptation to Boost of the libcxx
  10. // Copyright 2010 Vicente J. Botet Escriba
  11. // Distributed under the Boost Software License, Version 1.0.
  12. // See http://www.boost.org/LICENSE_1_0.txt
  13. #include <boost/chrono/chrono.hpp>
  14. #include <boost/type_traits.hpp>
  15. #include <boost/detail/lightweight_test.hpp>
  16. #if !defined(BOOST_NO_CXX11_STATIC_ASSERT)
  17. #define NOTHING ""
  18. #endif
  19. #ifdef BOOST_NO_CXX11_CONSTEXPR
  20. #define BOOST_CONSTEXPR_ASSERT(C) BOOST_TEST(C)
  21. #else
  22. #include <boost/static_assert.hpp>
  23. #define BOOST_CONSTEXPR_ASSERT(C) BOOST_STATIC_ASSERT(C)
  24. #endif
  25. template <class FromDuration, class ToDuration>
  26. void
  27. test(const FromDuration& df, const ToDuration& d)
  28. {
  29. typedef boost::chrono::system_clock Clock;
  30. typedef boost::chrono::time_point<Clock, FromDuration> FromTimePoint;
  31. typedef boost::chrono::time_point<Clock, ToDuration> ToTimePoint;
  32. FromTimePoint f(df);
  33. ToTimePoint t(d);
  34. //~ #if defined(BOOST_NO_CXX11_DECLTYPE)
  35. //~ typedef BOOST_TYPEOF_TPL(boost::chrono::time_point_cast<ToDuration>(f)) R;
  36. //~ #else
  37. //~ typedef decltype(boost::chrono::time_point_cast<ToDuration>(f)) R;
  38. //~ #endif
  39. //~ BOOST_CHRONO_STATIC_ASSERT((boost::is_same<R, ToTimePoint>::value), NOTHING, ());
  40. BOOST_TEST(boost::chrono::time_point_cast<ToDuration>(f) == t);
  41. }
  42. int main()
  43. {
  44. test(boost::chrono::milliseconds(7265000), boost::chrono::hours(2));
  45. test(boost::chrono::milliseconds(7265000), boost::chrono::minutes(121));
  46. test(boost::chrono::milliseconds(7265000), boost::chrono::seconds(7265));
  47. test(boost::chrono::milliseconds(7265000), boost::chrono::milliseconds(7265000));
  48. test(boost::chrono::milliseconds(7265000), boost::chrono::microseconds(7265000000LL));
  49. test(boost::chrono::milliseconds(7265000), boost::chrono::nanoseconds(7265000000000LL));
  50. test(boost::chrono::milliseconds(7265000),
  51. boost::chrono::duration<double, boost::ratio<3600> >(7265./3600));
  52. test(boost::chrono::duration<int, boost::ratio<2, 3> >(9),
  53. boost::chrono::duration<int, boost::ratio<3, 5> >(10));
  54. {
  55. typedef boost::chrono::system_clock Clock;
  56. typedef boost::chrono::time_point<Clock, boost::chrono::milliseconds> FromTimePoint;
  57. typedef boost::chrono::time_point<Clock, boost::chrono::hours> ToTimePoint;
  58. BOOST_CONSTEXPR FromTimePoint f(boost::chrono::milliseconds(7265000));
  59. BOOST_CONSTEXPR ToTimePoint tph = boost::chrono::time_point_cast<boost::chrono::hours>(f);
  60. BOOST_CONSTEXPR_ASSERT(tph.time_since_epoch().count() == 2);
  61. }
  62. return boost::report_errors();
  63. }