标准库头文件 <chrono>

来自cppreference.com
< cpp‎ | header

此头文件是日期和时间库的一部分。

目录

[编辑]

定义于命名空间 std::chrono
(C++11)
时间区间
(类模板) [编辑]
来自系统范畴实时时钟的壁钟时间
(类) [编辑]
决不被调整的单调时钟
(类) [编辑]
拥有可用的最短计次周期的时钟
(类) [编辑]
时间中的点
(类模板) [编辑]
指示时长可转换为拥有不同计次周期的时长
(类模板)
构造给定类型计次的零、最小及最大值
(类模板)
便利 typedef
定义于命名空间 std::chrono
std::chrono::nanoseconds Period 为 std::nano 的时长类型
std::chrono::microseconds Period 为 std::micro 的时长类型
std::chrono::milliseconds Period 为 std::milli 的时长类型
std::chrono::seconds Period 为 std::ratio<1> 的时长类型
std::chrono::minutes Period 为 std::ratio<60> 的时长类型
std::chrono::hours Period 为 std::ratio<3600> 的时长类型
特化
定义于命名空间 std
特化 std::common_type 特性
(类模板特化) [编辑]
特化 std::common_type 特性
(类模板特化) [编辑]

[编辑] 函数

duration
定义于命名空间 std::chrono
实现以时长为参数的算术运算
(函数模板) [编辑]
比较两个时长
(函数模板) [编辑]
转换 duration 到另一个拥有不同计次间隔者
(函数模板) [编辑]
以向下取整的方式,将一个时间间隔转换为另一个
(函数模板) [编辑]
以向上取整的方式,将一个时间间隔转换为另一个
(函数模板) [编辑]
转换 duration 到另一个,就近取整,倾向偶数
(函数模板) [编辑]
获取时间间隔的绝对值
(函数模板) [编辑]
time_point
定义于命名空间 std::chrono
以给定的 duration 修改 time_point
(函数模板) [编辑]
比较两个时间点
(函数模板) [编辑]
转换 time_point 为同一时钟上拥有不同 duration 的另一 time_point
(函数模板) [编辑]
转换 time_point 到另一个,向下取整
(函数模板) [编辑]
转换 time_point 到另一个,向上取整
(函数模板) [编辑]
转换 time_point 到另一个,取整至最接近者,偶数优先
(函数模板) [编辑]
字面量
定义于内联命名空间 std::literals::chrono_literals
表示小时的 std::chrono::duration 字面量
(函数) [编辑]
表示分钟的 std::chrono::duration 字面量
(函数) [编辑]
表示秒的 std::chrono::duration 字面量
(函数) [编辑]
表示毫秒的 std::chrono::duration 字面量
(函数) [编辑]
表示微秒的 std::chrono::duration 字面量
(函数) [编辑]
表示纳秒的 std::chrono::duration 字面量
(函数) [编辑]

[编辑] 概要

namespace std {
  namespace chrono {
    // 类模板 duration
    template <class Rep, class Period = ratio<1>> class duration;
    // 类模板 time_point
    template <class Clock, class Duration = typename Clock::duration> class time_point;
  }
  // common_type 特化
  template <class Rep1, class Period1, class Rep2, class Period2>
  struct common_type<chrono::duration<Rep1, Period1>,
                     chrono::duration<Rep2, Period2>>;
  template <class Clock, class Duration1, class Duration2>
  struct common_type<chrono::time_point<Clock, Duration1>,
                     chrono::time_point<Clock, Duration2>>;
  namespace chrono {
    // 定制的特性
    template <class Rep> struct treat_as_floating_point;
    template <class Rep> struct duration_values;
    template <class Rep>
    constexpr bool treat_as_floating_point_v = treat_as_floating_point<Rep>::value;
    // duration 算术
    template <class Rep1, class Period1, class Rep2, class Period2>
    common_type_t<duration<Rep1, Period1>, duration<Rep2, Period2>>
    constexpr operator+(const duration<Rep1, Period1>& lhs,
                        const duration<Rep2, Period2>& rhs);
    template <class Rep1, class Period1, class Rep2, class Period2>
    common_type_t<duration<Rep1, Period1>, duration<Rep2, Period2>>
    constexpr operator-(const duration<Rep1, Period1>& lhs,
                        const duration<Rep2, Period2>& rhs);
    template <class Rep1, class Period, class Rep2>
    duration<common_type_t<Rep1, Rep2>, Period>
    constexpr operator*(const duration<Rep1, Period>& d,
                        const Rep2& s);
    template <class Rep1, class Rep2, class Period>
    duration<common_type_t<Rep1, Rep2>, Period>
    constexpr operator*(const Rep1& s,
                        const duration<Rep2, Period>& d);
    template <class Rep1, class Period, class Rep2>
    duration<common_type_t<Rep1, Rep2>, Period>
    constexpr operator/(const duration<Rep1, Period>& d,
                        const Rep2& s);
    template <class Rep1, class Period1, class Rep2, class Period2>
    common_type_t<Rep1, Rep2>
    constexpr operator/(const duration<Rep1, Period1>& lhs,
                        const duration<Rep2, Period2>& rhs);
    template <class Rep1, class Period, class Rep2>
    duration<common_type_t<Rep1, Rep2>, Period>
    constexpr operator%(const duration<Rep1, Period>& d,
                        const Rep2& s);
    template <class Rep1, class Period1, class Rep2, class Period2>
    common_type_t<duration<Rep1, Period1>, duration<Rep2, Period2>>
    constexpr operator%(const duration<Rep1, Period1>& lhs,
                        const duration<Rep2, Period2>& rhs);
    // duration 比较
    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 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);
    // duration_cast
    template <class ToDuration, class Rep, class Period>
    constexpr ToDuration duration_cast(const duration<Rep, Period>& d);
    template <class ToDuration, class Rep, class Period>
    constexpr ToDuration floor(const duration<Rep, Period>& d);
    template <class ToDuration, class Rep, class Period>
    constexpr ToDuration ceil(const duration<Rep, Period>& d);
    template <class ToDuration, class Rep, class Period>
    constexpr ToDuration round(const duration<Rep, Period>& d);
    // 便利 typedef
    using nanoseconds = duration</*至少 64 位的有符号整数类型*/ , nano>;
    using microseconds = duration</*至少 55 位的有符号整数类型*/ , micro>;
    using milliseconds = duration</*至少 45 位的有符号整数类型*/ , milli>;
    using seconds = duration</*至少 35 位的有符号整数类型*/ >;
    using minutes = duration</*至少 29 位的有符号整数类型*/ , ratio< 60>>;
    using hours = duration</*至少 23 位的有符号整数类型*/ , ratio<3600>>;
    // time_point 算术
    template <class Clock, class Duration1, class Rep2, class Period2>
    constexpr time_point<Clock, common_type_t<Duration1, duration<Rep2, Period2>>>
    operator+(const time_point<Clock, Duration1>& lhs,
              const duration<Rep2, Period2>& rhs);
    template <class Rep1, class Period1, class Clock, class Duration2>
    constexpr time_point<Clock, common_type_t<duration<Rep1, Period1>, Duration2>>
    operator+(const duration<Rep1, Period1>& lhs,
              const time_point<Clock, Duration2>& rhs);
    template <class Clock, class Duration1, class Rep2, class Period2>
    constexpr time_point<Clock, common_type_t<Duration1, duration<Rep2, Period2>>>
    operator-(const time_point<Clock, Duration1>& lhs,
              const duration<Rep2, Period2>& rhs);
    template <class Clock, class Duration1, class Duration2>
    constexpr common_type_t<Duration1, Duration2>
    operator-(const time_point<Clock, Duration1>& lhs,
              const time_point<Clock, Duration2>& rhs);
    // time_point 比较
    template <class Clock, class Duration1, class Duration2>
    constexpr bool operator==(const time_point<Clock, Duration1>& lhs,
                              const time_point<Clock, Duration2>& rhs);
    template <class Clock, class Duration1, class Duration2>
    constexpr bool operator!=(const time_point<Clock, Duration1>& lhs,
                              const time_point<Clock, Duration2>& rhs);
    template <class Clock, class Duration1, class Duration2>
    constexpr bool operator< (const time_point<Clock, Duration1>& lhs,
                              const time_point<Clock, Duration2>& rhs);
    template <class Clock, class Duration1, class Duration2>
    constexpr bool operator<=(const time_point<Clock, Duration1>& lhs,
                              const time_point<Clock, Duration2>& rhs);
    template <class Clock, class Duration1, class Duration2>
    constexpr bool operator> (const time_point<Clock, Duration1>& lhs,
                              const time_point<Clock, Duration2>& rhs);
    template <class Clock, class Duration1, class Duration2>
    constexpr bool operator>=(const time_point<Clock, Duration1>& lhs,
                              const time_point<Clock, Duration2>& rhs);
    // time_point_cast
    template <class ToDuration, class Clock, class Duration>
    constexpr time_point<Clock, ToDuration>
    time_point_cast(const time_point<Clock, Duration>& t);
    template <class ToDuration, class Clock, class Duration>
    constexpr time_point<Clock, ToDuration>
    floor(const time_point<Clock, Duration>& tp);
    template <class ToDuration, class Clock, class Duration>
    constexpr time_point<Clock, ToDuration>
    ceil(const time_point<Clock, Duration>& tp);
    template <class ToDuration, class Clock, class Duration>
    constexpr time_point<Clock, ToDuration>
    round(const time_point<Clock, Duration>& tp);
    // 特化的算法
    template <class Rep, class Period>
    constexpr duration<Rep, Period> abs(duration<Rep, Period> d);
    // 时钟
    class system_clock;
    class steady_clock;
    class high_resolution_clock;
  }
  inline namespace literals {
    inline namespace chrono_literals {
      // suffixes for duration literals
      constexpr chrono::hours operator "" h(unsigned long long);
      constexpr chrono::duration</*unspecified*/,
                                 ratio<3600,1>> operator "" h(long double);
      constexpr chrono::minutes operator "" min(unsigned long long);
      constexpr chrono::duration</*unspecified*/,
                                 ratio<60,1>> operator "" min(long double);
      constexpr chrono::seconds operator "" s(unsigned long long);
      constexpr chrono::duration</*unspecified*/ > operator "" s(long double);
      constexpr chrono::milliseconds operator "" ms(unsigned long long);
      constexpr chrono::duration</*unspecified*/,
                                 milli> operator "" ms(long double);
      constexpr chrono::microseconds operator "" us(unsigned long long);
      constexpr chrono::duration</*unspecified*/,
                                 micro> operator "" us(long double);
      constexpr chrono::nanoseconds operator "" ns(unsigned long long);
      constexpr chrono::duration</*unspecified*/, 
                                 nano> operator "" ns(long double);
    }
  }
  namespace chrono {
    using namespace literals::chrono_literals;
  }

