- Description: A note on
<chrono> — durations, time points, clocks, calendar (C++20), time zones, formatting, and timing patterns
- My Notion Note ID: K2A-B1-22
- Created: 2020-04-15
- Updated: 2026-02-28
- License: Reuse is very welcome. Please credit Yu Zhang and link back to the original on yuzhang.io
Table of Contents
1. Durations
std::chrono::duration<Rep, Period> — length of time as Rep count of Period-sized ticks.
- Standard library provides typedefs for common durations.
#include <chrono>
using namespace std::chrono;
nanoseconds ns{500};
microseconds us{250};
milliseconds ms{100};
seconds s{5};
minutes m{2};
hours h{1};
days d{7};
weeks w{2};
months mo{6};
years y{1};
milliseconds total = h + s;
seconds approx = duration_cast<seconds>(total);
auto x = 5s + 200ms;
auto y = 3min / 30;
auto z = 1h - 30min;
using namespace std::chrono_literals;
auto t = 250ms + 5s;
2. Time Points and Clocks
- Clock = source of time → produces time points (instants).
| Clock |
Properties |
Use for |
std::chrono::system_clock |
Wall clock; can jump, may go backward (NTP, DST) |
Storing dates/times, displaying time |
std::chrono::steady_clock |
Monotonic; never goes backward |
Measuring elapsed time |
std::chrono::high_resolution_clock |
Highest available resolution; alias for one of the above |
Avoid — use the explicit ones |
std::chrono::utc_clock (C++20) |
UTC; leap seconds explicit |
Storing UTC timestamps |
std::chrono::file_clock |
Filesystem timestamps |
Pair with std::filesystem::last_write_time |
auto now = std::chrono::system_clock::now();
auto epoch = now.time_since_epoch();
auto sec_since_epoch = duration_cast<seconds>(epoch).count();
auto utc_now = std::chrono::utc_clock::now();
auto sys_now = std::chrono::clock_cast<std::chrono::system_clock>(utc_now);
- Always use
steady_clock for durations — system_clock may jump and produce negative durations:
auto t0 = std::chrono::steady_clock::now();
do_work();
auto t1 = std::chrono::steady_clock::now();
auto elapsed = duration_cast<milliseconds>(t1 - t0);
3. Calendar Types (C++20)
- C++20 added a calendar system — named types + operator chaining.
using namespace std::chrono;
year_month_day today = 2026y/February/28;
year_month_day_last eom = 2026y/February/last;
auto next_week = sys_days{today} + days{7};
auto next_year = today + years{1};
weekday wd{sys_days{today}};
std::cout << wd;
bool ok = today.ok();
sys_days = duration of days from system epoch. Useful for date arithmetic.
4. Time Zones (C++20)
using namespace std::chrono;
const time_zone* la = locate_zone("America/Los_Angeles");
zoned_time<seconds> now_la{la, system_clock::now()};
std::cout << now_la;
zoned_time pairs clock time with zone, handles conversions automatically.
- Requires IANA tz database — ships on most platforms; check on Windows.
#include <format>
using namespace std::chrono;
auto now = system_clock::now();
std::string s = std::format("{:%Y-%m-%d %H:%M:%S}", now);
std::string iso = std::format("{:%FT%TZ}", floor<seconds>(now));
- Format specifiers from
strftime. Key codes:
| Code |
Meaning |
%Y |
4-digit year |
%m, %d |
2-digit month, day |
%H, %M, %S |
2-digit hour (24h), min, sec |
%F |
%Y-%m-%d |
%T |
%H:%M:%S |
%A, %a |
weekday name (full / abbreviated) |
%Z, %z |
time zone name / offset |
6. Sleeping and Waiting
#include <thread>
using namespace std::chrono_literals;
std::this_thread::sleep_for(500ms);
std::this_thread::sleep_until(system_clock::now() + 2s);
- Pair with
condition_variable::wait_for / wait_until for timeouts:
std::unique_lock<std::mutex> lock(m);
if (cv.wait_for(lock, 500ms, [&] { return ready; })) {
} else {
}
7. Timing Code
- Helper for measuring elapsed time:
template <typename Fn>
auto time_it(Fn&& fn) {
auto t0 = std::chrono::steady_clock::now();
fn();
auto t1 = std::chrono::steady_clock::now();
return std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0);
}
auto elapsed = time_it([] { do_work(); });
std::cout << elapsed.count() / 1e6 << " ms\n";
- For benchmarks → Google Benchmark or nanobench (warm-up, repetition, variance).
chrono alone is fine for ad-hoc timing or instrumentation.