Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::chrono::

<system_clock>

类    <chrono>
class system_clock;

系统时钟
提供对当前时间点访问的时钟类。

具体来说,system_clock是一个系统级的实时时钟。

☲  时钟属性


实时
它表示实时时钟,因此可以以某种方式将其转换为日历表示(请参阅to_time_t和from_time_t成员函数)。

有符号计数
它的时间点值可以引用时间戳之前的时间(负值)。

系统级
系统上运行的所有进程都应该使用这个时钟获取相同的时间点值。

☞ 成员类型



以下是system_clock的成员类型的别名:
成员类型 定义 注释
rep 有符号算术类型(或模拟该类型的类) 用于存储时间单位的计数器。
period 一种比率类型 以秒为单位表示单位时间的比率类型。
time_point time_point<system_clock> 时钟的时间点类型。
duration duration<rep,period> 时钟的持续时间类型

☞ 成员常量



成员常量 定义
is_steady 一个bool值,指定时钟是否总是前进,以及它是否在相对于物理时间的恒定状态下前进。 如果为true,说明可能无法调整系统时钟。参阅 steady_clock.

☞ 静态成员函数



now 获取当前时间 (静态成员函数)
to_time_t 转换为time_t (静态成员函数)
from_time_t 从time_t转换(静态成员函数)

☣  示例



// system_clock example
#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>

int main ()
{
  using std::chrono::system_clock;

  std::chrono::duration<int,std::ratio<60*60*24> > one_day (1);

  system_clock::time_point today = system_clock::now();
  system_clock::time_point tomorrow = today + one_day;

  std::time_t tt;

  tt = system_clock::to_time_t ( today );
  std::cout << "today is: " << ctime(&tt);

  tt = system_clock::to_time_t ( tomorrow );
  std::cout << "tomorrow will be: " << ctime(&tt);

  return 0;
}

可能输出:
today is: Wed May 30 12:25:03 2012 tomorrow will be: Thu May 31 12:25:03 2012

🍄  另请参阅



steady_clock 恒定时钟(类)
high_resolution_clock 高精度时钟(类)

联系我们 免责声明 关于CandCplus 网站地图