目录
一 chrono库
- C++ 标准库chrono中有三个比较重要的概念,它们分别是:
- clocks 参考此前文章 C++11 clock.
- durations 参考此前文章 C++11 std::duration.
- time points
- 本文主要对 time_point 概念进行讨论。
二 time_point
-
类模板 std::chrono::time_point 表示时间中的一个点。它被实现成如同存储一个 Duration 类型的自 Clock 的epoch开始的时间间隔值。
Class template std::chrono::time_point represents a point in time. It is implemented as if it stores a value of type Duration indicating the time interval from the start of the Clock’s epoch.
-
定义于头文件 < chrono >
template<class Clock, class Duration = typename Clock::duration> class time_point; (C++11 起)
三 成员类型
- clock Clock, 用于计量时间点的时钟类型
- duration Duration ,用于计量从时钟epoch起的时间间隔类型
- rep Rep ,与duration ::rep相同含义,表示计次类型
- period Period ,与duration ::period 相同含义,表示时间单位(计次周期)类型
四 Demo辅助函数
- 下面Demo用到的辅助函数如下:
using namespace std; using Clock = chrono::high_resolution_clock; using TimePoint = chrono::time_point<Clock>; void print_ms(const TimePoint& point) { using Ms = chrono::milliseconds; const Clock::duration since_epoch = point.time_since_epoch(); cout << chrono::duration_cast<Ms>(since_epoch).count() << " ms\n"; } template <typename TP> string to_string(const TP& time_point) { return to_string(time_point.time_since_epoch().count()); }
五 成员函数
1 构造函数
-
定义
time_point(); (C++11 起)(C++14 前)(1) constexpr time_point(); (C++14 起)(1) explicit time_point( const duration& d ); (C++11 起)(C++14 前)(2) constexpr explicit time_point( const duration& d ); (C++14 起)(2) template< class Duration2 > time_point( const time_point<Clock,Duration2>& t ); (C++11 起)(C++14 前)(3) template< class Duration2 > constexpr time_point( const time_point<Clock,Duration2>& t );(C++14 起)(3)
-
解释
- (1) 默认构造,时间点为Clock的epoch
- (2) 构造的时间点为Clock的epoch + 时间间隔d
- (3) 用于隐式转换
-
例子
const TimePoint default_value = TimePoint(); // (1) print_ms(default_value); const Clock::duration duration_4_seconds = chrono::seconds(4); const TimePoint time_point_4_seconds(duration_4_seconds); // (2) print_ms(time_point_4_seconds); const TimePoint time_point_now = Clock::now(); // (3) print_ms(time_point_now);
-
结果
0 ms 4000 ms 587455781 ms
2 time_since_epoch
-
返回时间点到Clock epoch之间的时间间隔。
-
定义
duration time_since_epoch() const; (since C++11)(until C++14) constexpr duration time_since_epoch() const; (since C++14)
-
Demo
const auto p0 = chrono::time_point<chrono::system_clock>{}; const auto p1 = chrono::system_clock::now(); const auto p2 = p1 - chrono::hours(24); time_t epoch_time = chrono::system_clock::to_time_t(p0); cout << "epoch: " << ctime(&epoch_time); time_t today_time = chrono::system_clock::to_time_t(p1); cout << "today: " << ctime(&today_time); cout << "hours since epoch: " << chrono::duration_cast<chrono::hours>(p1.time_since_epoch()).count() << endl; cout << "yesterday, hours since epoch: " << chrono::duration_cast<chrono::hours>(p2.time_since_epoch()).count() << endl;
-
结果
epoch: Thu Jan 1 08:00:00 1970 today: Wed Sep 30 14:33:23 2020 hours since epoch: 444846 yesterday, hours since epoch: 444822
3 operator+= operator-=
- 将时间点加减指定的时间间隔。
- 定义
time_point& operator+=( const duration& d );(until C++17)(1) constexpr time_point& operator+=( const duration& d );(since C++17)(1) time_point& operator-=( const duration& d );(until C++17)(2) constexpr time_point& operator-=( const duration& d );(since C++17)(2)
- Demo
const Clock::duration duration_4_seconds = chrono::seconds(4); TimePoint time_point_4_seconds(duration_4_seconds); time_point_4_seconds += duration_4_seconds; print_ms(time_point_4_seconds);
- 结果
8000 ms
3 min max [static]
- 返回特殊时间点值
- Demo
print_ms(TimePoint::min()); print_ms(TimePoint::max());
- 结果
-9223372036854 ms 9223372036854 ms
4 operator++ ++(int) – --(int) (C++20)
- 略
六 非成员函数
1 算术运算
- operator+ operator-
- 定义
template <class C, class D1, class R2, class P2> time_point<C, typename common_type<D1, duration<R2, P2>>::type> operator+(const time_point<C, D1>& pt, const duration<R2, P2>& d); (since C++ 11)(until C++ 14)(1) template <class C, class D1, class R2, class P2> constexpr time_point<C, common_type_t<D1, duration<R2, P2>>> operator+(const time_point<C, D1>& pt, const duration<R2, P2>& d); (since C++ 14)(1) template <class R1, class P1, class C, class D2> time_point<C, typename common_type<duration<R1, P1>, D2>::type> operator+(const duration<R1, P1>& d, const time_point<C, D2>& pt); (since C++ 11)(until C++ 14)(2) template <class R1, class P1, class C, class D2> constexpr time_point<C, common_type_t<duration<R1, P1>, D2>> operator+(const duration<R1, P1>& d, const time_point<C, D2>& pt); (since C++ 14)(2) template <class C, class D1, class R2, class P2> time_point<C, typename common_type<D1, duration<R2, P2>>::type> operator-(const time_point<C, D1>& pt, const duration<R2, P2>& d); (since C++ 11)(until C++ 14)(3) template <class C, class D1, class R2, class P2> constexpr time_point<C, common_type_t<D1, duration<R2, P2>>> operator-(const time_point<C, D1>& pt, const duration<R2, P2>& d); (since C++ 14)(3) template <class C, class D1, class D2> typename common_type<D1, D2>::type operator-( const time_point<C, D1>& pt_lhs, const time_point<C, D2>& pt_rhs); (since C++ 11)(until C++ 14)(4) template <class C, class D1, class D2> constexpr common_type_t<D1, D2> operator-( const time_point<C, D1>& pt_lhs, const time_point<C, D2>& pt_rhs); (since C++ 14)(4)
- Demo
const Clock::duration duration_4_seconds = chrono::seconds(4); TimePoint time_point_4_seconds(duration_4_seconds); time_point_4_seconds = time_point_4_seconds + duration_4_seconds; print_ms(time_point_4_seconds); time_point_4_seconds = time_point_4_seconds - duration_4_seconds; print_ms(time_point_4_seconds); const Clock::duration milliseconds = chrono::milliseconds(10); time_point_4_seconds = time_point_4_seconds - milliseconds; print_ms(time_point_4_seconds);
- 结果
8000 ms 4000 ms 3990 ms
- 注意
- 前提是Clock相同
- 实际是Duration的转换运算
2 关系运算
- operator==,!=,<,<=,>,>=,<=>
- 略
3 time_point_cast
- 显式转换
- 定义
template <class ToDuration, class Clock, class Duration> time_point<Clock, ToDuration> time_point_cast( const time_point<Clock, Duration> &t);(since C++11)(until C++14) template <class ToDuration, class Clock, class Duration> constexpr time_point<Clock, ToDuration> time_point_cast( const time_point<Clock, Duration> &t);(since C++14)
- 说明
- Clock相同,实际是Duration的转换运算
Converts a std::chrono::time_point from one duration to another.
- Clock相同,实际是Duration的转换运算
- Demo
const Clock::duration duration_4_seconds = chrono::seconds(4); TimePoint time_point_4_seconds(duration_4_seconds); print_ms(chrono::time_point_cast<chrono::milliseconds>(time_point_4_seconds));
- 结果
4000 ms
4 floor ceil round
- floor向下取整;ceil向上取整;round就近取整,偶数优先;
- Demo
using namespace literals::chrono_literals; using Sec = chrono::seconds; cout << "Time point\t" "Cast\t" "Floor\t" "Round\t" "Ceil\n"; cout << "(ms)\t\t" "(s)\t" "(s)\t" "(s)\t" "(s)\n"; for (const auto value_ms : {5432ms, 5678ms}) { chrono::time_point<chrono::system_clock, chrono::milliseconds> time_point_ms(value_ms); cout << to_string(time_point_ms) << "\t\t" << to_string(chrono::time_point_cast<Sec>(time_point_ms)) << "\t" << to_string(chrono::floor<Sec>(time_point_ms)) << "\t" << to_string(chrono::round<Sec>(time_point_ms)) << "\t" << to_string(chrono::ceil<Sec>(time_point_ms)) << "\n"; }
- 结果
Time point Cast Floor Round Ceil (ms) (s) (s) (s) (s) 5432 5 5 5 6 5678 5 5 6 6
七 辅助类
std::common_type< std::chrono::time_point >
- std::common_type 特化)
- Demo
cout << typeid(common_type<chrono::time_point<Clock, chrono::seconds>, chrono::time_point<Clock, chrono::milliseconds>>::type).name() << endl;
- 结果
class std::chrono::time_point<struct std::chrono::steady_clock, class std::chrono::duration<__int64,struct std::ratio<1,1000> > >