| 1 | // xtime.cpp ----------------------------------------------------------// |
| 2 | |
| 3 | // Copyright 2008 Howard Hinnant |
| 4 | // Copyright 2008 Beman Dawes |
| 5 | // Copyright 2009 Vicente J. Botet Escriba |
| 6 | |
| 7 | // Distributed under the Boost Software License, Version 1.0. |
| 8 | // See http://www.boost.org/LICENSE_1_0.txt |
| 9 | |
| 10 | /* |
| 11 | This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which |
| 12 | was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. |
| 13 | Many thanks to Howard for making his code available under the Boost license. |
| 14 | The original code was modified to conform to Boost conventions and to section |
| 15 | 20.9 Time utilities [time] of the C++ committee's working paper N2798. |
| 16 | See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. |
| 17 | |
| 18 | time2_demo contained this comment: |
| 19 | |
| 20 | Much thanks to Andrei Alexandrescu, |
| 21 | Walter Brown, |
| 22 | Peter Dimov, |
| 23 | Jeff Garland, |
| 24 | Terry Golubiewski, |
| 25 | Daniel Krugler, |
| 26 | Anthony Williams. |
| 27 | */ |
| 28 | |
| 29 | #include <boost/chrono/chrono.hpp> |
| 30 | #include <boost/type_traits.hpp> |
| 31 | |
| 32 | #include <iostream> |
| 33 | |
| 34 | using namespace boost::chrono; |
| 35 | |
| 36 | // Example round_up utility: converts d to To, rounding up for inexact conversions |
| 37 | // Being able to *easily* write this function is a major feature! |
| 38 | template <class To, class Rep, class Period> |
| 39 | To |
| 40 | round_up(duration<Rep, Period> d) |
| 41 | { |
| 42 | To result = duration_cast<To>(d); |
| 43 | if (result < d) |
| 44 | ++result; |
| 45 | return result; |
| 46 | } |
| 47 | |
| 48 | // demonstrate interaction with xtime-like facility: |
| 49 | |
| 50 | // msvc defines ::xtime in <mutex>, so we use xtime_ |
| 51 | struct xtime_ |
| 52 | { |
| 53 | long sec; |
| 54 | unsigned long usec; |
| 55 | }; |
| 56 | |
| 57 | template <class Rep, class Period> |
| 58 | xtime_ |
| 59 | to_xtime_truncate(duration<Rep, Period> d) |
| 60 | { |
| 61 | xtime_ xt; |
| 62 | xt.sec = static_cast<long>(duration_cast<seconds>(d).count()); |
| 63 | xt.usec = static_cast<long>(duration_cast<microseconds>(d - seconds(xt.sec)).count()); |
| 64 | return xt; |
| 65 | } |
| 66 | |
| 67 | template <class Rep, class Period> |
| 68 | xtime_ |
| 69 | to_xtime_round_up(duration<Rep, Period> d) |
| 70 | { |
| 71 | xtime_ xt; |
| 72 | xt.sec = static_cast<long>(duration_cast<seconds>(d).count()); |
| 73 | xt.usec = static_cast<unsigned long>(round_up<microseconds>(d - seconds(xt.sec)).count()); |
| 74 | return xt; |
| 75 | } |
| 76 | |
| 77 | microseconds |
| 78 | from_xtime(xtime_ xt) |
| 79 | { |
| 80 | return seconds(xt.sec) + microseconds(xt.usec); |
| 81 | } |
| 82 | |
| 83 | void print(xtime_ xt) |
| 84 | { |
| 85 | std::cout << '{' << xt.sec << ',' << xt.usec << "}\n" ; |
| 86 | } |
| 87 | |
| 88 | void test_with_xtime() |
| 89 | { |
| 90 | std::cout << "test_with_xtime\n" ; |
| 91 | xtime_ xt = to_xtime_truncate(d: seconds(3) + milliseconds(251)); |
| 92 | print(xt); |
| 93 | milliseconds ms = duration_cast<milliseconds>(fd: from_xtime(xt)); |
| 94 | std::cout << ms.count() << " milliseconds\n" ; |
| 95 | xt = to_xtime_round_up(d: ms); |
| 96 | print(xt); |
| 97 | xt = to_xtime_truncate(d: seconds(3) + nanoseconds(999)); |
| 98 | print(xt); |
| 99 | xt = to_xtime_round_up(d: seconds(3) + nanoseconds(999)); |
| 100 | print(xt); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | int main() |
| 105 | { |
| 106 | test_with_xtime(); |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | |