std::chrono::time_point

定义于头文件 <chrono>
template<

    class Clock,
    class Duration = typename Clock::duration

> class time_point;
(C++11 起)

类模板 std::chrono::time_point 表示时间中的一个点。它被实现成如同存储一个 Duration 类型的自 Clock 的纪元起始开始的时间间隔的值。

目录

成员类型

成员类型 定义
clock Clock ,此时间点计量于此时钟上
duration Duration ,用于计量从纪元起时间的 std::chrono::duration 类型
rep Rep ,表示时期的计次数的算术类型
period Period ,表示时期周期的 std::ratio 类型

成员函数

构造新的 time_point
(公开成员函数)
返回表示为从其时钟起点开始的时间点
(公开成员函数)
以给定的 duration 修改 time_point
(公开成员函数)
[静态]
返回对应最小时长的时间点
(公开静态成员函数)
[静态]
返回对应最大时长的时间点
(公开静态成员函数)

非成员函数

特化 std::common_type 特性
(类模板特化)
以给定的 duration 修改 time_point
(函数模板)
比较两个时间点
(函数模板)
转换 time_point 为同一时钟上拥有不同 duration 的另一 time_point
(函数模板)
转换 time_point 到另一个,向下取整
(函数模板)
转换 time_point 到另一个,向上取整
(函数模板)
转换 time_point 到另一个,取整至最接近者,偶数优先
(函数模板)

示例

#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
 
int main()
{
    std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
    std::time_t now_c = std::chrono::system_clock::to_time_t(now - std::chrono::hours(24));
    std::cout << "24 hours ago, the time was "
              << std::put_time(std::localtime(&now_c), "%F %T") << '\n';
 
    std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
    std::cout << "Hello World\n";
    std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
    std::cout << "Printing took "
              << std::chrono::duration_cast<std::chrono::microseconds>(end - start).count()
              << "us.\n";
}

可能的输出:

24 hours ago, the time was 2011-10-25 12:00:08
Hello World
Printing took 84us.

参阅

(C++11)
时间间隔
(类模板)

版本历史

  • (当前 | 先前 2017年12月7日 (四) 02:15Kafuuchino讨论 | 贡献. . (2,605字节) (+6). . (撤销)
  • 当前 | 先前 2017年5月7日 (日) 20:13Fruderica讨论 | 贡献. . (2,599字节) (-475). . (撤销)
  • 当前 | 先前 2014年10月26日 (日) 16:42P12bot讨论 | 贡献 . . (3,074字节) (0). . (Fix some translations) (撤销)
  • 当前 | 先前 2013年7月2日 (二) 07:22P12bot讨论 | 贡献 . . (3,074字节) (-265). . (Use {{lc}}. Update links. Various fixes.) (撤销)
  • 当前 | 先前 2012年11月2日 (五) 14:52P12bot讨论 | 贡献 . . (3,339字节) (+233). . (r2.7.3) (机器人添加:de, en, es, fr, it, ja, pt, ru) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 09:51P12讨论 | 贡献 . . (3,106字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前 2012年10月26日 (五) 06:00TranslationBot讨论 | 贡献. . (3,106字节) (-79). . (Translated from the English version using Google Translate) (撤销)
  • 当前 | 先前 2012年10月25日 (四) 13:15P12讨论 | 贡献 . . (3,185字节) (0). . (1个修订: Translate from the English version) (撤销)
  • 当前 | 先前) 2012年10月25日 (四) 12:00TranslationBot讨论 | 贡献. . (3,185字节) (+3,185). . (Translated from the English version using Google Translate)