chrono介绍

article/2025/10/5 1:45:13

chrono是在std下,属于C++11的标准,它的头文件为#include<chrono>,是一个处理时间的元素。主要包括三个概念:

  1. Durantions:时间段
  2. Time points:时间点
  3. Clocks:包括system_clock(系统时钟),steady_clock(稳定时钟);high_resolution_clock(高精度时钟);

1. Durations

std::chrono::duration表示一段时间,它的模板类型为:

 template <class Rep, class Period = ratio<1> > class duration;
  • Rep:算术类型,比如int,float,double。
  • Period:时间单位,为ratio类型。默认类型以秒为单位。
  • 可以使用内部的.count函数对它进行使用。

常用的duration<REP,Period>类型如下:

typeRepresentationPeriod
hourssigned integral type of at least 23 bitsratio<3600,1>
minutessigned integral type of at least 29 bitsratio<60,1>
secondssigned integral type of at least 35 bitsratio<1,1>
millisecondssigned integral type of at least 45 bitsratio<1,1000>
microsecondssigned integral type of at least 55 bitsratio<1,1000000>
nanosecondssigned integral type of at least 64 bitsratio<1,1000000000>

使用举例:

#include <iostream>    // std::cout
#include <chrono>      // std::chrono::seconds, std::chrono::milliseconds// std::chrono::duration_cast
int main ()
{using namespace std::chrono;// std::chrono::milliseconds is an instatiation of std::chrono::duration:milliseconds foo (1000); // 1 second,1000微秒foo*=60;std::cout << "duration (in periods): ";std::cout << foo.count() << " milliseconds.\n";std::cout << "duration (in seconds): ";std::cout << foo.count() * milliseconds::period::num / milliseconds::period::den;
//den表示分母,num表示分子,ratio的单位是秒
//所以milliseconds::period::num=1000,milliseconds::period::den = 1;//所以num/den表示千分之一
//同理hours::period::num = 1;hours::period::den = 3600;最好用作比例std::cout << " seconds.\n";return 0;
}

输出结果:
在这里插入图片描述

  • ratio类型(默认:秒)

它的模板类型为

template <intmax_t N, intmax_t D = 1> class ratio;
  • ratio表示分数值,N表示分子,D表示分母。

  • 定义自己的period,比如ratio<1,-2>,表示的单位是-0.5秒。

  • ratio<60, 1> 表示60s,参考上面的标准类型表。

Durations&ratio举例

// duration constructor
#include <iostream>
#include <ratio>
#include <chrono>
int main ()
{typedef std::chrono::duration<int> seconds_type;//默认为秒typedef std::chrono::duration<int,std::milli> milliseconds_type;typedef std::chrono::duration<int,std::ratio<60*60>> hours_type;hours_type h_oneday (24);                  // 24hseconds_type s_oneday (60*60*24);          // 86400smilliseconds_type ms_oneday (s_oneday);    // 86400000ms,强制类型转换seconds_type s_onehour (60*60);            // 3600s
//hours_type h_onehour (s_onehour);          // NOT VALID (type truncates), use:hours_type h_onehour (std::chrono::duration_cast<hours_type>(s_onehour));//强制类型转换//上面的强制类型转换,说明大单位可以转小单位,小单位转大单位必须使用duration_castmilliseconds_type ms_onehour (s_onehour);  // 3600000ms (ok, no type truncation)std::cout << ms_onehour.count() << "ms in 1h" << std::endl;//count为duration的成员函数,有多少微秒、秒。。return 0;
}

2. Time points

std::chrono::time_point 表示一个具体时间,它的模板类型为:

template <class Clock, class Duration = typename Clock::duration> class time_point;
  • time_point应用举例

time_point有一个函数time_from_eproch()用来获得1970年1月1日8:00:00到time_point时间经过的duration。

下面用到了C语言中的ctime。

