C++ Basics: Convert Steady-Clock Durations Explicitly
Subtract time points from the same clock and choose integer or fractional duration units deliberately.
Prepare a scratch directory
Tested on Linux x86_64 with GCC 14.2.0. Install a C++20-capable compiler and standard-library development headers, with g++ on PATH. You also need Bash, a text editor, and the mktemp, rm, and rmdir utilities. No third-party library is required.
Keep the same terminal open. Stop if setup fails, before saving the source:
start_directory="$PWD"
work_directory="$(mktemp -d)" || exit 1
cd -- "$work_directory" || exit 1Run the example
Save this as steady-clock-duration-units-demo.cpp:
#include <chrono>
#include <iomanip>
#include <iostream>
int main() {
using Clock = std::chrono::steady_clock;
using MillisecondPoint = std::chrono::time_point<Clock, std::chrono::milliseconds>;
const MillisecondPoint started{std::chrono::milliseconds{1000}};
const MillisecondPoint finished{std::chrono::milliseconds{2750}};
const auto elapsed = finished - started;
const auto whole_seconds = std::chrono::duration_cast<std::chrono::seconds>(elapsed);
const std::chrono::duration<double> fractional_seconds = elapsed;
std::cout << "Steady clock: " << std::boolalpha << Clock::is_steady << '\n';
std::cout << "Milliseconds: " << elapsed.count() << '\n';
std::cout << "Whole seconds: " << whole_seconds.count() << '\n';
std::cout << std::fixed << std::setprecision(2)
<< "Fractional seconds: " << fractional_seconds.count() << '\n';
}Compile and run it. The second command runs only after compilation succeeds:
g++ -std=c++20 -Wall -Wextra -Werror -pedantic steady-clock-duration-units-demo.cpp -o steady-clock-duration-units-demo &&
./steady-clock-duration-units-demoObserved output:
Steady clock: true
Milliseconds: 1750
Whole seconds: 1
Fractional seconds: 1.75Read the result
The synthetic time points are 1,750 milliseconds apart. Casting to an integer seconds duration discards the fractional part; using a floating representation retains 1.75 seconds. These fixed inputs make the conversion example reproducible.
Apply it carefully
This is a unit-conversion example, not a benchmark or a measured execution time. For real elapsed time, capture steady_clock::now() before and after the operation and subtract those same-clock points. Its epoch is not a calendar timestamp. duration_cast to an integer representation truncates toward zero; select floor, ceil or round when that is the desired policy.
The C++ working draft describes the relevant library rules. This example uses only C++20 facilities, even though the linked draft also documents newer additions.
Clean up
Remove only the source and executable created here, then remove the empty practice directory:
rm -- steady-clock-duration-units-demo.cpp steady-clock-duration-units-demo
cd -- "$start_directory" || exit 1
rmdir -- "$work_directory"
unset start_directory work_directoryIf the directory is not empty, inspect its remaining contents before removing anything else.
Acknowledgements
This post was created with assistance from AI.