Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/chrono/duration"

From cppreference.com
< cpp‎ | chrono
m (+inline namespace)
m (hidden comment attributing the "only data.." line just in case)
Line 15: Line 15:
 
It consists of a count of ticks of type {{tt|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.
 
It consists of a count of ticks of type {{tt|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 {{tt|duration}} is a tick count of type {{tt|Rep}}.  If {{tt|Rep}} is floating point, then the {{tt|duration}} can represent fractions of ticks.  {{tt|Period}} is included as part of the duration's type, and is only used when converting between different durations.
+
The only data stored in a {{tt|duration}} is a tick count of type {{tt|Rep}}.<!-- per N2661, not actually in the standard wording --> If {{tt|Rep}} is floating point, then the {{tt|duration}} can represent fractions of ticks.  {{tt|Period}} is included as part of the duration's type, and is only used when converting between different durations.
  
 
===Member types===
 
===Member types===

Revision as of 11:35, 7 February 2014

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
 
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.

Contents

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

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

Non-member functions

specializes the std::common_type trait
(class template specialization) [edit]
implements arithmetic operations with durations as arguments
(function template) [edit]
(C++11)(C++11)(removed in C++20)(C++11)(C++11)(C++11)(C++11)(C++20)
compares two durations
(function template) [edit]
converts a duration to another, with a different tick interval
(function template) [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>>

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)

Literals

Defined in inline namespace std::literals::chrono_literals
a std::chrono::duration literal representing hours
(function) [edit]
a std::chrono::duration literal representing minutes
(function) [edit]
a std::chrono::duration literal representing seconds
(function) [edit]
a std::chrono::duration literal representing milliseconds
(function) [edit]
a std::chrono::duration literal representing microseconds
(function) [edit]
a std::chrono::duration literal representing nanoseconds
(function) [edit]


Example

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 << microfortnights(sec).count() << " microfortnights\n";
    std::cout << nanocenturies(sec).count() << " nanocenturies\n";
}

Output:

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