Chris@16: // boost/chrono/round.hpp ------------------------------------------------------------// Chris@16: Chris@16: // (C) Copyright Howard Hinnant Chris@16: // Copyright 2011 Vicente J. Botet Escriba Chris@16: Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: // See http://www.boost.org/libs/chrono for documentation. Chris@16: Chris@16: #ifndef BOOST_CHRONO_ROUND_HPP Chris@16: #define BOOST_CHRONO_ROUND_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: //#include Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: namespace chrono Chris@16: { Chris@16: Chris@16: /** Chris@16: * rounds to nearest, to even on tie Chris@16: */ Chris@16: template Chris@16: To round(const duration& d) Chris@16: { Chris@101: typedef typename common_type >::type result_type; Chris@101: result_type diff0; Chris@101: result_type diff1; Chris@101: Chris@16: To t0 = duration_cast(d); Chris@16: To t1 = t0; Chris@101: if (t0>d) { Chris@101: --t1; Chris@101: diff0 = t0 - d; Chris@101: diff1 = d - t1; Chris@101: } else { Chris@101: ++t1; Chris@101: diff0 = d - t0; Chris@101: diff1 = t1 - d; Chris@101: } Chris@101: Chris@16: if (diff0 == diff1) Chris@16: { Chris@16: if (t0.count() & 1) Chris@16: return t1; Chris@16: return t0; Chris@16: } Chris@16: else if (diff0 < diff1) Chris@16: return t0; Chris@16: return t1; Chris@16: } Chris@16: Chris@16: } // namespace chrono Chris@16: } // namespace boost Chris@16: Chris@16: #endif