// time_point constructors
#include <iostream>
#include <chrono>
#include <ctime>
int main ()
{using namespace std::chrono;time_point <system_clock,duration<int>> tp_seconds (duration<int>(1));//系统经历了一秒的时间点,系统时间1970年开始算system_clock::time_point tp (tp_seconds);//获取时间点std::cout << "1 second since system_clock epoch = ";std::cout << tp.time_since_epoch().count();//经历的clockstd::cout << " system_clock periods." << std::endl;// display time_point:std::time_t tt = system_clock::to_time_t(tp);//经历了一秒的标准时间std::cout << "time_point tp is: " << ctime(&tt);return 0;
}
  • 经历了多少天
#include <iostream>
#include <ratio>
#include <chrono>
int main ()
{using namespace std::chrono;typedef duration<int,std::ratio<60*60*24>> days_type;//表示一天time_point<system_clock,days_type> today = time_point_cast<days_type>(system_clock::now());std::cout << today.time_since_epoch().count() << " days since epoch" << std::endl;return 0;
}
  • 记录一段时间,并写到txt文件
#include <iostream>     // std::cout
#include <chrono>        // std::tuple, std::make_tuple, std::tie
using namespace std;
int main ()
{
ofstream fout("/home/../工作资料/测试时间/buildStructure.txt");;//创建输出流对象chrono::steady_clock::time_point t1 = chrono::steady_clock::now();chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
chrono::duration<double,std::milli> time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);//毫秒
fout << time_used.count()<<endl;t1 = chrono::steady_clock::now();
t2 = chrono::steady_clock::now();
time_used = chrono::duration_cast<chrono::duration<double>>(t2 - t1);return 0;
}

3. Clocks

Clock类获取的是当前的time_point。

  • std::chrono::system_clock获取的是当前的系统时钟.system_clock是不稳定的。因为时钟是可调的,即这种是完全自动适应本地账户的调节;
  • std::chrono::steady_clock可以用来获取程序运行时间差;
  • std::chrono::high_resolution_clock精度最高,它也可能是上面两个类型的代名词。

常用函数:

  1. 成员函数now():用于获取系统的当前时间。
  2. to_time_t(): time_point转换成time_t秒
  3. from_time_t():从time_t转换成time_point
  • system_clock举例
#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 );//转换成ctime标准类型std::cout << "today is: " << ctime(&tt);tt = system_clock::to_time_t ( tomorrow );std::cout << "tomorrow will be: " << ctime(&tt);return 0;
}
  • steady_clock举例
// steady_clock example
#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>int main ()
{using namespace std::chrono;steady_clock::time_point t1 = steady_clock::now();std::cout << "printing out 1000 stars...\n";for (int i=0; i<1000; ++i) std::cout << "*";std::cout << std::endl;steady_clock::time_point t2 = steady_clock::now();duration<double> time_span = duration_cast<duration<double>>(t2 - t1);std::cout << "It took me " << time_span.count() << " seconds.";std::cout << std::endl;return 0;
}

4. chrono与ctime格式转换

参考代码:

#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_today, tt_tomorrow;/********chrono转ctime*********/tt_today = system_clock::to_time_t ( today );//转time_tstd::cout << "today is: " << ctime(&tt_today);tt_tomorrow = system_clock::to_time_t ( tomorrow );std::cout << "tomorrow will be: " << ctime(&tt_tomorrow);/************ctime转chrono***************/system_clock::time_point tp_tomorrow = system_clock::from_time_t (tt_tomorrow);system_clock::time_point tp_today = system_clock::from_time_t (tt_today);system_clock::duration d = tp_tomorrow - tp_today;typedef std::chrono::duration<int,std::ratio<60*60>> hours_type;hours_type nhours = std::chrono::duration_cast<hours_type> (d);std::cout<<"tomorrow duration: " << nhours.count() <<std::endl;return 0;
}

参考:
chrono参考:http://www.cplusplus.com/reference/chrono/


http://chatgpt.dhexx.cn/article/i8Uqhlbc.shtml

相关文章

linux中chrony服务器的使用

