home | help
std::chrono::duration_cast(3)  C++ Standard Libary std::chrono::duration_cast(3)

NAME
     std::chrono::duration_cast - std::chrono::duration_cast

Synopsis
	Defined in header <chrono>
	template< class ToDuration, class Rep, class Period >
	constexpr	     ToDuration 	  duration_cast(	   const
     (since C++11)
	std::chrono::duration<Rep,Period>& d );

	Converts a std::chrono::duration to a duration of different type ToDura-
     tion.

	The function does not participate in overload resolution unless  ToDura-
     tion is a
	specialization of std::chrono::duration.

	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, as  if
     by
	static_cast, to the result type only when finished.

Parameters
	d - duration to convert

Return value
	d converted to a duration of type ToDuration.

Notes
	Casting between integer durations where the source period is exactly di-
     visible by
	the  target period (e.g. hours to minutes) or between floating-point du-
     rations can be
	performed with ordinary casts or  implicitly  via  std::chrono::duration
     constructors,
	no duration_cast is needed.

	Casting from a floating-point duration to an integer duration is subject
     to
	undefined  behavior  when  the floating-point value is NaN, infinity, or
     too large to
	be representable by the target's integer type. Otherwise, casting to  an
     integer
	duration  is subject to truncation as with any static_cast to an integer
     type.

Example
	This example measures the execution time of a function.

     // Run this code

      #include <chrono>
      #include <iostream>
      #include <ratio>
      #include <thread>

      void f()
      {
	  std::this_thread::sleep_for(std::chrono::seconds(1));
      }

      int main()
      {
	  const auto t1 = std::chrono::high_resolution_clock::now();
	  f();
	  const auto t2 = std::chrono::high_resolution_clock::now();

	  // floating-point duration: no duration_cast needed
	  const std::chrono::duration<double, std::milli> fp_ms = t2 - t1;

	  // integral duration: requires duration_cast
	  const auto int_ms =  std::chrono::duration_cast<std::chrono::millisec-
     onds>(t2 - t1);

	  // converting integral duration to integral duration of
	  // shorter divisible time unit: no duration_cast needed
	  const std::chrono::duration<long, std::micro> int_usec = int_ms;

	  std::cout << "f() took " << fp_ms << ", or "
		    << int_ms << " (whole milliseconds), or "
		    << int_usec << " (whole microseconds)\n";
      }

Possible output:
      f()  took  1000.14ms,  or 1000ms (whole milliseconds), or 1000000us (whole
     microseconds)

See also
	duration		     a time interval
	(C++11) 		     (class template)
	time_point_cast 	     converts a time point to another time point
     on the same
	(C++11) 		     clock, with a different duration
				     (function template)
	floor(std::chrono::duration) converts a duration  to  another,	rounding
     down
	(C++17) 		     (function template)
	ceil(std::chrono::duration)  converts a duration to another, rounding up
	(C++17) 		     (function template)
	round(std::chrono::duration) converts a duration to another, rounding to
     nearest,
	(C++17) 		     ties to even
				     (function template)

http://cppreference.com 	   2024.06.10	   std::chrono::duration_cast(3)

home | help