// xtime.cpp ----------------------------------------------------------// // Copyright 2008 Howard Hinnant // Copyright 2008 Beman Dawes // Copyright 2009 Vicente J. Botet Escriba // Distributed under the Boost Software License, Version 1.0. // See http://www.boost.org/LICENSE_1_0.txt /* This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. Many thanks to Howard for making his code available under the Boost license. The original code was modified to conform to Boost conventions and to section 20.9 Time utilities [time] of the C++ committee's working paper N2798. See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. time2_demo contained this comment: Much thanks to Andrei Alexandrescu, Walter Brown, Peter Dimov, Jeff Garland, Terry Golubiewski, Daniel Krugler, Anthony Williams. */ #include #include #include using namespace boost::chrono; // Example round_up utility: converts d to To, rounding up for inexact conversions // Being able to *easily* write this function is a major feature! template To round_up(duration d) { To result = duration_cast(d); if (result < d) ++result; return result; } // demonstrate interaction with xtime-like facility: // msvc defines ::xtime in , so we use xtime_ struct xtime_ { long sec; unsigned long usec; }; template xtime_ to_xtime_truncate(duration d) { xtime_ xt; xt.sec = static_cast(duration_cast(d).count()); xt.usec = static_cast(duration_cast(d - seconds(xt.sec)).count()); return xt; } template xtime_ to_xtime_round_up(duration d) { xtime_ xt; xt.sec = static_cast(duration_cast(d).count()); xt.usec = static_cast(round_up(d - seconds(xt.sec)).count()); return xt; } microseconds from_xtime(xtime_ xt) { return seconds(xt.sec) + microseconds(xt.usec); } void print(xtime_ xt) { std::cout << '{' << xt.sec << ',' << xt.usec << "}\n"; } void test_with_xtime() { std::cout << "test_with_xtime\n"; xtime_ xt = to_xtime_truncate(seconds(3) + milliseconds(251)); print(xt); milliseconds ms = duration_cast(from_xtime(xt)); std::cout << ms.count() << " milliseconds\n"; xt = to_xtime_round_up(ms); print(xt); xt = to_xtime_truncate(seconds(3) + nanoseconds(999)); print(xt); xt = to_xtime_round_up(seconds(3) + nanoseconds(999)); print(xt); } int main() { test_with_xtime(); return 0; }