c++ 获取当前时间(精确至秒、毫秒和微妙)

头文件

#include <chrono>

三个概念

Duration(时间段)

概念

表示两个时间点之间的时间差。

时间单位

  • 小时(hours):std::chrono::hours

  • 分钟(minutes):std::chrono::minutes

  • 秒(seconds):std::chrono::seconds

  • 毫秒(milliseconds):std::chrono::milliseconds

  • 微秒(microseconds):std::chrono::microseconds

  • 纳秒(nanoseconds):std::chrono::nanoseconds

时间精度

  • 整数类型精度:std::chrono::duration<int, TimeUnit>
  • 长整数类型精度:std::chrono::duration<long, TimeUnit>
  • 浮点类型精度:std::chrono::duration<float, TimeUnit>
  • 双精度类型精度:std::chrono::duration<double, TimeUnit>

示例1

// 创建一个200毫秒的时间段
std::chrono::duration<int, std::milli> duration1(200); // 表示5秒的duration,使用长整数类型精度
std::chrono::duration<long, std::seconds> duration2(5);// 表示2.5秒的duration,使用浮点类型精度
duration<float, std::seconds> duration3(2.5);// 表示1分钟的duration,使用双精度类型精度
duration<double, std::minutes> duration4(1);

示例2

#include <iostream>
#include <chrono>
#include <thread>int main()
{// 创建两个时间点auto start = std::chrono::steady_clock::now();std::this_thread::sleep_for(std::chrono::seconds(5)); // 模拟5s耗时操作auto end = std::chrono::steady_clock::now();// 计算时间间隔std::chrono::duration<double> duration = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);// 输出时间间隔std::cout << "Elapsed time: " << duration.count() << " seconds\n";return 0;
}

执行结果:

[root@localhost debug]# ./timeTest
Elapsed time: 5.00022 seconds
[root@localhost debug]#

Time point(时间点)

概念

特定时钟上的一个时间。

组成

  1. 时钟(Clock),时钟类型包括:

    • steady_clock(稳定时钟)
    • system_clock(系统时钟)
    • high_resolution_clock(高分辨率时钟)
  2. 表示时间的持续时间(Duration)

示例

#include <iostream>
#include <chrono>
#include <thread>int main()
{// 使用系统时钟获取当前时间点// std::chrono::system_clock::time_point currentTime = std::chrono::system_clock::now();auto currentTime = std::chrono::system_clock::now();std::this_thread::sleep_for(std::chrono::seconds(2));auto laterTime = std::chrono::system_clock::now();// std::chrono::duration<double> duration = std::chrono::duration_cast<std::chrono::duration<double>>(laterTime - currentTime);auto duration = std::chrono::duration_cast<std::chrono::duration<double>>(laterTime - currentTime);std::cout << "The duration is: " << duration.count() << std::endl;auto AfterTime = laterTime + std::chrono::hours(24);duration = std::chrono::duration_cast<std::chrono::duration<double>>(AfterTime - laterTime);std::cout << "The duration for 24H is: " << duration.count() << std::endl;return 0;
}    

执行结果:

[root@localhost debug]# ./timeTest
The duration is: 2.00589
The duration for 24H is: 86400
[root@localhost debug]#

Clock(时钟)

概念

提供了基准和刻度。

时钟类型

  • system_clock
    • system_clock是系统级别的时钟,它表示实时时钟,也就是指示当前时间的时钟。它的时间点是与系统的时钟相关联的,可能受到时钟调整和时区的影响;
    • system_clock用于获取当前的系统时间,可以用来进行日常时间计算和显示。它通常被用作默认的时钟类型;
    • system_clock的最小时间单位取决于系统,可能是秒、毫秒或微秒;
  • steady_clock
    • steady_clock是一个单调递增的时钟,不受任何时钟调整或时区的影响。它提供了一个稳定、可靠的时间基准,适合用于测量时间间隔和计算算法的执行时间;
    • steady_clock的最小时间单位取决于实现,通常是纳秒或微秒级别;
  • high_resolution_clock
    • 可用于测量小时间间隔的时钟。它通常使用最高分辨率的时钟源来提供更高的时间精度。在大部分平台上,high_resolution_clock是steady_clock的别名,因此也是一个单调递增的时钟;
    • 最小时间单位取决于实现,通常是纳秒或微秒级别;

示例1

#include <iostream>
#include <chrono>
#include <thread>int main()
{// std::chrono::steady_clock::time_point steady_start = std::chrono::steady_clock::now();auto steady_start = std::chrono::steady_clock::now();std::this_thread::sleep_for(std::chrono::seconds(1));auto steady_end = std::chrono::steady_clock::now();auto duration = std::chrono::duration_cast<std::chrono::duration<double>>(steady_end - steady_start);std::cout << "The steady_clock duration is: " << duration.count() << std::endl;// std::chrono::high_resolution_clock::time_point high_resolution_start = std::chrono::high_resolution_clock::now();auto high_resolution_start = std::chrono::high_resolution_clock::now();std::this_thread::sleep_for(std::chrono::seconds(1));auto high_resolution_end = std::chrono::high_resolution_clock::now();duration = std::chrono::duration_cast<std::chrono::duration<double>>(high_resolution_end - high_resolution_start);std::cout << "The high_resolution_clock duration is: " << duration.count() << std::endl;return 0;
}

