1 //  miscellaneous.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 // miscellaneous tests and demos:
35 
36 #include <cassert>
37 #include <iostream>
38 
39 using namespace boost::chrono;
40 
physics_function(duration<double> d)41 void physics_function(duration<double> d)
42 {
43     std::cout << "d = " << d.count() << '\n';
44 }
45 
drive_physics_function()46 void drive_physics_function()
47 {
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) );
53 }
54 
test_range()55 void test_range()
56 {
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";
64 }
65 
test_extended_range()66 void test_extended_range()
67 {
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";
75 }
76 
77 template <class Rep, class Period>
inspect_duration(boost::chrono::duration<Rep,Period> d,const std::string & name)78 void inspect_duration(boost::chrono::duration<Rep, Period> d, const std::string& name)
79 {
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)
86     {
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";
90     }
91     else if (boost::is_integral<Rep>::value)
92     {
93         std::cout << "integral\n";
94         d = Duration(Rep(1));
95         boost::chrono::duration<double> dsec = d;
96         std::cout << "The precision is " << dsec.count() << " seconds.\n";
97     }
98     else
99     {
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";
104     }
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;
108     Years years = d;
109     std::cout << "The range is +/- " << years.count() << " years.\n";
110     std::cout << "sizeof(" << name << ") = " << sizeof(d) << '\n';
111 }
112 
inspect_all()113 void inspect_all()
114 {
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>");
124 }
125 
test_milliseconds()126 void test_milliseconds()
127 {
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";
135     else
136         std::cout << "failure: " << msdiff.count() << '\n';
137 }
138 
main()139 int main()
140 {
141     using namespace boost;
142     drive_physics_function();
143     test_range();
144     test_extended_range();
145     inspect_all();
146     test_milliseconds();
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";
154     return 0;
155 }
156 
157