In C++ std::chrono, which arithmetic is allowed between time_points and durations, and what types do literals like 250ms and 3d produce?
answer
- a point plus a span is a point
- point minus point is a span
- no point plus point
- one clock per expression
- 3d is a day, not days
basics
~20 stime_point plus duration gives a time_point, and the difference of two time_points of the same clock gives a duration; adding two time_points, or mixing clocks, does not compile. Literals like 250ms make durations, while C++20's 3d and 2026y make calendar day and year values.
solid answer
~40 s`std::chrono` models time like an affine space. `time_point + duration` and `time_point - duration` give a `time_point` of the same clock; `time_point - time_point` gives a `duration`; `time_point + time_point` has no operator, and two time points of different clocks cannot be subtracted, because their epochs are unrelated. `tp.time_since_epoch()` exposes the duration since the clock's epoch. The literals in `std::chrono_literals` (also visible after `using namespace std::chrono`) are `h`, `min`, `s`, `ms`, `us` and `ns`: an integer literal such as `250ms` is a `milliseconds`, and a floating literal such as `1.5h` is a duration with a floating-point count. C++20 added `d` and `y`, but `3d` is a `std::chrono::day`, the day of a month, and `2026y` a `std::chrono::year`; the three-day duration is `days{3}`.
code
cpp · 23 lines#include <chrono>
#include <iostream>
#include <type_traits>
int main() {
using namespace std::chrono; // also brings in the chrono literals
const steady_clock::time_point start = steady_clock::now();
const steady_clock::time_point deadline = start + 250ms; // time_point + duration
const auto budget = deadline - start; // time_point - time_point
static_assert(std::is_same_v<decltype(budget), const steady_clock::duration>);
std::cout << duration_cast<milliseconds>(budget) << '\n'; // 250ms (C++20 operator<<)
auto timeout = 1min + 30s; // minutes + seconds -> seconds
std::cout << timeout << ' ' << 1.5h << '\n'; // 90s 1.5h
static_assert(std::is_same_v<decltype(3d), day>); // C++20: a calendar day
static_assert(std::is_same_v<decltype(days{3}), days>); // the three-day duration
const sys_days epoch = 1970y / January / 1;
std::cout << epoch.time_since_epoch().count() << '\n'; // 0: Unix time since C++20
// steady_clock::now() + steady_clock::now(); error: no time_point + time_point
// steady_clock::now() - system_clock::now(); error: different clocks
}go deeper
Recall the point-and-span rules and the literals h, min, s, ms, us and ns.
Explain why time points of different clocks do not mix, how common types apply in arithmetic, and why 3d is a day rather than days.
Show how a deadline-and-timeout design keeps units and clocks straight across an API, and where explicit conversion belongs.
Decide how time values cross module and process boundaries, and which clock and representation each interface commits to.
## Points and spans `std::chrono` separates two ideas that plain integers blur: - a **`duration`** is a span of time, such as 250 milliseconds; - a **`time_point<Clock, Duration>`** is a moment, stored as a duration since the clock's **epoch**. The operators follow what makes physical sense, which is why the model is often called an affine space: | Expression | Result | Notes | |---|---|---| | `time_point + duration` | `time_point` | a deadline: `start + 250ms` | | `time_point - duration` | `time_point` | a moment in the past | | `time_point - time_point` | `duration` | elapsed time, same clock only | | `duration ± duration` | `duration` | common type of both units | | `duration * number`, `duration / number` | `duration` | scaling | | `duration / duration` | number | how many times one fits | | `time_point + time_point` | does not compile | a moment plus a moment means nothing | `tp.time_since_epoch()` returns the underlying duration, and comparisons (`<`, `==` and, since C++20, `<=>`) work between time points of the same clock. ## Each clock has its own time_point type The first template argument of `time_point` is the **clock**. `steady_clock::time_point` and `system_clock::time_point` are distinct types, and no `operator-` exists between them. Apple clang 21 reports `steady_clock::now() - system_clock::now()` as "invalid operands to binary expression". This is deliberate: `steady_clock`'s epoch is unspecified, while `system_clock`'s is 1970-01-01 UTC. Subtracting them would produce a number that means nothing. The duration types adjust automatically: `steady_clock::now() + 250ms` keeps the clock's finer tick, and `1min + 30s` is `seconds(90)` because the result takes the common type of the operands. ## The chrono literals C++14 put duration suffixes in the inline namespace `std::literals::chrono_literals`, and `namespace std::chrono` contains `using namespace literals::chrono_literals;`, so either `using namespace std::chrono_literals;` or `using namespace std::chrono;` brings them in. - `h`, `min`, `s`, `ms`, `us`, `ns` on an **integer** literal give `hours`, `minutes`, `seconds`, `milliseconds`, `microseconds`, `nanoseconds`; - the same suffixes on a **floating** literal, such as `1.5h` or `0.25s`, give a duration with an unspecified floating-point `rep` and the matching period; C++20 added two **calendar** literals that look like durations but are not: - `3d` is a `std::chrono::day`, the day-of-month field of a date, valid from 1 to 31; - `2026y` is a `std::chrono::year`. They exist so that `2026y / March / 29` builds a `year_month_day`. The duration of three days is `std::chrono::days{3}`, and `std::chrono::days`, `weeks`, `months` and `years` have no literal suffix. Confusing `3d` with `days{3}` is a classic C++20 slip: `start + 3d` fails to compile, which is the good outcome. ## Printing Since C++20, durations and time points can be written to an `std::ostream` and formatted with `std::format`; a duration prints its count followed by a unit suffix, so `90s` prints as `90s` and `1.5h` as `1.5h`. Before C++20 you printed `d.count()` and supplied the unit yourself, which is exactly where unit bugs came back in. ## Points to make in an interview 1. Store **deadlines** as `time_point`s and **timeouts** as `duration`s; never as raw integers. 2. Keep every expression on **one clock**; convert at the boundary on purpose. 3. Prefer `days{n}` for spans of days and reserve `d` for building dates. ## Changing the precision of a time point A `time_point` carries its duration type, so `system_clock::now()` (microsecond ticks in libc++ on macOS, nanoseconds in libstdc++) and a `sys_seconds` value are different types. Converting to a coarser one follows the duration rules: - the implicit conversion is allowed only when it is lossless, so `sys_seconds s = system_clock::now();` does not compile; - `std::chrono::time_point_cast<seconds>(tp)` truncates toward the epoch, the time-point counterpart of `duration_cast`; - `floor<seconds>(tp)`, `ceil<seconds>(tp)` and `round<seconds>(tp)` (C++17) choose the direction, and `floor` is the right tool for bucketing timestamps into minutes or days. A time point with a floating-point count, such as the result of `steady_clock::now() + 1.5s`, also needs one of these casts before it can be stored in the clock's own `time_point` type.
- What is the type of steady_clock::now() + 1.5s?A `time_point<steady_clock, D>` where `D` is the common type of the clock's duration and a duration with a floating-point count, so the result's count is floating-point. That is legal, but it is a different type from `steady_clock::time_point`, so storing it in a `steady_clock::time_point` variable needs a cast such as `time_point_cast` or `floor`.
- How do you get the number of whole days between two sys_days dates?Subtract them: `sys_days{2026y/March/1} - sys_days{2026y/January/31}` is a `days` duration, and `.count()` gives 29. Because `sys_days` is a `time_point` of `system_clock` with a period of one day, the ordinary time-point arithmetic does the calendar work, including month lengths and leap years.
- you can add two time points to get a later one
- steady_clock and system_clock time points can be subtracted directly
- 3d is a duration of three days
- time_point - time_point returns an integer tick count
- the chrono literals need a separate header besides <chrono>
Assumes C++14 duration literals; the d and y calendar literals, the Unix-time epoch of system_clock and stream output for chrono types are C++20.