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";
}