A duration consists of a span of time, defined as some number of ticks of some time unit. For example, “42 seconds” could be represented by a duration consisting of 42 ticks of a 1-second time unit.
template<class Rep, class Period = std::ratio<1>>
class duration; (since C++11)
template<class DurationT>
void print(const std::string pre, const DurationT& d) {
std::cout << "(" << pre << ")";
std::cout << ".count(): " << d.count() << std::endl;
}
constexpr duration() = default;(1)(since C++11)
duration( const duration& ) = default;(2)(since C++11)
template< class Rep2 >
constexpr explicit duration( const Rep2& r );(3)(since C++11)
template< class Rep2, class Period2 >
constexpr duration( const duration<Rep2,Period2>& d );(4)(since C++11)
std::chrono::duration<int, std::ratio<1,2>> d1 {10};
print("d1", d1);
(d1).count(): 10
std::chrono::duration<uint8_t, std::ratio<1>>::zero());
std::chrono::duration<uint8_t, std::ratio<1>>::min());
std::chrono::duration<uint8_t, std::ratio<1>>::max());
constexpr duration operator+() const;(1)(until C++17)
constexpr std::common_type_t<duration> operator+() const;(1)(since C++17)
constexpr duration operator-() const;(2)(until C++17)
constexpr std::common_type_t<duration> operator-() const;(2)(since C++17)
std::chrono::duration<int, std::ratio<1,2>> d1 {10};
print("d1", d1);
std::chrono::duration<int, std::ratio<1,2>> d2 = +d1;
print("d2", d2);
d2 = -d1;
print("d2", d2);
(d1).count(): 10
(d2).count(): 10
(d2).count(): -10
duration& operator++();(1)(C++17 前)
constexpr duration& operator++();(1)(C++17 起)
duration operator++(int);(2)(C++17 前)
constexpr duration operator++(int);(2)(C++17 起)
duration& operator--();(3)(C++17 前)
constexpr duration& operator--();(3)(C++17 起)
duration operator--(int);(4)(C++17 前)
constexpr duration operator--(int);(4)(C++17 起)
std::chrono::duration<int, std::ratio<1, 2>> d1{10};
print("d1", d1);
print("d1++", d1++);
print("d1--", d1--);
print("++d1", ++d1);
print("--d1", --d1);
(d1).count(): 10
(d1++).count(): 10
(d1--).count(): 11
(++d1).count(): 11
(--d1).count(): 10
duration& operator+=(const duration& d);(1)(until C++17)
constexpr duration& operator+=(const duration& d);(1)(since C++17)
duration& operator-=(const duration& d);(2)(until C++17)
constexpr duration& operator-=(const duration& d);(2)(since C++17)
duration& operator*=(const rep& rhs);(3)(until C++17)
constexpr duration& operator*=(const rep& rhs);(3)(since C++17)
duration& operator/=(const rep& rhs);(4)(until C++17)
constexpr duration& operator/=(const rep& rhs);(4)(since C++17)
duration& operator%=(const rep& rhs);(5)(until C++17)
constexpr duration& operator%=(const rep& rhs);(5)(since C++17)
duration& operator%=(const duration& rhs);(6)(until C++17)
constexpr duration& operator%=(const duration& rhs);(6)(since C++17)
std::chrono::duration<int, std::ratio<1, 2>> d1{1};
print("d1", d1);
std::chrono::duration<int, std::ratio<1, 3>> d2{1};
std::chrono::duration<int, std::ratio<1>> d3{1};
// d1 += d2; 不存在从d2 Period 到 d1 Period的隐式转换
// 通俗的说就是复合赋值的右变量不能是 "短" period
d1 += d3;
print("d1", d1);
(d1).count(): 1
(d1).count(): 3
Implicit conversions between two durations normally depends on the tick period of the durations.
However, implicit conversions can happen regardless of tick period if std::chrono::treat_as_floating_point::value == true
template< class Rep1, class Period1, class Rep2, class Period2 >
typename std::common_type<duration<Rep1,Period1>, duration<Rep2,Period2>>::type
constexpr operator+( const duration<Rep1,Period1>& lhs,
const duration<Rep2,Period2>& rhs );(1)
template< class Rep1, class Period1, class Rep2, class Period2 >
typename std::common_type<duration<Rep1,Period1>, duration<Rep2,Period2>>::type
constexpr operator-( const duration<Rep1,Period1>& lhs,
const duration<Rep2,Period2>& rhs );(2)
template< class Rep1, class Period, class Rep2 >
duration<typename std::common_type<Rep1,Rep2>::type, Period>
constexpr operator*( const duration<Rep1,Period>& d, const Rep2& s );(3)
template< class Rep1, class Rep2, class Period >
duration<typename std::common_type<Rep1,Rep2>::type, Period>
constexpr operator*( const Rep1& s, const duration<Rep2,Period>& d );(4)
template< class Rep1, class Period, class Rep2 >
duration<typename std::common_type<Rep1,Rep2>::type, Period>
constexpr operator/( const duration<Rep1,Period>& d, const Rep2& s );(5)
template< class Rep1, class Period1, class Rep2, class Period2 >
typename std::common_type<Rep1,Rep2>::type
constexpr operator/( const duration<Rep1,Period1>& lhs,
const duration<Rep2,Period2>& rhs );(6)
template< class Rep1, class Period, class Rep2 >
duration<typename std::common_type<Rep1,Rep2>::type, Period>
constexpr operator%( const duration<Rep1, Period>& d, const Rep2& s );(7)
template< class Rep1, class Period1, class Rep2, class Period2 >
typename std::common_type<duration<Rep1,Period1>, duration<Rep2,Period2>>::type
constexpr operator%( const duration<Rep1,Period1>& lhs,
const duration<Rep2,Period2>& rhs );(8)
std::chrono::duration<int, std::ratio<1, 2>> d1{1};
print("d1", d1);
std::chrono::duration<int, std::ratio<1, 3>> d2{1};
print("d1+d2", d1 + d2);
std::cout << "typeid(decltype(d1+d2)).name(): " << std::endl
<< typeid(decltype(d1+d2)).name() << std::endl;
print("d1-d2", d1 - d2);
print("2*d1", 2 * d1);
print("d2*2", d2 * 2);
std::cout << "d1/d2 : " << d1 / d2 << std::endl; // 2
print("d1/2", d1 / 2);
print("d1%d2", d1 % d2);
print("d1%2", d1 % 2);
(d1).count(): 1
(d1+d2).count(): 5 // 1
typeid(decltype(d1+d2)).name(): // 1
class std::chrono::duration<int,struct std::ratio<1,6> >
(d1-d2).count(): 1
(2*d1).count(): 2
(d2*2).count(): 2
d1/d2 : 1 // 2
(d1/2).count(): 0
(d1%d2).count(): 1
(d1%2).count(): 1
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator==(const std::chrono::duration<Rep1, Period1>& lhs,
const std::chrono::duration<Rep2, Period2>& rhs);(1)(since C++11)
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator!=(const std::chrono::duration<Rep1, Period1>& lhs,
const std::chrono::duration<Rep2, Period2>& rhs);(2)(since C++11)(until C++20)
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator<(const std::chrono::duration<Rep1, Period1>& lhs,
const std::chrono::duration<Rep2, Period2>& rhs);(3)(since C++11)
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator<=(const std::chrono::duration<Rep1, Period1>& lhs,
onst std::chrono::duration<Rep2, Period2>& rhs);(4)(since C++11)
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator>(const std::chrono::duration<Rep1, Period1>& lhs,
const std::chrono::duration<Rep2, Period2>& rhs);(5)(since C++11)
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator>=(const std::chrono::duration<Rep1, Period1>& lhs,
const std::chrono::duration<Rep2, Period2>& rhs);(6)(since C++11)
template <class Rep1, class Period1, class Rep2, class Period2>
requires std::three_way_comparable<std::common_type_t<Rep1, Rep2>>
constexpr auto operator<=>(const std::chrono::duration<Rep1, Period1>& lhs,
const std::chrono::duration<Rep2, Period2>& rhs);(7)(since C++20)
std::chrono::duration<int, std::ratio<1, 2>> d1{1};
std::chrono::duration<int, std::ratio<1, 3>> d2{1};
std::cout << "d1==d2 : " << (d1==d2) << std::endl;
d1==d2 : 0
template <class ToDuration, class Rep, class Period>
constexpr ToDuration duration_cast(const duration<Rep,Period>& d);(C++11 起)
std::chrono::duration<int, std::ratio<1, 3>> d1{1};
auto d2 = std::chrono::duration_cast<std::chrono::duration<int,
std::ratio<1, 10>>>(d1);
print("d2", d2);
(d2).count(): 3
std::chrono::duration<int, std::ratio<1, 3>> d1{1};
auto d3 = std::chrono::floor<std::chrono::duration<int,
std::ratio<1, 10>>>(d1);
print("d3", d3);
auto d4 =
std::chrono::ceil<std::chrono::duration<int, std::ratio<1, 10>>>(d1);
print("d4", d4);
auto d5 =
std::chrono::round<std::chrono::duration<int, std::ratio<1, 10>>>(d1);
print("d5", d5);
auto d5_1 =std::chrono::round<std::chrono::duration<int,
std::ratio<1, 5>>>(d5);
print("d5_1", d5_1);
std::chrono::duration<int, std::ratio<1, 3>> d6{-1};
auto d7 = std::chrono::abs(d6);
print("d7", d7);
(d3).count(): 3
(d4).count(): 4
(d5).count(): 3 // round 就近取整
(d5_1).count(): 2 // round 偶数优先
(d7).count(): 1
std::chrono::nanoseconds;
std::chrono::microseconds;
std::chrono::milliseconds;
std::chrono::seconds;
std::chrono::minutes;
std::chrono::hours;
// C++20
// std::chrono::days
// std::chrono::weeks
// std::chrono::months
// std::chrono::years
std::this_thread::sleep_for(std::chrono::seconds(1));
std::chrono::seconds d1(1);
std::chrono::minutes d2(1);
typedef std::common_type<std::chrono::seconds,
std::chrono::minutes>::type type;
std::cout << typeid(type).name() << std::endl;
type d3(d2);
print("d3", d3);
class std::chrono::duration<__int64,struct std::ratio<1,1> >
(d3).count(): 60
auto v = std::chrono::treat_as_floating_point<std::chrono::milliseconds::rep>::value;
template <class Rep>
struct duration_values;(C++11 起)
std::cout << std::chrono::duration_values<std::chrono::seconds::rep>::zero()
<< std::endl;
std::cout << std::chrono::duration_values<std::chrono::seconds::rep>::min()
<< std::endl;
std::cout << std::chrono::duration_values<std::chrono::seconds::rep>::max()
<< std::endl;
0
-9223372036854775808
9223372036854775807
operator"“h(C++14) 表示小时的 std::chrono::duration 字面量
operator”“min(C++14) 表示分钟的 std::chrono::duration 字面量
operator”“s (C++14) 表示秒的 std::chrono::duration 字面量
operator”“ms(C++14) 表示毫秒的 std::chrono::duration 字面量
operator”“us(C++14) 表示微秒的 std::chrono::duration 字面量
operator”"ns(C++14) 表示纳秒的 std::chrono::duration 字面量
// 例子1
using namespace std::chrono_literals;
print("10h", 10h);
// 例子2
std::this_thread::sleep_for(1s);
// 例子1
(10h).count(): 10