chrono

Introduction

The chrono library is a flexible collection of types that track time with varying degrees of precision. It is modeled after the standard library’s std::chrono.

It defines three main types (durations, clocks, and time points) as well as utility functions and common typedefs. The chrono is most suited for measuring time intervals and independent of the system clock. If the destination type has less precision, the value is truncated.

Usage

In addition to the general safety assumptions at the module level which are taken care at the system, the usage guidelines apply for estd::chrono.

Usage guidance

Implement function getSystemTimeNs() for full chrono support on the respective platform.

Example

The following example shows that “42 seconds” could be represented by a duration consisting of 42 ticks of a 1-second time unit, or of 42’000 ticks of a 1-millisecond time unit. Kindly refer typedefs for the supported clock units.

// using milli = duration<int32_t>;
estd::chrono::duration<int32_t, estd::milli> src(1001);
{
    // using duration_cast to pass the value
    estd::chrono::duration<int32_t, estd::micro> cut
        = estd::chrono::duration_cast<estd::chrono::duration<int32_t, estd::micro>>(src);

    // using count to print duration
    std::cout << "Duration: " << cut.count() << "ms\n";

    // using extreme duration values from duration
    estd::chrono::duration<int8_t, estd::milli>::min().count();
    estd::chrono::duration<int8_t, estd::milli>::max().count();
}

// using seconds = duration<int64_t>;
auto const ts1 = ::estd::chrono::seconds(42);

// using milliseconds = duration<int64_t, milli>;
auto const ts2 = ::estd::chrono::milliseconds(42000);

// duration_cast using equality operator

estd_assert(::estd::chrono::duration_cast<::estd::chrono::seconds>(ts2) == ts1);

Usage example of time point is as follows:

// time_point created
estd::chrono::time_point<chrono_example::Clock, estd::chrono::milliseconds> cut;

// the time after the clock start is 0
std::cout << "time_point: " << cut.time_since_epoch().count() << "ms\n";

// value of 715 micro seconds is passed to time_point
estd::chrono::time_point<chrono_example::Clock, estd::chrono::microseconds> cut1(
    estd::chrono::microseconds(715));

// the time after the clock start is 715 micro seconds
std::cout << "time_point: " << cut1.time_since_epoch().count() << "ms\n";

// using extreme duration values from time_point
estd::chrono::time_point<chrono_example::Clock, estd::chrono::duration<int8_t, estd::milli>>::
    min()
        .time_since_epoch()
        .count();

estd::chrono::time_point<chrono_example::Clock, estd::chrono::duration<int8_t, estd::milli>>::
    max()
        .time_since_epoch()
        .count();