[编辑] std::chrono::treat_as_floating_point

template <class Rep> struct treat_as_floating_point : is_floating_point<Rep> { };

[编辑] std::chrono::duration_values

template <class Rep>
struct duration_values {
public:
  static constexpr Rep zero();
  static constexpr Rep min();
  static constexpr Rep max();
};

[编辑] std::chrono::duration

template <class Rep, class Period = ratio<1>>
class duration {
public:
  using rep = Rep;
  using period = Period;
private:
  rep rep_; // 仅为说明
public:
  // construct/copy/destroy
  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 duration operator+() const;
  constexpr 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();
};

[编辑] std::chrono::time_point

template <class Clock, class Duration = typename Clock::duration>
class time_point {
public:
  using clock = Clock;
  using duration = Duration;
  using rep = typename duration::rep;
  using period = typename duration::period;
private:
  duration d_; // 仅为说明
public:
  // 构造
  constexpr time_point(); // 拥有纪元值
  constexpr explicit time_point(const duration& d); // 同 time_point() + d
  template <class Duration2>
  constexpr time_point(const time_point<clock, Duration2>& t);
  // 观察器
  constexpr duration time_since_epoch() const;
  // 算术
  constexpr time_point& operator+=(const duration& d);
  constexpr time_point& operator-=(const duration& d);
  // 特殊值
  static constexpr time_point min();
  static constexpr time_point max();
};

[编辑] std::chrono::system_clock

class system_clock {
public:
  using rep = /*see description*/ ;
  using period = ratio</*unspecified*/, /*unspecified*/ >;
  using duration = chrono::duration<rep, period>;
  using time_point = chrono::time_point<system_clock>;
  static constexpr bool is_steady = /*unspecified*/ ;
  static time_point now() noexcept;
  // 映射到 C API
  static time_t to_time_t (const time_point& t) noexcept;
  static time_point from_time_t(time_t t) noexcept;
};

[编辑] std::chrono::steady_clock

class steady_clock {
public:
  using rep = /*unspecified*/ ;
  using period = ratio</*unspecified*/, /*unspecified*/ >;
  using duration = chrono::duration<rep, period>;
  using time_point = chrono::time_point</*unspecified*/, duration>;
  static constexpr bool is_steady = true;
  static time_point now() noexcept;
};

[编辑] std::chrono::high_resolution_clock

class high_resolution_clock {
public:
  using rep = /*unspecified*/ ;
  using period = ratio</*unspecified*/, /*unspecified*/ >;
  using duration = chrono::duration<rep, period>;
  using time_point = chrono::time_point</*unspecified*/, duration>;
  static constexpr bool is_steady = /*unspecified*/ ;
  static time_point now() noexcept;
};