Professional C__ - Marc Gregoire [279]
duration The following example demonstrates several aspects of durations. It shows you how to define durations, how to perform arithmetic operations on them, and how to convert one duration into another duration with a different tick period: // Specify a duration where each tick is 60 seconds duration cout << d1.count() << endl; // Specify a duration represented by a double with each tick // equal to 1 second and assign the largest possible duration to it. duration d2 = d2.max(); cout << d2.count() << endl; // Define 2 durations: // For the first duration, each tick is 1 minute // For the second duration, each tick is 1 second duration duration // Compare both durations if (d3 > d4) cout << "d3 > d4" << endl; else cout << "d3 <= d4" << endl; // Increment d4 with 1 resulting in 15 seconds ++d4; // Multiply d4 by 2 resulting in 30 seconds d4 *= 2; // Add both durations and store as minutes duration // Add both durations and store as seconds duration cout << d3.count() << " minutes + " << d4.count() << " seconds = " << d5.count() << " minutes or " << d6.count() << " seconds" << endl; // Create a duration of 30 seconds duration // Convert the seconds of d7 to minutes duration cout << d7.count() << " seconds = " << d8.count() << " minutes" << endl; Code snippet from Chrono\durations.cpp The output is as follows: 123 1.79769e+308 d3 > d4 10 minutes + 30 seconds = 10.5 minutes or 630 seconds 30 seconds = 0.5 minutes The second line in the output represents the largest possible duration with type double. The exact value might be different depending on your compiler. Pay special attention to the following two lines: duration duration They both calculate d3+d4 but the first one stores it as a floating point value representing minutes while the second one stores the result as an integer representing seconds. Conversion from minutes to seconds or vice versa happens automatically. The following two lines from the preceding example demonstrate how to convert between different units of time: duration duration The first line defines a duration representing 30 seconds. The second line converts these 30 seconds into minutes, resulting in 0.5 minutes. Converting in this direction can result in a non-integral value and thus requires you to use a duration represented by a floating point type; otherwise, you will get some cryptic compiler errors. The following lines, for example, will not compile because d8 is using long instead of a floating point type: duration duration Converting in the other direction does not require floating point types if the source is an integral type, because the result is always an integral value if you started with an integral value. For example, the following lines convert 10 minutes into seconds, both represented by the integral type long: duration duration The library also provides the following standard duration types, where X stands for “signed integer type of at least”: typedef duration typedef duration typedef duration typedef duration typedef duration typedef duration The exact type of X depends on your compiler, but the C++ standard requires it to be a signed integer type of at least the specified size. The preceding typedefs