]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/chrono/example/miscellaneous.cpp
1 // miscellaneous.cpp ----------------------------------------------------------//
3 // Copyright 2008 Howard Hinnant
4 // Copyright 2008 Beman Dawes
5 // Copyright 2009 Vicente J. Botet Escriba
7 // Distributed under the Boost Software License, Version 1.0.
8 // See http://www.boost.org/LICENSE_1_0.txt
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.
18 time2_demo contained this comment:
20 Much thanks to Andrei Alexandrescu,
29 #include <boost/chrono/chrono.hpp>
30 #include <boost/type_traits.hpp>
34 // miscellaneous tests and demos:
39 using namespace boost::chrono
;
41 void physics_function(duration
<double> d
)
43 std::cout
<< "d = " << d
.count() << '\n';
46 void drive_physics_function()
48 physics_function(nanoseconds(3));
49 physics_function(hours(3));
50 physics_function(duration
<double>(2./3));
51 std::cout
.precision(16);
52 physics_function( hours(3) + nanoseconds(-3) );
57 using namespace boost::chrono
;
58 hours h1
= hours(24 * ( 365 * 292 + 292/4));
59 nanoseconds n1
= h1
+ nanoseconds(1);
60 nanoseconds delta
= n1
- h1
;
61 std::cout
<< "292 years of hours = " << h1
.count() << "hr\n";
62 std::cout
<< "Add a nanosecond = " << n1
.count() << "ns\n";
63 std::cout
<< "Find the difference = " << delta
.count() << "ns\n";
66 void test_extended_range()
68 using namespace boost::chrono
;
69 hours h1
= hours(24 * ( 365 * 244000 + 244000/4));
70 /*auto*/ microseconds u1
= h1
+ microseconds(1);
71 /*auto*/ microseconds delta
= u1
- h1
;
72 std::cout
<< "244,000 years of hours = " << h1
.count() << "hr\n";
73 std::cout
<< "Add a microsecond = " << u1
.count() << "us\n";
74 std::cout
<< "Find the difference = " << delta
.count() << "us\n";
77 template <class Rep
, class Period
>
78 void inspect_duration(boost::chrono::duration
<Rep
, Period
> d
, const std::string
& name
)
80 typedef boost::chrono::duration
<Rep
, Period
> Duration
;
81 std::cout
<< "********* " << name
<< " *********\n";
82 std::cout
<< "The period of " << name
<< " is " << (double)Period::num
/Period::den
<< " seconds.\n";
83 std::cout
<< "The frequency of " << name
<< " is " << (double)Period::den
/Period::num
<< " Hz.\n";
84 std::cout
<< "The representation is ";
85 if (boost::is_floating_point
<Rep
>::value
)
87 std::cout
<< "floating point\n";
88 std::cout
<< "The precision is the most significant ";
89 std::cout
<< std::numeric_limits
<Rep
>::digits10
<< " decimal digits.\n";
91 else if (boost::is_integral
<Rep
>::value
)
93 std::cout
<< "integral\n";
95 boost::chrono::duration
<double> dsec
= d
;
96 std::cout
<< "The precision is " << dsec
.count() << " seconds.\n";
100 std::cout
<< "a class type\n";
101 d
= Duration(Rep(1));
102 boost::chrono::duration
<double> dsec
= d
;
103 std::cout
<< "The precision is " << dsec
.count() << " seconds.\n";
105 d
= Duration((std::numeric_limits
<Rep
>::max
)());
106 using namespace boost::chrono
;
107 typedef duration
<double, boost::ratio_multiply
<boost::ratio
<24*3652425,10000>, hours::period
>::type
> Years
;
109 std::cout
<< "The range is +/- " << years
.count() << " years.\n";
110 std::cout
<< "sizeof(" << name
<< ") = " << sizeof(d
) << '\n';
115 using namespace boost::chrono
;
116 std::cout
.precision(6);
117 inspect_duration(nanoseconds(), "nanoseconds");
118 inspect_duration(microseconds(), "microseconds");
119 inspect_duration(milliseconds(), "milliseconds");
120 inspect_duration(seconds(), "seconds");
121 inspect_duration(minutes(), "minutes");
122 inspect_duration(hours(), "hours");
123 inspect_duration(duration
<double>(), "duration<double>");
126 void test_milliseconds()
128 using namespace boost::chrono
;
129 milliseconds
ms(250);
130 ms
+= milliseconds(1);
131 milliseconds
ms2(150);
132 milliseconds msdiff
= ms
- ms2
;
133 if (msdiff
== milliseconds(101))
134 std::cout
<< "success\n";
136 std::cout
<< "failure: " << msdiff
.count() << '\n';
141 using namespace boost
;
142 drive_physics_function();
144 test_extended_range();
147 inspect_duration(common_type
<duration
<double>, hours
, microseconds
>::type(),
148 "common_type<duration<double>, hours, microseconds>::type");
149 duration
<double, boost::milli
> d
= milliseconds(3) * 2.5;
150 inspect_duration(milliseconds(3) * 2.5, "milliseconds(3) * 2.5");
151 std::cout
<< d
.count() << '\n';
152 // milliseconds ms(3.5); // doesn't compile
153 // std::cout << "milliseconds ms(3.5) doesn't compile\n";