当前位置: 首页 > article >正文

C++ —— 时间操作 chrono 库

时间操作 chrono 库

  • 时间长度
  • 获取系统时间
  • 时间运算
  • 计时器

时间长度

#include <iostream>
#include <chrono> // 时间相关操作的头文件
using namespace std;

int main () {
    chrono::hours t1(1);
    chrono::minutes t2(60);
    chrono::seconds t3(60 * 60);
    chrono::milliseconds t4(60 * 60 * 1000);

    cout << "t1 = " << t1.count() << " hour" << endl;
    cout << "t2 = " << t2.count() << " minutes" << endl;
    cout << "t3 = " << t3.count() << " seconds" << endl;
    cout << "t4 = " << t4.count() << " milisecons" << endl;

    return 0;
}	

运行结果:

t1 = 1 hour
t2 = 60 minutes
t3 = 3600 seconds
t4 = 3600000 milisecons

获取系统时间

system_clock类支持了对系统时间的访问

#include <iostream>
#include <chrono> // 时间相关操作的头文件
#include <iomanip> // 使用 std::put_time 需要包含的头文件
using namespace std;

int main () {
    // 1.静态成员函数chrono::system_clock::now()用于获取系统时间(C++时间)
    chrono::time_point<chrono::system_clock> now = chrono::system_clock::now();
    // auto now = chrono::system_clock::now();
    
    // 2.把系统时间转换为 time_t(UTC时间)
    time_t t_now = chrono::system_clock::to_time_t(now);
    // auto t_now = chrono::system_clock::to_time_t(now);
    
    // 3.把 time_t 转换成本地时间(北京时间)
    tm* tm_now = std::localtime(&t_now);
    // auto tm_now = std::localtime(&t_now);

    cout << std::put_time(tm_now, "%Y-%m-%d %H:%M:%S") << endl;

    // 获取系统时间后,不一定显示出来,用字符串变量存起来。
    stringstream ss; // 创建stringstream对象ss,可能需要包含<sstream>头文件
    ss << std::put_time(tm_now, "%Y-%m-%d %H:%M:%S"); // 把时间输出到对象ss中
    string timestr = ss.str(); // 把ss转换成string对象
    cout << timestr << endl;

    return 0;
}

运行结果:

2025-03-17 11:33:52
2025-03-17 11:33:52

时间运算

int main () {
    // 获取系统时间(C++时间)
    auto now = chrono::system_clock::now();
    
    // 把系统时间转换为 time_t(UTC时间)
    auto t_now = chrono::system_clock::to_time_t(now);
    // t_now = t_now + 24 * 60 * 60; // 把当前时间往后推一天
    // t_now = t_now - 1 * 60 * 60; // 再把时间往前推一小时
    t_now = t_now + 120; // 再把时间往后推两分钟
    
    // 把 time_t 转换成本地时间(北京时间)
    auto tm_now = std::localtime(&t_now);

    cout << std::put_time(tm_now, "%Y-%m-%d %H:%M:%S") << endl;

    return 0;
}

计时器

steady_clock相当于秒表,实现计时功能。示例代码如下:

int main () {
    // 获取开始的时间点
    // chrono::steady_clock::time_point start = chrono::steady_clock::now();
    auto start = chrono::steady_clock::now();

	// 执行一些代码,消耗一些时间。
    cout << "start" << endl;
    for (int i = 0; i < 1000000; i++) {}
    cout << "end" << endl;

    // 获取结束的时间点
    // chrono::steady_clock::time_point end = chrono::steady_clock::now();
    auto end = chrono::steady_clock::now();

    // 计算时间差
    auto duration = end - start;
    cout << "耗时:" << duration.count() << "纳秒(" << (double)duration.count() / (1000*1000*1000) << "秒)" << endl;

    return 0;
}

运行结果:

start
end
耗时:493699纳秒(0.000493699秒)


http://www.kler.cn/a/591825.html

相关文章:

  • DeepLearning:卷积神经网络基础补充
  • python实现接口自动化
  • Paper Reading: AnomalyGPT:利用大型视觉-语言模型检测工业异常 (AAAI 2024 Oral)
  • 20. Excel 自动化:Excel 对象模型
  • Springboot中的@ConditionalOnBean注解:使用指南与最佳实践
  • 4.2 Reactive 对象的深度类型约束方案
  • linux 命令 cp
  • Pycharm接入DeepSeek,提升自动化脚本的写作效率
  • 基于YOLOv8深度学习的PCB缺陷检测识别系统【python源码+GUI界面+数据集+训练代码】
  • C# BindingFlags 使用详解
  • 在linux 系统下的qt 安装mqtt库
  • maven在idea上搭建
  • flutter 专题 九十八 Flutter 1.7正式版发布
  • WPF 开发从入门到进阶(五)
  • JAVA EE(9)——线程安全——锁策略CAS
  • 【安全运营】用户与实体行为分析(UEBA)浅析
  • Lua语言的自动化测试
  • 【python】带有\n的json字符串,如何优雅打印
  • goweb中文件上传和文件下载
  • 监控视频联网平台在智慧水利中的应用