duration Class

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at duration Class.

Describes a type that holds a time interval, which is an elapsed time between two time points.

template <class Rep,  
    class Period = ratio<1>>  
class duration;  
template <class Rep,  
    class Period>  
class duration;  
template <class Rep,  
    class Period1,  
    class Period2>  
class duration  
 <duration<Rep, Period1>, Period2>;  

The template argument Rep describes the type that is used to hold the number of clock ticks in the interval. The template argument Period is an instantiation of ratio that describes the size of the interval that each tick represents.

Public Typedefs

NameDescription
duration::period TypedefRepresents a synonym for the template parameter Period.
duration::rep TypedefRepresents a synonym for the template parameter Rep.

Public Constructors

NameDescription
duration::duration ConstructorConstructs a duration object.

Public Methods

NameDescription
duration::count MethodReturns the number of clock ticks in the time interval.
duration::max MethodStatic. Returns the maximum allowable value of template parameter Ref.
duration::min MethodStatic. Returns the lowest allowable value of template parameter Ref.
duration::zero MethodStatic. In effect, returns Rep(0).

Public Operators

NameDescription
duration::operator-Returns a copy of the duration object together with a negated tick count.
duration::operator--Decrements the stored tick count.
duration::operator=Reduces the stored tick count modulo a specified value.
duration::operator*=Multiplies the stored tick count by a specified value.
duration::operator/=Divides the stored tick count by the tick count of a specified duration object.
duration::operator+Returns *this.
duration::operator++Increments the stored tick count.
duration::operator+=Adds the tick count of a specified duration object to the stored tick count.
duration::operator-=Subtracts the tick count of a specified duration object from the stored tick count.

Header: chrono

Namespace: std::chrono

Retrieves the number of clock ticks in the time interval.

constexpr Rep count() const;

Return Value

The number of clock ticks in the time interval.

Constructs a duration object.

constexpr duration() = default;  
 
template <class Rep2>  
constexpr explicit duration(const Rep2& R);

 
template <class Rep2, class Period2>  
constexpr duration(const duration<Rep2, Period2>& Dur);

Parameters

Rep2
An arithmetic type to represent the number of ticks.

Period2
A std::ratio template specialization to represent the tick period in units of seconds.

R
The number of ticks of default period.

Dur
The number of ticks of period specified by Period2.

Remarks

The default constructor constructs an object that is uninitialized. Value initialization by using empty braces initializes an object that represents a time interval of zero clock ticks.

The second, one template argument constructor constructs an object that represents a time interval of R clock ticks using a default period of std::ratio<1>. To avoid round-off of tick counts, it is an error to construct a duration object from a representation type Rep2 that can be treated as a floating-point type when duration::rep cannot be treated as a floating-point type.

The third, two template argument constructor constructs an object that represents a time interval whose length is the time interval that is specified by Dur. To avoid truncation of tick counts, it is an error to construct a duration object from another duration object whose type is incommensurable with the target type.

A duration type D1 is incommensurable with another duration type D2 if D2 cannot be treated as a floating-point type and ratio_divide<D1::period, D2::period>::type::den is not 1.

Unless Rep2 is implicitly convertible to rep and either treat_as_floating_point<rep>holds true or treat_as_floating_point<Rep2>holds false, the second constructor does not participate in overload resolution. For more information, see <type_traits>.

Unless no overflow is induced in the conversion and treat_as_floating_point<rep>holds true, or both ratio_divide<Period2, period>::den equals 1 and treat_as_floating_point<Rep2>holds false, the third constructor does not participate in overload resolution. For more information, see <type_traits>.

Static method that returns the upper bound for values of template parameter type Ref.

static constexpr duration max();

Return Value

In effect, returns duration(duration_values<rep>::max()).

Static method that returns the lower bound for values of template parameter type Ref.

static constexpr duration min();

Return Value

In effect, returns duration(duration_values<rep>::min()).

Returns a copy of the duration object together with a negated tick count.

constexpr duration operator-() const;

Decrements the stored tick count.

duration& operator--();

duration operator--(int);

Return Value

The first method returns *this.

The second method returns a copy of *this that is made before the decrement.

Reduces the stored tick count modulo a specified value.

duration& operator%=(const rep& Div);

duration& operator%=(const duration& Div);

Parameters

Div
For the first method, Div represents a tick count. For the second method, Div is a duration object that contains a tick count.

Return Value

The duration object after the modulo operation is performed.

Multiplies the stored tick count by a specified value.

duration& operator*=(const rep& Mult);

Parameters

Mult
A value of the type that is specified by duration::rep.

Return Value

The duration object after the multiplication is performed.

Divides the stored tick count by a specified value.

duration& operator/=(const rep& Div);

Parameters

Div
A value of the type that is specified by duration::rep.

Return Value

The duration object after the division is performed.

Returns *this.

constexpr duration operator+() const;

Increments the stored tick count.

duration& operator++();

duration operator++(int);

Return Value

The first method returns *this.

The second method returns a copy of *this that is made before the increment.

Adds the tick count of a specified duration object to the stored tick count.

duration& operator+=(const duration& Dur);

Parameters

Dur
A duration object.

Return Value

The duration object after the addition is performed.

Subtracts the tick count of a specified duration object from the stored tick count.

duration& operator-=(const duration& Dur);

Parameters

Dur
A duration object.

Return Value

The duration object after the subtraction is performed.

Returns duration(duration_values<rep>::zero()).

static constexpr duration zero();

Reduces the stored tick count modulo Div or Div.count().

duration& operator%=(const rep& Div);duration& operator%=(const duration& Div);

Parameters

Div
The divisor, which is either a duration object or a value that represents tick counts.

Remarks

The first member function reduces the stored tick count modulo Div and returns *this. The second member function reduces the stored tick count modulo Div.count() and returns *this.

Header Files Reference
<chrono>
duration_values Structure

Show: