round.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // boost/chrono/round.hpp ------------------------------------------------------------//
  2. // (C) Copyright Howard Hinnant
  3. // Copyright 2011 Vicente J. Botet Escriba
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org/libs/chrono for documentation.
  7. #ifndef BOOST_CHRONO_ROUND_HPP
  8. #define BOOST_CHRONO_ROUND_HPP
  9. #include <boost/chrono/duration.hpp>
  10. #include <boost/chrono/duration.hpp>
  11. //#include <boost/chrono/typeof/boost/chrono/chrono.hpp>
  12. namespace boost
  13. {
  14. namespace chrono
  15. {
  16. /**
  17. * rounds to nearest, to even on tie
  18. */
  19. template <class To, class Rep, class Period>
  20. To round(const duration<Rep, Period>& d)
  21. {
  22. To t0 = duration_cast<To>(d);
  23. To t1 = t0;
  24. ++t1;
  25. #if 0
  26. // Avoid the user of BOOST_AUTO to make the library portable to Sun, PGI, ..
  27. BOOST_AUTO(diff0, d - t0);
  28. BOOST_AUTO(diff1, t1 - d);
  29. #else
  30. typedef typename common_type<To, duration<Rep, Period> >::type result_type;
  31. result_type diff0 = d - t0;
  32. result_type diff1 = t1 - d;
  33. #endif
  34. if (diff0 == diff1)
  35. {
  36. if (t0.count() & 1)
  37. return t1;
  38. return t0;
  39. }
  40. else if (diff0 < diff1)
  41. return t0;
  42. return t1;
  43. }
  44. } // namespace chrono
  45. } // namespace boost
  46. #endif