23 General utilities library [utilities]
A
duration type measures time between two points in time (
time_points)
. A
duration has a representation which holds a count of ticks and a tick period
. The tick period is the amount of time which occurs from one tick to the next, in units
of seconds
. It is expressed as a rational constant using the template
ratio.
template <class Rep, class Period = ratio<1>>
class duration {
public:
using rep = Rep;
using period = typename Period::type;
private:
rep rep_; public:
constexpr duration() = default;
template <class Rep2>
constexpr explicit duration(const Rep2& r);
template <class Rep2, class Period2>
constexpr duration(const duration<Rep2, Period2>& d);
~duration() = default;
duration(const duration&) = default;
duration& operator=(const duration&) = default;
constexpr rep count() const;
constexpr common_type_t<duration> operator+() const;
constexpr common_type_t<duration> operator-() const;
constexpr duration& operator++();
constexpr duration operator++(int);
constexpr duration& operator--();
constexpr duration operator--(int);
constexpr duration& operator+=(const duration& d);
constexpr duration& operator-=(const duration& d);
constexpr duration& operator*=(const rep& rhs);
constexpr duration& operator/=(const rep& rhs);
constexpr duration& operator%=(const rep& rhs);
constexpr duration& operator%=(const duration& rhs);
static constexpr duration zero();
static constexpr duration min();
static constexpr duration max();
};
Rep shall be an arithmetic type or a class emulating an arithmetic type
. If
duration is instantiated with a
duration type as the argument for the template
parameter
Rep, the program is ill-formed
.If
Period is not a specialization of
ratio, the program is ill-formed
. If
Period::num is not positive, the program is ill-formed
.Members of
duration shall not throw exceptions other than
those thrown by the indicated operations on their representations
.The defaulted copy constructor of duration shall be a
constexpr function if and only if the required initialization
of the member
rep_ for copy and move, respectively, would
satisfy the requirements for a constexpr function
.[
Example:
duration<long, ratio<60>> d0; duration<long long, milli> d1; duration<double, ratio<1, 30>> d2;
—
end example ]
template <class Rep2>
constexpr explicit duration(const Rep2& r);
Remarks: This constructor shall not participate in overload
resolution unless
Rep2 is implicitly convertible to
rep and
[
Example:
duration<int, milli> d(3); duration<int, milli> d(3.5);
—
end example ]
Effects: Constructs an object of type
duration. Postconditions: count() == static_cast<rep>(r). template <class Rep2, class Period2>
constexpr duration(const duration<Rep2, Period2>& d);
Remarks: This constructor shall not participate in overload resolution unless
no overflow is induced in the conversion and
treat_as_floating_point_v<rep> is
true or both
ratio_divide<Period2, period>::den is
1 and
treat_as_floating_point_v<Rep2> is
false. [
Note: This
requirement prevents implicit truncation error when converting between
integral-based
duration types
. Such a construction could easily lead to
confusion about the value of the
duration. —
end note ]
[
Example:
duration<int, milli> ms(3);
duration<int, micro> us = ms; duration<int, milli> ms2 = us;
—
end example ]
Effects: Constructs an object of type
duration, constructing
rep_ from
duration_cast<duration>(d).count(). constexpr rep count() const;
constexpr common_type_t<duration> operator+() const;
Returns: common_type_t<duration>(*this). constexpr common_type_t<duration> operator-() const;
Returns: common_type_t<duration>(-rep_). constexpr duration& operator++();
Effects: As if by
++rep_. constexpr duration operator++(int);
Returns: duration(rep_++). constexpr duration& operator--();
Effects: As if by
--rep_. constexpr duration operator--(int);
Returns: duration(rep_--). constexpr duration& operator+=(const duration& d);
Effects: As if by: rep_ += d.count();
constexpr duration& operator-=(const duration& d);
Effects: As if by: rep_ -= d.count();
constexpr duration& operator*=(const rep& rhs);
Effects: As if by: rep_ *= rhs;
constexpr duration& operator/=(const rep& rhs);
Effects: As if by: rep_ /= rhs;
constexpr duration& operator%=(const rep& rhs);
Effects: As if by: rep_ %= rhs;
constexpr duration& operator%=(const duration& rhs);
Effects: As if by: rep_ %= rhs.count();
static constexpr duration zero();
Returns: duration(duration_values<rep>::zero()). static constexpr duration min();
Returns: duration(duration_values<rep>::min()). static constexpr duration max();
Returns: duration(duration_values<rep>::max()). In the function descriptions that follow,
CD represents the return type
of the function
. CR(A, B) represents
common_type_t<A, B>. template <class Rep1, class Period1, class Rep2, class Period2>
constexpr common_type_t<duration<Rep1, Period1>, duration<Rep2, Period2>>
operator+(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
Returns: CD(CD(lhs).count() + CD(rhs).count()). template <class Rep1, class Period1, class Rep2, class Period2>
constexpr common_type_t<duration<Rep1, Period1>, duration<Rep2, Period2>>
operator-(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
Returns: CD(CD(lhs).count() - CD(rhs).count()). template <class Rep1, class Period, class Rep2>
constexpr duration<common_type_t<Rep1, Rep2>, Period>
operator*(const duration<Rep1, Period>& d, const Rep2& s);
Remarks: This operator shall not participate in overload
resolution unless
Rep2 is implicitly convertible to
CR(Rep1, Rep2). Returns: CD(CD(d).count() * s). template <class Rep1, class Rep2, class Period>
constexpr duration<common_type_t<Rep1, Rep2>, Period>
operator*(const Rep1& s, const duration<Rep2, Period>& d);
Remarks: This operator shall not participate in overload
resolution unless
Rep1 is implicitly convertible to
CR(Rep1, Rep2). template <class Rep1, class Period, class Rep2>
constexpr duration<common_type_t<Rep1, Rep2>, Period>
operator/(const duration<Rep1, Period>& d, const Rep2& s);
Remarks: This operator shall not participate in overload
resolution unless
Rep2 is implicitly convertible to
CR(Rep1, Rep2)
and
Rep2 is not a specialization of
duration. Returns: CD(CD(d).count() / s). template <class Rep1, class Period1, class Rep2, class Period2>
constexpr common_type_t<Rep1, Rep2>
operator/(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
Returns: CD(lhs).count() / CD(rhs).count(). template <class Rep1, class Period, class Rep2>
constexpr duration<common_type_t<Rep1, Rep2>, Period>
operator%(const duration<Rep1, Period>& d, const Rep2& s);
Remarks: This operator shall not participate in overload
resolution unless
Rep2 is implicitly convertible to
CR(Rep1, Rep2) and
Rep2 is not a specialization of
duration. Returns: CD(CD(d).count() % s). template <class Rep1, class Period1, class Rep2, class Period2>
constexpr common_type_t<duration<Rep1, Period1>, duration<Rep2, Period2>>
operator%(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
Returns: CD(CD(lhs).count() % CD(rhs).count()). In the function descriptions that follow,
CT represents
common_type_t<A, B>, where
A and
B are the types of
the two arguments to the function
. template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator==(const duration<Rep1, Period1>& lhs,
const duration<Rep2, Period2>& rhs);
Returns: CT(lhs).count() == CT(rhs).count(). template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator!=(const duration<Rep1, Period1>& lhs,
const duration<Rep2, Period2>& rhs);
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator<(const duration<Rep1, Period1>& lhs,
const duration<Rep2, Period2>& rhs);
Returns: CT(lhs).count() < CT(rhs).count(). template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator<=(const duration<Rep1, Period1>& lhs,
const duration<Rep2, Period2>& rhs);
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator>(const duration<Rep1, Period1>& lhs,
const duration<Rep2, Period2>& rhs);
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator>=(const duration<Rep1, Period1>& lhs,
const duration<Rep2, Period2>& rhs);
template <class ToDuration, class Rep, class Period>
constexpr ToDuration duration_cast(const duration<Rep, Period>& d);
Remarks: This function shall not participate in overload resolution
unless
ToDuration is a specialization of
duration. Returns: Let
CF be
ratio_divide<Period, typename
ToDuration::period>, and
CR be
common_type< typename
ToDuration::rep, Rep, intmax_t>::type. If
CF::num == 1 and
CF::den == 1, returns
ToDuration(static_cast<typename ToDuration::rep>(d.count()))
otherwise, if
CF::num != 1 and
CF::den == 1, returns
ToDuration(static_cast<typename ToDuration::rep>(
static_cast<CR>(d.count()) * static_cast<CR>(CF::num)))
otherwise, if
CF::num == 1 and
CF::den != 1, returns
ToDuration(static_cast<typename ToDuration::rep>(
static_cast<CR>(d.count()) / static_cast<CR>(CF::den)))
otherwise, returns
ToDuration(static_cast<typename ToDuration::rep>(
static_cast<CR>(d.count()) * static_cast<CR>(CF::num) / static_cast<CR>(CF::den)))
[
Note: This function does not use any implicit conversions; all conversions
are done with
static_cast. It avoids multiplications and divisions when
it is known at compile time that one or more arguments is 1
. Intermediate
computations are carried out in the widest representation and only converted to
the destination representation at the final step
. —
end note ]
template <class ToDuration, class Rep, class Period>
constexpr ToDuration floor(const duration<Rep, Period>& d);
Remarks: This function shall not participate in overload resolution
unless
ToDuration is a specialization of
duration. Returns: The greatest result
t representable in
ToDuration
for which
t <= d. template <class ToDuration, class Rep, class Period>
constexpr ToDuration ceil(const duration<Rep, Period>& d);
Remarks: This function shall not participate in overload resolution
unless
ToDuration is a specialization of
duration. Returns: The least result
t representable in
ToDuration
for which
t >= d. template <class ToDuration, class Rep, class Period>
constexpr ToDuration round(const duration<Rep, Period>& d);
Remarks: This function shall not participate in overload resolution
unless
ToDuration is a specialization of
duration,
and
treat_as_floating_point_v<typename ToDuration::rep>
is
false. Returns: The value of
ToDuration that is closest to
d. If there are two closest values, then return the value
t
for which
t % 2 == 0.This section describes literal suffixes for constructing duration literals
. The
suffixes
h,
min,
s,
ms,
us,
ns
denote duration values of the corresponding types
hours,
minutes,
seconds,
milliseconds,
microseconds, and
nanoseconds
respectively if they are applied to integral literals
.If any of these suffixes are applied to a floating-point literal the result is a
chrono::duration literal with an unspecified floating-point representation
.If any of these suffixes are applied to an integer literal and the resulting
chrono::duration value cannot be represented in the result type because
of overflow, the program is ill-formed
.[
Example: The following code shows some duration literals
.
using namespace std::chrono_literals;
auto constexpr aday=24h;
auto constexpr lesson=45min;
auto constexpr halfanhour=0.5h;
—
end example ]
constexpr chrono::hours operator""h(unsigned long long hours);
constexpr chrono::duration<unspecified, ratio<3600, 1>> operator""h(long double hours);
Returns:
A
duration literal representing
hours hours
. constexpr chrono::minutes operator""min(unsigned long long minutes);
constexpr chrono::duration<unspecified, ratio<60, 1>> operator""min(long double minutes);
Returns:
A
duration literal representing
minutes minutes
. constexpr chrono::seconds operator""s(unsigned long long sec);
constexpr chrono::duration<unspecified> operator""s(long double sec);
Returns:
A
duration literal representing
sec seconds
. [
Note: The same suffix
s is used for
basic_string but there is no
conflict, since duration suffixes apply to numbers and string literal suffixes
apply to character array literals
. —
end note ]
constexpr chrono::milliseconds operator""ms(unsigned long long msec);
constexpr chrono::duration<unspecified, milli> operator""ms(long double msec);
Returns:
A
duration literal representing
msec milliseconds
. constexpr chrono::microseconds operator""us(unsigned long long usec);
constexpr chrono::duration<unspecified, micro> operator""us(long double usec);
Returns:
A
duration literal representing
usec microseconds
. constexpr chrono::nanoseconds operator""ns(unsigned long long nsec);
constexpr chrono::duration<unspecified, nano> operator""ns(long double nsec);
Returns:
A
duration literal representing
nsec nanoseconds
. template <class Rep, class Period>
constexpr duration<Rep, Period> abs(duration<Rep, Period> d);
Remarks: This function shall not participate in overload resolution
unless
numeric_limits<Rep>::is_signed is
true. Returns: If
d >= d.zero(), return
d,
otherwise return
-d.