执行结果:

[root@localhost debug.x64-linux-g8]# ./timeTest
The steady_clock duration is: 1.00066
The high_resolution_clock duration is: 1.00085
[root@localhost debug.x64-linux-g8]#

示例2

// 获取当前时间的时间戳#include <iostream>
#include <chrono>
#include <thread>int main()
{auto currentTime = std::chrono::system_clock::now();auto currentTime_s = std::chrono::time_point_cast<std::chrono::seconds>(currentTime);auto currentTime_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(currentTime);auto currentTime_micro = std::chrono::time_point_cast<std::chrono::microseconds>(currentTime);auto currentTime_ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(currentTime);auto valueS = currentTime_s.time_since_epoch().count();auto valueMS = currentTime_ms.time_since_epoch().count();auto valueMicroS = currentTime_micro.time_since_epoch().count();auto valueNS = currentTime_ns.time_since_epoch().count();std::cout << "Seconds: " << valueS << std::endl;std::cout << "Milliseconds: " << valueMS << std::endl;std::cout << "Microseconds: " << valueMicroS << std::endl;std::cout << "Nanoseconds: " << valueNS << std::endl;return 0;
}

执行结果:

[root@localhost debug]# ./timeTest
Seconds: 1700544228
Milliseconds: 1700544228873
Microseconds: 1700544228873536
Nanoseconds: 1700544228873536309
[root@localhost debug]#
示例3
// 将当前时间格式化为时间字符串
#include <iostream>
#include <chrono>
#include <iomanip>int main()
{auto currentTime = std::chrono::system_clock::now();std::time_t t = std::chrono::system_clock::to_time_t(currentTime);std::cout << "CurrentTime: " << std::put_time(std::localtime(&t), "%F %T") << std::endl;return 0;
}

执行结果:

[root@localhost debug]# ./timeTest
CurrentTime: 2023-11-20 14:50:35
[root@localhost debug]#

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/151669.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vue3 源码解读系列(十四)——内置组件

内置组件 问题&#xff1a;内置组件为什么不需要引入&#xff1f; 答&#xff1a;内置组件默认是全局引入的。 <Teleport> 定义 /*** Teleport 组件定义*/ const Teleport {__isTeleport: true,// 组件创建和更新process(nl, n2, container, anchor, parentComponent,…

echarts 横向柱状图示例

该示例有如下几个特点&#xff1a; ①实现tooltip自定义样式&#xff08;echarts 实现tooltip提示框样式自定义-CSDN博客&#xff09; ②实现数据过多时滚动展示&#xff08;echarts 数据过多时展示滚动条-CSDN博客&#xff09; ③柱状图首尾展示文字&#xff0c;文字内容嵌入图…

SpringCloud相关

文章目录 Gateway动态路由灰度策略 FeignRibbon SpringCloud五大组件分别对应&#xff08;1&#xff09;服务注册与发现&#xff08;2&#xff09;客服端负载均衡&#xff08;3&#xff09;断路器&#xff08;4&#xff09;服务网关&#xff08;5&#xff09;分布式配置 Gatewa…

力扣刷题第二十六天--二叉树

前言 昨天看总决赛&#xff0c;差距太大&#xff0c;看的没意思&#xff0c;真的是一点变通没有啊。难受&#xff0c;没有写题的状态了。大概率是最后一次看比赛了&#xff0c;青春已复过&#xff0c;白日忽相催。召唤师要和生活对线了。英雄们的语音&#xff0c;台词&#xf…

【LeetCode】1773. 统计匹配检索规则的物品数量

1773. 统计匹配检索规则的物品数量 难度&#xff1a;简单 题目 给你一个数组 items &#xff0c;其中 items[i] [typei, colori, namei] &#xff0c;描述第 i 件物品的类型、颜色以及名称。 另给你一条由两个字符串 ruleKey 和 ruleValue 表示的检索规则。 如果第 i 件物…

thonny的汉字编码是UTF-8,如何才能转为GB2312?

>>> chinese_str "你" >>> gb2312_str chinese_str.encode(GB2312) >>> print(gb2312_str) b\xe4\xbd\xa0 >>> print(chinese_str.encode(GB2312)) b\xe4\xbd\xa0 一个晚上了&#xff0c;就是找不到方法。好在知道问题在哪里…

Android Studio常见问题

Run一直是上次的apk 内存占用太大&#xff0c;导致闪退

R语言——taxize(第二部分)

