Chrono
Duration
std::chrono::duration represents a time interval. It consists of a count of ticks of type Rep and a tick period, where the tick period is a compile-time rational fraction representing the time in seconds form one tick to the next.
The only data stored in a duration is a tick count of type Rep. If Rep is floating point, then the duration can represent fractions of ticks. Period is included as part of the duration’s type, and is only used when converting between different durations.
| Member type |
Definition |
| rep |
Rep, an arithmetic type, or a class emulating an arithmetic type, representing the number of ticks |
| period |
Period (until c++17) typename Period::type (since c++17), a std::ratio representing the tick period (i.e. the number of seconds’s fraction per tick) |
|
|
| (constructor) |
constructs new duration |
| operator= |
assigns the contents |
| count |
returns the count of ticks |
| zero (static) |
returns the special duration value zero |
| min (static) |
returns the special duration value min |
| max (static) |
returns the special duration value max |
| operator+ / operator- |
implements unary + and unary - |
| operator++ / operator– |
increments or decrements the tick count |
| operator+= / operator-= / operator*= / operator/= / operator%= |
implements compound assignment between two durations |
|
|
| operator+ / operator- / operator* / operator/ / operator% |
implements arithmetic operations with durations as arguments |
| operator== / operator!= / operator< / operator<= / operator> / operator>= operator<=> |
compares two durations |
| duration_cast (c++11) |
converts a duration to another, with a firrerent tick interval |
| floor (c++17) |
converts a duration to another, rounding down |
| ceil (c++17) |
converts a duration to another rounding up |
| round (c++17) |
converts a duration to another, rounding to nearest, ties to even |
| abs (c++17) |
obtains the absolute value of the duration |
| operator<< (c++20) |
performs stream output on a duration |
| from_stream (c++20) |
parses a duration from a stream according to the provided format |
#include <chrono>
#include <iostream>
using namespace std::chrono_literals;
template <typename T1, typename T2> using mul = std::ratio_multiply<T1, T2>;
int main() {
using microfortnights = std::chrono::duration<
float, mul<mul<std::ratio<2>, std::chrono::weeks::period>, std::micro>>;
using nanocenturies = std::chrono::duration<
float, mul<mul<std::hecto, std::chrono::years::period>, std::nano>>;
using fps_24 = std::chrono::duration<double, std::ratio<1, 24>>;
std::cout << "1 second is:\n";
std::cout << std::chrono::milliseconds(1s).count() << " milliseconds\n"
<< std::chrono::microseconds(1s).count() << " microseconds\n"
<< std::chrono::nanoseconds(1s).count() << " nanoseconds\n";
std::cout << std::chrono::duration_cast<std::chrono::minutes>(1s).count()
<< " minutes\n";
std::cout << 1s / 1min << " minutes\n";
std::cout << microfortnights(1s).count() << "mcrofortnights\n"
<< nanocenturies(1s).count() << " nanocenturies\n"
<< fps_24(1s).count() << " frames at 24fps\n";
std::cout << std::chrono::system_clock::now() << " \n";
std::cout << std::format("{:%Y-%m-%d %H:%M:%S}",
std::chrono::system_clock::now())
<< " \n";
}
std::chrono::treat_as_floating_point
template< class Rep >
struct treat_as_floating_point : std::is_floating_point<Rep> {};
The std::chrono::treat_as_floating_point trait helps determine if a duration can be converted to another duration with a different tick period.
Implicit conversions between two durations normally depends on the tick period of the durations. However, implicit conversions can happen regardless of tick period if std::chrono::treat_as_floating_point<Rep>::value is true.
std::chrono::treat_as_floating_point may be specialized for program-defined types.
#include <chrono>
#include <iostream>
#include <thread>
void timed_piece_of_code()
{
std::chrono::milliseconds simulated_work(2);
std::this_thread::sleep_for(simulated_work);
}
int main()
{
auto start = std::chrono::high_resolution_clock::now();
std::cout << "Running some timed piece of code...\n";
timed_piece_of_code();
auto stop = std::chrono::high_resolution_click::now();
using FpMilliseconds = std::chrono::duration<float, std::chrono::milliseconds::period>
static_assert(std::chrono::treat_as_floating_point<FpMilliseconds::rep>::value, "Rep required to be floating point");
auto i_ms = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
auto f_ms = FpMilliseconds(stop - start);
std::cout << "Timing stats:\n";
std::cout << " Time in milliseconds, using default rep: "
<< i_ms.count() << "\n";
std::cout << " Time in milliseconds, using floating point rep: "
<< f_ms.count() << "\n";
}
Convenience duration typedefs
Reference
1.cppreference