Show Menu
Cheatography

c++ (chrono) Cheat Sheet (DRAFT) by

A cheatsheet on the chrono library

This is a draft cheat sheet. It is a work in progress and is not finished yet.

std::c­hro­no:­:st­ead­y_c­loc­k::­now()

auto now = std::chrono::steady_clock::now(); // e.g. t=1'052'348'281 ns
Returns the current time as a time_point from a monoto­nically increasing clock — it never goes backwards, even if the system clock is adjusted. Used to measure elapsed time and set deadlines.

std::c­hro­no:­:ti­me_­point

std::chrono::time_point<std::chrono::steady_clock> tp = std::chrono::steady_clock::now();
Represents a specific point in time relative to a clock's epoch. You rarely construct one directly — you typically get one back from now() and subtract two of them to get a duration.

std::c­hro­no:­:se­conds

std::chrono::seconds timeout = std::chrono::seconds(30); // timeout.count() == 30

// Extracts the raw numeric value from a duration. Necessary because chrono deliberately hides the number to prevent accidental unit mismatches — you have to opt in to get it. 
.count()
A duration type repres­enting a number of seconds. It is a convenient alias for std::c­hro­no:­:du­rat­ion­<long long>. Call .count() on it to get the raw integer value out.