template <class ToDuration, class Rep, class Period>
constexpr ToDuration duration_cast(const duration<Rep,Period>& d);
(since C++11)

Converts a std::chrono::duration to a duration of different type ToDuration.

No implicit conversions are used. Multiplications and divisions are avoided where possible, if it is known at compile time that one or more parameters are 1. Computations are done in the widest type available and converted to the result type only when finished.


[edit] Parameters

d - duration to convert

[edit] Return value

d converted to a duration of type ToDuration.

[edit] Notes

The function does not participate in the overload resolution unless ToDuration is an instance of std::chrono::duration.

[edit] Example

This example measures the execution time of a function

#include <iostream>
#include <chrono>
#include <thread>
void f()
int main()
    auto t1 = std::chrono::high_resolution_clock::now();
    auto t2 = std::chrono::high_resolution_clock::now();
    std::cout << "f() took "
              << std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count()
              << " milliseconds\n";


f() took 1000 milliseconds