Durations and Clocks
Measure time intervals.
Durations and Clocks is a free C++ Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
Why <chrono>?
The <chrono> library gives C++ a type-safe way to work with time. Instead of raw integers, you use strongly typed durations and time points so the compiler catches unit mistakes.
- No more mixing up seconds and milliseconds.
- Units are part of the type system.
#include <iostream>
#include <chrono>
int main() {
using namespace std::chrono;
seconds s{5};
std::cout << "Duration: " << s.count() << " seconds\n";
return 0;
}What Is a Duration?
A std::chrono::duration represents a span of time. It stores a count of ticks plus a ratio describing the tick period. Predefined aliases cover common units.
hours,minutes,secondsmilliseconds,microseconds,nanoseconds
#include <iostream>
#include <chrono>
int main() {
using namespace std::chrono;
milliseconds ms{1500};
std::cout << ms.count() << " ms\n";
return 0;
}Duration Literals
The std::chrono_literals namespace lets you write durations with suffixes like 5s or 200ms, making code read like plain English.
#include <iostream>
#include <chrono>
int main() {
using namespace std::chrono_literals;
auto wait = 3s;
auto blink = 250ms;
std::cout << wait.count() << "s, " << blink.count() << "ms\n";
return 0;
}Converting Durations
duration_cast converts between units. Casting from a finer unit to a coarser one truncates, so 1500 ms becomes 1 s.
#include <iostream>
#include <chrono>
int main() {
using namespace std::chrono;
milliseconds ms{1500};
seconds s = duration_cast<seconds>(ms);
std::cout << s.count() << " whole seconds\n";
return 0;
}Arithmetic on Durations
Durations support +, -, *, and /. Mixing units automatically promotes to the finer common unit.
#include <iostream>
#include <chrono>
int main() {
using namespace std::chrono;
auto total = minutes{2} + seconds{30};
std::cout << total.count() << " seconds total\n";
return 0;
}Floating-Point Durations
You can use a floating-point representation to keep fractional values when converting, avoiding the truncation of integer durations.
#include <iostream>
#include <chrono>
int main() {
using namespace std::chrono;
milliseconds ms{1500};
duration<double> secs = ms;
std::cout << secs.count() << " seconds\n";
return 0;
}What Is a Clock?
A clock provides the current time. The standard library offers three:
system_clock— wall-clock timesteady_clock— never goes backwards, ideal for timinghigh_resolution_clock— finest available tick
#include <iostream>
#include <chrono>
int main() {
using namespace std::chrono;
auto now = steady_clock::now();
std::cout << "Got a time point: " << (now.time_since_epoch().count() > 0) << '\n';
return 0;
}system_clock
system_clock tracks real-world calendar time and can be converted to a time_t for display. It may jump if the system clock is adjusted.
#include <iostream>
#include <chrono>
int main() {
using namespace std::chrono;
auto tp = system_clock::now();
auto secs = duration_cast<seconds>(tp.time_since_epoch());
std::cout << "Seconds since epoch is positive: " << (secs.count() > 0) << '\n';
return 0;
}steady_clock
steady_clock is monotonic: it only moves forward at a constant rate. Use it whenever you measure elapsed time, never system_clock.
#include <iostream>
#include <chrono>
int main() {
using namespace std::chrono;
auto a = steady_clock::now();
auto b = steady_clock::now();
std::cout << "Monotonic: " << std::boolalpha << (b >= a) << '\n';
return 0;
}Clock Resolution
Each clock exposes its period as a compile-time ratio. You can inspect whether a clock is steady at compile time too.
#include <iostream>
#include <chrono>
int main() {
using namespace std::chrono;
std::cout << "steady_clock is_steady: " << std::boolalpha << steady_clock::is_steady << '\n';
std::cout << "system_clock is_steady: " << system_clock::is_steady << '\n';
return 0;
}Putting It Together
Combine clocks and durations: read two time points and compute the difference as a duration in your chosen unit.
#include <iostream>
#include <chrono>
int main() {
using namespace std::chrono;
auto start = steady_clock::now();
long sum = 0;
for (int i = 0; i < 1000000; ++i) sum += i;
auto end = steady_clock::now();
auto us = duration_cast<microseconds>(end - start);
std::cout << "Loop took at least 0 us: " << (us.count() >= 0) << '\n';
return 0;
}Quick Check
Test your understanding of which clock to use for timing.
Recap
You learned that <chrono> provides:
- durations — type-safe spans of time with literals like
5s duration_castfor converting units (truncates integers)- clocks —
system_clock,steady_clock,high_resolution_clock - use
steady_clockfor measuring intervals
Next, you will represent specific moments in time with time points.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Durations and Clocks” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Durations and Clocks”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
What will I learn in “Durations and Clocks”?
Measure time intervals. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “Durations and Clocks” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Durations and Clocks
- Time Points
- Measuring Code Performance
- Calendar and Time Zones