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

2023-11-21 00:04

本文主要是介绍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]#

这篇关于c++ 获取当前时间(精确至秒、毫秒和微妙)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/398404

相关文章

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

go中的时间处理过程

《go中的时间处理过程》:本文主要介绍go中的时间处理过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 获取当前时间2 获取当前时间戳3 获取当前时间的字符串格式4 相互转化4.1 时间戳转时间字符串 (int64 > string)4.2 时间字符串转时间

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

MySQL 获取字符串长度及注意事项

《MySQL获取字符串长度及注意事项》本文通过实例代码给大家介绍MySQL获取字符串长度及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 获取字符串长度详解 核心长度函数对比⚠️ 六大关键注意事项1. 字符编码决定字节长度2

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

C++11委托构造函数和继承构造函数的实现

《C++11委托构造函数和继承构造函数的实现》C++引入了委托构造函数和继承构造函数这两个重要的特性,本文主要介绍了C++11委托构造函数和继承构造函数的实现,具有一定的参考价值,感兴趣的可以了解一下... 目录引言一、委托构造函数1.1 委托构造函数的定义与作用1.2 委托构造函数的语法1.3 委托构造函