Online Book Reader

Home Category

Professional C__ - Marc Gregoire [280]

By Root 1169 0
make use of the predefined SI ratio typedefs as described earlier in this chapter. The following is an example of how to use these predefined durations. The code first defines a variable t, which is the result of 1 hour + 23 minutes + 45 seconds. The auto keyword is used to let the compiler automatically figure out the exact type of t. The second line uses the constructor of the predefined seconds duration to convert the value of t to seconds and write the result to the console:

auto t = hours(1) + minutes(23) + seconds(45);

cout << seconds(t).count() << " seconds" << endl;

Code snippet from Chrono\durations.cpp

Because the standard requires that the predefined durations use integer types, there can be compiler errors if a conversion could end up with a non-integral value. While integer division normally truncates, in the case of durations, which are implemented by ratio types, the compiler declares any computation that could result in a non-zero remainder as a compile-time error. For example, the following will not compile because converting 90 seconds results in 1.5 minutes:

seconds s(90);

minutes m(s);

However, the following will not compile either, even though 60 seconds is exactly 1 minute. It is flagged as a compile-time error because converting from seconds to minutes could result in non-integral values:

seconds s(60);

minutes m(s);

Converting in the other direction works perfectly fine because the minutes duration is an integral value and converting it to seconds always results in an integral value:

minutes m(2);

seconds s(m);

Clock

A clock is a class consisting of a time_point and a duration. The time_point type is discussed in detail in the next section, but those details are not required to understand how clocks work. However, time_points themselves depend on clocks, so it’s important to know the details of clocks first.

Three clocks are defined by the standard. The first is called system_clock and represents the wall clock time from the system-wide realtime clock. The second is called steady_clock, which is a clock that guarantees its time_point will never decrease. The third is the high_resolution_clock, which has the shortest possible tick period. Depending on your compiler, it is possible for the high_resolution_clock to be a synonym for steady_clock or system_clock.

Every clock has a static now() method to get the current time as a time_point. The system_clock also defines two static helper functions for converting time_points to and from the time_t C-style time representation. The first is called to_time_t() converting a given time_point to a time_t; the second is called from_time_t(), which will return a time_point initialized with a given time_t value. The time_t type is defined in the header file.

The following example shows a complete program, which gets the current time from the system and outputs the time in a human readable format on the console. The localtime() function converts a time_t to a local time represented by tm and is defined in the header file. The C++11 put_time() stream manipulator, defined in the header is discussed in Chapter 15:

// Get current time as a time_point

system_clock::time_point tpoint = system_clock::now();

// Convert to a time_t

time_t tt = system_clock::to_time_t(tpoint);

// Convert to local time

tm* t = localtime(&tt);

// Write the time to the console

cout << put_time(t, "%H:%M:%S") << endl;

Code snippet from Chrono\now_put_time.cpp

If your compiler does not yet support the put_time() manipulator, you can use the C-style strftime() function, defined in , as follows. Using the old strftime() function, requires you to supply a buffer that is big enough to hold the human readable representation of the given time:

// Get current time as a time_point

system_clock::time_point tpoint = system_clock::now();

// Convert to a time_t

time_t tt = system_clock::to_time_t(tpoint);

// Convert to local time

tm* t = localtime(&tt);

// Convert to readable format

char buffer[80] = {0};

strftime(buffer, sizeof(buffer),

Return Main Page Previous Page Next Page

®Online Book Reader