taxize&#xff08;第二部分&#xff09; 3. taxize 文档中译3.10. classification&#xff08;根据类群ID检索分类阶元层级&#xff09;示例1&#xff1a;传递单个ID值示例2&#xff1a;传递多个ID值示例3&#xff1a;传递单个名称示例4&#xff1a;传递多个名称示例5&#xf…

【C++】传递‘类非静态成员函数’用作回调函数

在C语言中&#xff0c;传递函数指针是非常常见的操作。 在C语言中&#xff0c;使用C语言一致的方法传递全局函数指针&#xff0c;或者传递静态函数指针也很常见。 不过如果遇到想传递非静态成员函数时&#xff0c;可以参考以下示例代码。 #ifndef _WORKER_HPP_ #define _WOR…

详解FreeRTOS:二值信号量和计数信号量(高级篇—2)

目录 1、二值信号量 1.1、二值信号量运行机制 1.2、创建二值信号量 1

2023.11.18 -自用hadoop高可用环境搭建命令

启动hadoop高可用环境 # 1.先恢复快照到高可用环境 # 2.三台服务器启动zookeeper服务 [rootnode1 ~]# zkServer.sh start [rootnode2 ~]# zkServer.sh start [rootnode3 ~]# zkServer.sh start 查看服务状态: [rootnode]# zkServer.sh status 关闭zk服务的命令是: [rootnode]# …

C#入门(5):数组、一维数组,二维数组、交错数组、数组复制

在C#中&#xff0c;数组是一种用于存储相同类型元素的数据结构。数组提供了一种有序、索引访问的方式&#xff0c;使得可以通过索引快速访问和修改数组中的元素。在C#中&#xff0c;主要有一维数组和二维数组两种类型。 一维数组&#xff08;Single-Dimensional Array&#xf…

SpringCloud -Token传递之Feign

目录 方法一 RequestHeader 方法二 使用Feign的Interceptor 步骤一 实现RequestInterceptor接口 步骤二&#xff1a;配置Feign 通常微服务对于用户认证信息解析有两种方案 在 gateway 就解析用户的 token 然后路由的时候把 userId 等相关信息添加到 header 中传递下去。在…

YOLOv5 配置C2模块构造新模型

&#x1f368; 本文为[&#x1f517;365天深度学习训练营学习记录博客 &#x1f366; 参考文章&#xff1a;365天深度学习训练营 &#x1f356; 原作者&#xff1a;[K同学啊] &#x1f680; 文章来源&#xff1a;[K同学的学习圈子](https://www.yuque.com/mingtian-fkmxf/zxwb4…

【Linux】Linux下的基础IO

❤️前言 大家好&#xff01;今天这篇博客和大家聊一聊关于Linux下的基础IO。 正文 在阅读本篇博客之前&#xff0c;请大家先回顾一下C语言文件操作的一些方法&#xff0c;这里可以看看我之前记录的一些内容&#xff1a; 【C语言】C语言成长之路之文件操作_MO_lion的博客-CSD…

mysql使用--表达式和函数

1.表达式 如&#xff1a;11&#xff0c;一般包含操作数&#xff0c;运算符。 _1.操作数 MYSQL中最常用的操作数有以下几种 (1).常数 (2).列名&#xff0c;针对某个具体的表&#xff0c;它的列名可被当作表达式的一部分 (3).函数调用 一个函数用于完成某个特定的功能。比如NOW()…

【PyQt小知识 - 3】: QComboBox下拉框内容的设置和更新、默认值的设置、值和下标的获取

QComboBox 内容的设置和更新 from PyQt5.QtWidgets import * import sysapp QApplication(sys.argv)mainwindow QMainWindow() mainwindow.resize(200, 200) # 设置下拉框 comboBox QComboBox(mainwindow) comboBox.addItems([上, 中, 下])button QPushButton(更新, main…

Colab跑项目

这里写目录标题 Colab文件目录路径显示更改colab当前工作文件夹Colab挂载谷歌云盘colab使用命令&#xff08;从这开始看&#xff0c;前面no zuo no die)最紧要&#xff0c;首先&#xff0c;修改笔记本设置使用启用gpu![在这里插入图片描述](https://img-blog.csdnimg.cn/591a6c…

NAS层协议栈学习笔记

NAS(Non-Access Stratum)是无线网络中非接入层及包括移动性管理(MM)和会话管理(SM)协议 &#xff0c;在5G(NR)系统中连接管理(Connection Management)用于建立和释放UE与AMF之间的控制面(CP)信令连接。 5G中移动性管理是通过NAS信令在UE与核心网之间进行交互的&#xff0c;连接…

SpringBoot——静态资源及原理

优质博文&#xff1a;IT-BLOG-CN 一、使用 SpringBoot 的步骤 【1】创建SpringBoot应用&#xff0c;选中自己需要的模块。 【2】SpringBoot已经默认将这些场景配置好&#xff0c;只需要在配置文件中指定少量配置就可以运行起来。 【3】编写业务逻辑代码。 二、自动配置原理 …