min_time_point.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // min_time_point.cpp ----------------------------------------------------------//
  2. // Copyright 2008 Howard Hinnant
  3. // Copyright 2008 Beman Dawes
  4. // Copyright 2009 Vicente J. Botet Escriba
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // See http://www.boost.org/LICENSE_1_0.txt
  7. /*
  8. This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which
  9. was derived by Beman Dawes from Howard Hinnant's time2_demo prototype.
  10. Many thanks to Howard for making his code available under the Boost license.
  11. The original code was modified to conform to Boost conventions and to section
  12. 20.9 Time utilities [time] of the C++ committee's working paper N2798.
  13. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf.
  14. time2_demo contained this comment:
  15. Much thanks to Andrei Alexandrescu,
  16. Walter Brown,
  17. Peter Dimov,
  18. Jeff Garland,
  19. Terry Golubiewski,
  20. Daniel Krugler,
  21. Anthony Williams.
  22. */
  23. #include <boost/chrono/typeof/boost/chrono/chrono.hpp>
  24. #include <boost/type_traits.hpp>
  25. #include <iostream>
  26. using namespace boost::chrono;
  27. template <class Rep, class Period>
  28. void print_duration(std::ostream& os, duration<Rep, Period> d)
  29. {
  30. os << d.count() << " * " << Period::num << '/' << Period::den << " seconds\n";
  31. }
  32. namespace my_ns {
  33. // Example min utility: returns the earliest time_point
  34. // Being able to *easily* write this function is a major feature!
  35. template <class Clock, class Duration1, class Duration2>
  36. inline
  37. typename boost::common_type<time_point<Clock, Duration1>,
  38. time_point<Clock, Duration2> >::type
  39. min BOOST_PREVENT_MACRO_SUBSTITUTION (time_point<Clock, Duration1> t1, time_point<Clock, Duration2> t2)
  40. {
  41. return t2 < t1 ? t2 : t1;
  42. }
  43. }
  44. void test_min()
  45. {
  46. #if 1
  47. typedef time_point<system_clock,
  48. boost::common_type<system_clock::duration, seconds>::type> T1;
  49. typedef time_point<system_clock,
  50. boost::common_type<system_clock::duration, nanoseconds>::type> T2;
  51. typedef boost::common_type<T1, T2>::type T3;
  52. /*auto*/ T1 t1 = system_clock::now() + seconds(3);
  53. /*auto*/ T2 t2 = system_clock::now() + nanoseconds(3);
  54. /*auto*/ T3 t3 = (my_ns::min)(t1, t2);
  55. #else
  56. BOOST_AUTO(t1, system_clock::now() + seconds(3));
  57. BOOST_AUTO(t2, system_clock::now() + nanoseconds(3));
  58. BOOST_AUTO(t3, (min)(t1, t2));
  59. #endif
  60. print_duration(std::cout, t1 - t3);
  61. print_duration(std::cout, t2 - t3);
  62. }
  63. int main()
  64. {
  65. test_min();
  66. return 0;
  67. }