1. chrony的优势 Chrony是NTP的另一种实现&#xff0c;与ntpd不同&#xff0c;它可以更快且更准确地同步系统时钟&#xff0c;最大程度的减少时间和频率误差。 chrony相比ntp的优势&#xff1a; 更快的同步&#xff0c;只需要数分钟而非数小时时间&#xff0c;从而最大程度减…

chrony

chrony简介 Chrony是网络时间协议&#xff08;NTP&#xff09;的实现。您可以使用Chrony&#xff1a; 使系统时钟与NTP服务器同步&#xff0c; 使系统时钟与参考时钟&#xff08;例如GPS接收器&#xff09;同步&#xff0c;要将系统时钟与手动时间输入同步&#xff0c; 作为N…

再见 NTP,是时候拥抱下一代时间同步服务 Chrony 了!

Chrony 是一个多功能的 NTP (Network Time Protocol) 实现&#xff0c;类 Unix 系统上 NTP 客户端和服务器的替代品。它可以通过 NTP 服务或者类似 GPS 时钟接收器的硬件级参考时钟来同步系统时钟&#xff0c;具有更好的时钟准确度&#xff0c;并且对于那些间歇性互联网连接的系…

chrony 时间同步详解

一、 简介 Chrony是一个开源自由的网络时间协议 NTP 的客户端和服务器软软件。它能让计算机保持系统时钟与时钟服务器&#xff08;NTP&#xff09;同步&#xff0c;因此让你的计算机保持精确的时间&#xff0c;Chrony也可以作为服务端软件为其他计算机提供时间同步服务。 Chr…

Linux中的Chrony时间同步服务

目录 一、时间同步 1.概念 2.时间同步在运维工作中的作用 3.时间同步完成方法 &#xff08;1&#xff09;NTP时间服务&#xff08;centos 6 &#xff09; &#xff08;2&#xff09;Chrony时间服务 二、Chrony时间服务 1.Chrony介绍 2.Chrony的优点 三、Chrony安装 …

配置chrony时间同步服务

一、chrony简介 chrony 的优势&#xff1a; 更快的同步&#xff0c;从而最大程度减少了时间和频率误差&#xff0c;对于并非全天 24 小时运行的虚拟计算机而言非常有用 能够更好地响应时钟频率的快速变化&#xff0c;对于具备不稳定时钟的虚拟机或导致时钟频率发生变化的节能技…

乐优商城项目

目录 3.4.创建父工程 3.5.创建EurekaServer 3.5.1.创建工程 3.5.2.添加依赖 3.5.3.编写启动类 3.5.4.配置文件 3.6.创建Zuul网关 3.6.1.创建工程 3.6.2.添加依赖 3.6.3.编写启动类 3.6.4.配置文件 3.6.5.项目结构 3.7.创建商品微服务 3.7.1.微服务的结构 3.7.5.…

Java之Spring Data Elasticsearch一篇文章从入门到实战

Elasticsearch提供的Java客户端有一些不太方便的地方&#xff1a; 很多地方需要拼接Json字符串&#xff0c;在java中拼接字符串有多恐怖你应该懂的需要自己把对象序列化为json存储查询到结果也需要自己反序列化为对象 因此&#xff0c;我们这里就不讲解原生的Elasticsearch客…

【Elasticsearch】整合Spring Data Elasticsearch

整合Spring Data Elasticsearch 如何查看官方文档&#xff08;了解&#xff09; 官方文档&#xff1a;Java High Level REST Client | Java REST Client [6.8] | Elastic 下面是获得文档的方式&#xff08;可以不用看&#xff09;&#xff1a; 步骤一&#xff1a;查询“文档…

ElasticSearch高级篇

注&#xff1a;该文档是网上资源&#xff0c;该文档通俗易懂&#xff0c;我已经按照文档学习完了&#xff0c;后期我会加入自己的实践内容。 0.学习目标 独立安装Elasticsearch会使用Rest的API操作索引会使用Rest的API查询数据会使用Rest的API聚合数据掌握Spring Data Elastics…

自己挖坑自己填 spring-data-elasticsearch 4.0.0.M4 简单实践

