##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::chrono::duration

From cppreference.com
< cpp‎ | chrono

Defined in header `<chrono>` |  |
template< class Rep, class Period = std::ratio<1> > class duration; |  | (since C++11)

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.

### [edit] Member types

Member type | Definition
`rep` | `Rep`, an arithmetic type representing the number of ticks
`period` | `Period` (until C++17)`typename Period::type` (since C++17), a std::ratio representing the tick period (i.e. the number of seconds per tick)

### [edit] Member functions

(constructor) | constructs new duration (public member function) [edit]
operator= | assigns the contents (public member function) [edit]
count | returns the count of ticks (public member function) [edit]
zero [static] | returns the special duration value zero (public static member function) [edit]
min [static] | returns the special duration value min (public static member function) [edit]
max [static] | returns the special duration value max (public static member function) [edit]
operator+operator- | implements unary + and unary - (public member function) [edit]
operator++operator++(int)operator--operator--(int) | increments or decrements the tick count (public member function) [edit]
operator+=operator-=operator*=operator/=operator%= | implements compound assignment between two durations (public member function) [edit]

### [edit] Non-member functions

std::common_type<std::chrono::duration> | specializes the std::common_type trait (class template specialization) [edit]
operator+operator-operator*operator/operator% | implements arithmetic operations with durations as arguments (function template) [edit]
operator==operator!=operator<operator<=operator>operator>= | compares two durations (function template) [edit]
duration_cast | converts a duration to another, with a different tick interval (function template) [edit]
floor(std::chrono::duration) (C++17) | converts a duration to another, rounding down (function template) [edit]
ceil(std::chrono::duration) (C++17) | converts a duration to another, rounding up (function template) [edit]
round(std::chrono::duration) (C++17) | converts a duration to another, rounding to nearest, ties to even (function template) [edit]
abs(std::chrono::duration) (C++17) | obtains the absolute value of the duration (function template) [edit]

### [edit] Helper types

Type | Definition
std::chrono::nanoseconds | duration</*signed integer type of at least 64 bits*/, std::nano>
std::chrono::microseconds | duration</*signed integer type of at least 55 bits*/, std::micro>
std::chrono::milliseconds | duration</*signed integer type of at least 45 bits*/, std::milli>
std::chrono::seconds | duration</*signed integer type of at least 35 bits*/>
std::chrono::minutes | duration</*signed integer type of at least 29 bits*/, std::ratio<60>>
std::chrono::hours | duration</*signed integer type of at least 23 bits*/, std::ratio<3600>>

Note: each of the predefined duration types covers a range of at least ±292 years.

### [edit] Helper classes

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

### [edit] Literals

Defined in inline namespace `std::literals::chrono_literals`
operator""h (C++14) | A std::chrono::duration literal representing hours (function) [edit]
operator""min (C++14) | A std::chrono::duration literal representing minutes (function) [edit]
operator""s (C++14) | A std::chrono::duration literal representing seconds (function) [edit]
operator""ms (C++14) | A std::chrono::duration literal representing milliseconds (function) [edit]
operator""us (C++14) | A std::chrono::duration literal representing microseconds (function) [edit]
operator""ns (C++14) | A std::chrono::duration literal representing nanoseconds (function) [edit]

### [edit] Example

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

Run this code

    #include <iostream>
    #include <chrono>

    int main()
    {
        using shakes = std::chrono::duration<int, std::ratio<1, 100000000>>;
        using jiffies = std::chrono::duration<int, std::centi>;
        using microfortnights = std::chrono::duration<float, std::ratio<12096,10000>>;
        using nanocenturies = std::chrono::duration<float, std::ratio<3155,1000>>;

        std::chrono::seconds sec(1);

        std::cout << "1 second is:\n";

        // integer scale conversion with no precision loss: no cast
        std::cout << std::chrono::microseconds(sec).count() << " microseconds\n"
                  << shakes(sec).count() << " shakes\n"
                  << jiffies(sec).count() << " jiffies\n";

        // integer scale conversion with precision loss: requires a cast
        std::cout << std::chrono::duration_cast<std::chrono::minutes>(sec).count()
                  << " minutes\n";

        // floating-point scale conversion: no cast
        std::cout << microfortnights(sec).count() << " microfortnights\n"
                  << nanocenturies(sec).count() << " nanocenturies\n";
    }

Output:

    1 second is:
    1000000 microseconds
    100000000 shakes
    100 jiffies
    0 minutes
    0.82672 microfortnights
    0.316957 nanocenturies

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/chrono/duration&oldid=91977"