Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/chrono/duration"

From cppreference.com
< cpp‎ | chrono
(Helper classes: templatize)
(Improve example to use std rational arithmetic and types instead of "reinventing the wheel")
Line 100: Line 100:
 
#include <iostream>
 
#include <iostream>
 
#include <chrono>
 
#include <chrono>
 
constexpr auto year = 31'556'952LL; // seconds in average Gregorian year
 
  
 
int main()
 
int main()
Line 107: Line 105:
 
     using shakes = std::chrono::duration<int, std::ratio<1, 100'000'000>>;
 
     using shakes = std::chrono::duration<int, std::ratio<1, 100'000'000>>;
 
     using jiffies = std::chrono::duration<int, std::centi>;
 
     using jiffies = std::chrono::duration<int, std::centi>;
     using microfortnights = std::chrono::duration<float, std::ratio<14*24*60*60, 1'000'000>>;
+
     using microfortnights = std::chrono::duration<float,  
     using nanocenturies = std::chrono::duration<float, std::ratio<100*year, 1000'000'000>>;
+
          std::ratio_multiply<
 
+
                std::ratio_multiply<
 +
                    std::ratio<2>,
 +
                    std::chrono::weeks::period>,
 +
                std::micro>>;
 +
     using nanocenturies = std::chrono::duration<float,  
 +
          std::ratio_multiply<
 +
                std::ratio_multiply<
 +
                    std::hecto,
 +
                    std::chrono::years::period>,
 +
                std::nano>>;
 +
 
     std::chrono::seconds sec(1);
 
     std::chrono::seconds sec(1);
 
+
 
     std::cout << "1 second is:\n";
 
     std::cout << "1 second is:\n";
 
+
 
     // integer scale conversion with no precision loss: no cast
 
     // integer scale conversion with no precision loss: no cast
 
     std::cout << std::chrono::microseconds(sec).count() << " microseconds\n"
 
     std::cout << std::chrono::microseconds(sec).count() << " microseconds\n"
 
               << shakes(sec).count() << " shakes\n"
 
               << shakes(sec).count() << " shakes\n"
 
               << jiffies(sec).count() << " jiffies\n";
 
               << jiffies(sec).count() << " jiffies\n";
 
+
 
     // integer scale conversion with precision loss: requires a cast
 
     // integer scale conversion with precision loss: requires a cast
 
     std::cout << std::chrono::duration_cast<std::chrono::minutes>(sec).count()
 
     std::cout << std::chrono::duration_cast<std::chrono::minutes>(sec).count()
 
               << " minutes\n";
 
               << " minutes\n";
 
+
 
     // floating-point scale conversion: no cast
 
     // floating-point scale conversion: no cast
 
     std::cout << microfortnights(sec).count() << " microfortnights\n"
 
     std::cout << microfortnights(sec).count() << " microfortnights\n"

Revision as of 03:06, 5 October 2021

 
 
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 fraction representing the time in 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(until C++17)typename Period::type(since C++17), a std::ratio representing the tick period (i.e. the number of second's fractions 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

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]
converts a duration to another, rounding down
(function template) [edit]
converts a duration to another, rounding up
(function template) [edit]
converts a duration to another, rounding to nearest, ties to even
(function template) [edit]
obtains the absolute value of the duration
(function template) [edit]
performs stream output on a duration
(function template) [edit]
parses a duration from a stream according to the provided format
(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>>
std::chrono::days (since C++20) duration</*signed integer type of at least 25 bits*/, std::ratio<86400>>
std::chrono::weeks (since C++20) duration</*signed integer type of at least 22 bits*/, std::ratio<604800>>
std::chrono::months (since C++20) duration</*signed integer type of at least 20 bits*/, std::ratio<2629746>>
std::chrono::years (since C++20) duration</*signed integer type of at least 17 bits*/, std::ratio<31556952>>

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

Each of the predefined duration types days, weeks, months and years covers a range of at least ±40000 years. years is equal to 365.2425 days (the average length of a Gregorian year). months is equal to 30.436875 days (exactly 1/12 of years).

(since C++20)

Helper classes

specializes the std::common_type trait
(class template specialization) [edit]
indicates that a duration is convertible to duration with different tick period
(class template) [edit]
constructs zero, min, and max values of a tick count of given type
(class template) [edit]
formatting support for duration
(class template specialization) [edit]

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]

Note: the literal suffixes d and y do not refer to days and years but to day and year, respectively.

(since C++20)

Notes

The actual time interval (in seconds) that is held by a duration object d is roughly equal to d.count() * D::period::num / D::period::den, where D is of type chrono::duration<> and d is an object of such type.

Example

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

#include <iostream>
#include <chrono>
 
int main()
{
    using shakes = std::chrono::duration<int, std::ratio<1, 100'000'000>>;
    using jiffies = std::chrono::duration<int, std::centi>;
    using microfortnights = std::chrono::duration<float, 
           std::ratio_multiply<
                std::ratio_multiply<
                    std::ratio<2>,
                    std::chrono::weeks::period>, 
                std::micro>>;
    using nanocenturies = std::chrono::duration<float, 
           std::ratio_multiply<
                std::ratio_multiply<
                    std::hecto,
                    std::chrono::years::period>,
                std::nano>>;
 
    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.316887 nanocenturies