前几天安装了elasticsearch7.6.1,今天准备实践下spring-data-elasticsearch,没想到4.0改动大资料少,全靠官方文档撑着 文章目录 搭建环境编写yml配置创建实体类 基于Spring Data的CRUD直接从方法名称派生查询自定义的查询 ElasticsearchRestTemplate的使用indexOperations 索引…

Elasticsearch集群,java操作es

今日内容 集群和分布式Elasticsearch集群Elasticsearch客户端 java代码取操作ES有三种方式 1、es提供的原生的操作方式 在es 8.0后会移除 2、es提供的rest风格的操作方式 3、SpringDataElasticSearch的方式 第一章 集群和分布式 集群和分布式的概念有联系也有区别&#xff0c;我…

数据结构(Java)-排序算法-插入排序

插入排序法思想: 插入排序的基本思想是&#xff1a;把n个待排序的元素看成为一个有序表和一个无序表&#xff0c;开始时有序表中只包含一个元素&#xff0c;无序表中包含有n-1个元素&#xff0c;排序过程中每次从无序表中取出第一个元素&#xff0c;把它依次与有序表元素进行比…

ES基础篇-11-Spring提供的elasticsearch组件:Spring Data Elasticsearch

1. 简介 1.1.什么是SpringDataElasticsearch SpringDataElasticsearch&#xff08;以后简称SDE&#xff09;是Spring Data项目下的一个子模块。 查看 Spring Data的官网&#xff1a;http://projects.spring.io/spring-data/ Spring Data 的使命是给各种数据访问提供统一的编…

91-Lucene+ElasticSeach核心技术

LuceneElasticSeach 什么是全文检索&#xff1a; 数据分类&#xff1a; 我们生活中的数据总体分为两种&#xff1a;结构化数据和非结构化数据 结构化数据&#xff1a;指具有固定格式或有限长度的数据&#xff0c;如数据库&#xff0c;元数据等 非结构化数据&#xff1a;指…

SpringBoot集成Elasticseach

目录 一、Elasticseach介绍 1.简单介绍 2.对比关系&#xff1a; 3.详细说明&#xff1a; 4.查出数据的解释 二、SpringBoot集成Elasticseach 1.引入依赖 2.添加配置 3.创建pojo类与索引对应 4.SpringData封装了基础的增删改查&#xff0c;自定义增删改查 5.测试方法-…

Windows系统下载安装MinGW(包括32位和64位)

一、MinGW简介 MinGW是是将GCC编译器和GNU Binutils移植到Win32平台下的产物&#xff0c;包括一系列头文件&#xff08;Win32API&#xff09;、库和可执行文件。MinGW是从Cygwin&#xff08;1.3.3版&#xff09;基础上发展而来。GCC支持的语言大多在MinGW也受支持&#xff0c;其…

MySQL寻找特定字符后的字符串

“日边清梦断,镜里朱颜改” 在操作数据库的某些情况下,我们需要对其中一字段的最后几个字做修改,如下图,把老师改为学生 我找了半天,好像没找到mysql中有合适的函数,能找到最后一个/符号并且修改后面的字符串.需要用好几个函数嵌套使用.如下: select 处理前 as status,org…

支付宝支付功能的实现

支付宝支付功能实现 一.环境准备 二.配置沙箱环境 1.浏览器搜索 支付宝开放平台 2.找到开发服务&#xff0c;选择沙箱 3.根据文档提示&#xff0c;一步步配置 4.配置完成后的样子 5.下载好支付宝手机开放平台助手&#xff0c;可以获取应用私钥 **注意&#xff1a;**应用公…

针对Spring/Gradle启动失败的一些通用解决方案

文章目录 0 前言1 更改Gradle JVM的Java JDK1.1 执行JUnit测试时&#xff0c;Gradle报错 0 前言 当你对Spring/Gradle启动失败的错误信息一筹莫展时&#xff0c;不妨试试以下这些通用的解决方案 1 更改Gradle JVM的Java JDK Settings -> Build, Execution, Deployment -&…