liveBook · Manning

Std::chrono::duration

As explained in Learn C++ by Example

std::chrono::milliseconds is a C++ chrono duration type whose period is one millisecond.

std::chrono::milliseconds is a C++ duration type representing intervals in one-millisecond units. It belongs to the std::chrono family alongside minutes, hours, days, and other durations. section 4.2.2

Conversions between durations can be implicit when moving from a coarser interval to a finer one. Conversions that may lose precision require duration_cast. section 4.2.2

Convert two hours to milliseconds

std::chrono::milliseconds ms = std::chrono::hours(2); This assignment converts a coarser two-hour duration into a finer-grained millisecond duration. section 4.2.2

auto two_hours_from_ms = duration_cast<hours>(ms);

Use duration_cast when converting milliseconds back to hours, because the conversion can discard fractional precision. section 4.2.2

How Learn C++ by Example covers it

How deep it goes
Explains the millisecond period and conversion direction, then stops short of defining the duration type internally.
Examples
Uses a two-hour assignment to milliseconds and a reverse conversion with duration_cast.
What it assumes
Assumes basic C++ duration usage while explaining duration conversion behavior through chrono examples.
What you can do after
Apply it via the C++ chrono library to represent milliseconds and select explicit casts when conversions may lose precision.
How it connects
Connects milliseconds with std::chrono::hours, std::chrono::minutes, days, duration_cast, and duration.

Also explained in

Compare how 2 books explain std::chrono::duration →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.