1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
Clocks are used for measuring time. bf(C++) offers several predefined
emi(clock) types, and all but one of them are defined in the tt(std::chrono)
namespace. The exception is the clock tt(std::filesystem::__file_clock) (see
section ref(FILECLOCK) for its details).
Before using the tt(chrono) clocks the tthi(chrono) header file must be
included.
We need clock types when defining points in time (see the next section). All
predefined clock types define the following types:
itemization(
it() the clock's duration type: tt(Clock::duration) (predefined clock
types use tt(nanoseconds)). E.g., tt(system_clock::duration
oneDay{ 24h });
it() the clock's resolution type: tt(Clock::period)
(predefined clock types use tt(nano)). E.g.,
tt(cout << system_clock::period::den << '\n');
it() the clock's type that is used to store amounts of time:
tt(Clock::rep) (predefined clock types use
tt(int64_t)). E.g., tt(system_clock::rep amount = 0);
it() the clock's type that is used to store time points (described in the
next section): tt(Clock::time_point) (predefined clock types use
tt(time_point<system_clock, nanoseconds>)) E.g.,
tt(system_clock::time_point start).
)
All clock types have a member ti(now) returning the clock type's
tt(time_point) corresponding to the current time (relative to the clock's
epoch). It is a static member and can be used this way:
tt(system_clock::time_point tp = system_clock::now()).
There are three predefined clock types in the chrono namespace:
itemization(
iti(system_clock) is the `wall clock', using the system's real time
clock;
iti(steady_clock) is a clock whose time increases in parallel with the
increase of real time;
iti(high_resolution_clock) is the computer's fastest clock (i.e., the
clock having the shortest timer-tick interval). In practice this is
the same clock as tt(system_clock).
)
These clock types also
hi(to_sys (__file_clock))
In addition, the tt(__file_clock) clock type is defined in the
tt(std::filesystem) namespace. The epoch time point of tt(__file_clock)
differs from the epoch time used by the other clock types, but
tt(__file_clock) has a static member tt(to_sys(__file_clock::time_point))
converting tt(__file_clock::time_points) to tt(system_clock::time_points)
(tt(__file_clock) is covered in more detail in section ref(FILECLOCK)).
In addition to tt(now) the classes tt(system_clock) and
tt(high_resolution_clock) (referred to as tt(Clock) below) offer these two
static members:
itemization(
itht(to_time_t)
(std::time_t Clock::to_time_t(Clock::time_point const &tp))nl()
a tt(std::time_t)hi(time_t) value (the same type as returned
by bf(C)'s bf(time)(2) function) representing the same point in
time as tt(timePoint).
itht(from_time_t)(Clock::time_point Clock::from_time_t(std::time_t
seconds))nl()
a tt(time_point) representing the same point in time as tt(time_t).
)
The example illustrates how these functions can be called:
verb( system_clock::from_time_t(
system_clock::to_time_t(
system_clock::from_time_t(
time(0);
)
)
);)
|