< cpp‎ | chrono
Revision as of 16:38, 2 November 2012 by P12bot (Talk | contribs)


Template:ddcl list begin <tr class="t-dsc-header">

Defined in header <chrono>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td >

    class Rep,
    class Period = std::ratio<1>

> class duration;

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> Template:ddcl list end

Class template std::chrono::duration represents a time interval.

It consists of a count of ticks of type Rep and a tick period, where the tick period is a compile-time rational constant representing the number of seconds from one tick to the next.

The only data stored in a duration is a tick count of type Rep. If Rep is floating point, then the duration can represent fractions of ticks. Period is included as part of the duration's type, and is only used when converting between different durations.


Member types

Member type Definition
rep Rep, an arithmetic type representing the number of ticks
period Period, a std::ratio representing the tick period (i.e. the number of seconds per tick)

Member functions

Template:cpp/chrono/duration/dcl list constructorTemplate:cpp/chrono/duration/dcl list operator=Template:cpp/chrono/duration/dcl list countTemplate:cpp/chrono/duration/dcl list zeroTemplate:cpp/chrono/duration/dcl list minTemplate:cpp/chrono/duration/dcl list maxTemplate:cpp/chrono/duration/dcl list operator arithTemplate:cpp/chrono/duration/dcl list operator arith2Template:cpp/chrono/duration/dcl list operator arith3

Non-member types

Type Definition
std::chrono::nanoseconds duration type with Period std::nano
std::chrono::microseconds duration type with Period std::micro
std::chrono::milliseconds duration type with Period std::milli
std::chrono::seconds duration type with Period std::ratio<1>
std::chrono::minutes duration type with Period std::ratio<60>
std::chrono::hours duration type with Period std::ratio<3600>

Non-member functions

Template:cpp/chrono/duration/dcl list common typeTemplate:cpp/chrono/duration/dcl list operator arith4Template:cpp/chrono/duration/dcl list operator cmpTemplate:cpp/chrono/duration/dcl list duration cast

Helper classes

indicates that a duration is convertible to duration with different tick period
(class template)
constructs zero, min, and max values of a tick count of given type
(class template)


This example shows how to define several custom duration types and convert between types:

#include <iostream>
#include <chrono>
int main()
    typedef std::chrono::duration<int, std::ratio<1, 100000000>> shakes;
    typedef std::chrono::duration<int, std::centi> jiffies;
    typedef std::chrono::duration<float, std::ratio<12096,10000>> microfortnights;
    typedef std::chrono::duration<float, std::ratio<3155,1000>> nanocenturies;
    std::chrono::seconds sec(1);
    std::cout << "1 second is:\n";
    std::cout << std::chrono::duration_cast<shakes>(sec).count()
              << " shakes\n";
    std::cout << std::chrono::duration_cast<jiffies>(sec).count()
              << " jiffies\n";
    std::cout << std::chrono::duration_cast<microfortnights>(sec).count()
              << " microfortnights\n";
    std::cout << std::chrono::duration_cast<nanocenturies>(sec).count()
              << " nanocenturies\n";


1 second is:
100000000 shakes
100 jiffies
0.82672 microfortnights
0.316957 nanocenturies