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

相关文章

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

C++构造函数中explicit详解

《C++构造函数中explicit详解》explicit关键字用于修饰单参数构造函数或可以看作单参数的构造函数,阻止编译器进行隐式类型转换或拷贝初始化,本文就来介绍explicit的使用,感兴趣的可以... 目录1. 什么是explicit2. 隐式转换的问题3.explicit的使用示例基本用法多参数构造

springboot的controller中如何获取applicatim.yml的配置值

《springboot的controller中如何获取applicatim.yml的配置值》本文介绍了在SpringBoot的Controller中获取application.yml配置值的四种方式,... 目录1. 使用@Value注解(最常用)application.yml 配置Controller 中

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

C++打印 vector的几种方法小结

《C++打印vector的几种方法小结》本文介绍了C++中遍历vector的几种方法,包括使用迭代器、auto关键字、typedef、计数器以及C++11引入的范围基础循环,具有一定的参考价值,感兴... 目录1. 使用迭代器2. 使用 auto (C++11) / typedef / type alias

C++ scoped_ptr 和 unique_ptr对比分析

《C++scoped_ptr和unique_ptr对比分析》本文介绍了C++中的`scoped_ptr`和`unique_ptr`,详细比较了它们的特性、使用场景以及现代C++推荐的使用`uni... 目录1. scoped_ptr基本特性主要特点2. unique_ptr基本用法3. 主要区别对比4. u

C++11中的包装器实战案例

《C++11中的包装器实战案例》本文给大家介绍C++11中的包装器实战案例,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录引言1.std::function1.1.什么是std::function1.2.核心用法1.2.1.包装普通函数1.2.

C++多线程开发环境配置方法

《C++多线程开发环境配置方法》文章详细介绍了如何在Windows上安装MinGW-w64和VSCode,并配置环境变量和编译任务,使用VSCode创建一个C++多线程测试项目,并通过配置tasks.... 目录下载安装 MinGW-w64下载安装VS code创建测试项目配置编译任务创建 tasks.js

C++ 多态性实战之何时使用 virtual 和 override的问题解析

《C++多态性实战之何时使用virtual和override的问题解析》在面向对象编程中,多态是一个核心概念,很多开发者在遇到override编译错误时,不清楚是否需要将基类函数声明为virt... 目录C++ 多态性实战:何时使用 virtual 和 override?引言问题场景判断是否需要多态的三个关