Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::chrono::system_clock: the wall-clock clock

std::chrono::system_clock represents system-wide real time and can map its time points to time_t.

Version: C++23

Syntax

class system_clock {
public:
    using duration = /* implementation-defined */;
    using time_point = std::chrono::time_point<system_clock, duration>;
    static constexpr bool is_steady = /* implementation-defined */;
    static time_point now() noexcept;
    static std::time_t to_time_t(const time_point&) noexcept;
    static time_point from_time_t(std::time_t) noexcept;
};

std::chrono::system_clock from <chrono> represents system-wide wall time. now() returns the current time_point, while to_time_t and from_time_t bridge to C time_t. sys_time types measure from 1970-01-01 00:00:00 UTC, excluding leap seconds.

The clock can be adjusted by synchronization, an administrator, or system settings; is_steady need not be true and later readings can move backward. It is therefore appropriate for calendar timestamps but not reliable elapsed-time or timeout measurement.

Conversion to or from time_t uses the coarser precision, with rounding or truncation chosen by the implementation. Precision, range, and exact representation are also platform-specific. Use steady_clock for elapsed time.

Examples

#include <chrono>
#include <iostream>

int main() {
    const std::chrono::system_clock::time_point point{std::chrono::seconds{7}};
    std::cout << std::chrono::duration_cast<std::chrono::seconds>(point.time_since_epoch()).count() << '\n';
}

Result

7

std::chrono::system_clock → 7

Sources