Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::chrono::time_point: a point on a specific clock timeline

std::chrono::time_point<Clock, Duration> stores an offset from a clock epoch and represents a point, not merely a length of time.

Version: C++23

Syntax

template<class Clock, class Duration = typename Clock::duration>
class time_point;

constexpr Duration time_since_epoch() const;

template<class Duration>
using sys_time = time_point<system_clock, Duration>;
using sys_days = sys_time<days>;

std::chrono::time_point<Clock, Duration> from <chrono> associates a duration with the epoch of Clock. Default construction denotes the epoch, and time_since_epoch() returns the stored offset. Adding or subtracting a duration moves the point; subtracting two points of the same clock yields a duration.

The std::chrono::sys_time alias template, used as std::chrono::sys_time<Duration>, denotes std::chrono::time_point<std::chrono::system_clock, Duration>, while std::chrono::sys_days is its day-resolution specialization. Both denote system-clock time points; std::chrono::sys_days is convenient for calendar-day boundaries but does not attach a time zone by itself.

The clock type is part of the type, so points from different clocks cannot be subtracted or compared without an explicit valid mapping. A numeric offset alone does not identify a date unless the clock epoch semantics are known. time_point_cast, floor, ceil, and round change resolution, not clocks.

The converting constructor participates in overload resolution only when is_convertible_v<Duration2, duration> is true. That constraint does not guarantee that a particular value avoids overflow or loss of Rep precision. min() and max() also require care. A steady_clock point is not a persistent calendar timestamp.

Examples

#include <chrono>
#include <iostream>

int main() {
    using Point = std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>;
    Point point{std::chrono::seconds{3}};
    point += std::chrono::seconds{4};
    std::cout << point.time_since_epoch().count() << '\n';
}

Result

7

std::chrono::time_point → 7

Sources