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