首页 > 编程语言 >C++获取机器启动至今的时长和机器启动的时间戳

C++获取机器启动至今的时长和机器启动的时间戳

时间:2023-11-28 10:45:07浏览次数:31  
标签:include 机器 启动 linux C++ machine epoch time ns

根据当前时间戳与机器启动至今的时间长度相减,可以精确计算出机器启动时刻的时间戳epochtime

代码

#include <iostream>
#include <stdio.h>
#include <time.h>
#include <chrono>

int main() 
{
#ifdef __linux
	// linux only
	std::cout << "=== linux only time analysis ===" << std::endl;

	struct timespec timestamp = { 0, 0 };

	clock_gettime(CLOCK_REALTIME, &timestamp);
	uint64_t now_epoch_time = timestamp.tv_sec * 1000000000 + timestamp.tv_nsec;
	std::cout << "current epoch time: " << now_epoch_time << " ns" << std::endl;

	clock_gettime(CLOCK_MONOTONIC, &timestamp);
	uint64_t machine_running_duration = timestamp.tv_sec * 1000000000 + timestamp.tv_nsec;
	std::cout << "machine running to now duration: " << machine_running_duration << " ns" << std::endl;

	uint64_t launch_epoch_time = now_epoch_time - machine_running_duration;
	std::cout << "machine launch epoch time: " << launch_epoch_time << " ns" << std::endl;
#endif

	// cross platform
	std::cout << "=== cross platform time analysis ===" << std::endl;

	auto now = std::chrono::system_clock::now(); // system_clock can get the current timestamp, accuracy to 1 ns in linux and 100 ns in win
	long long now_timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count(); // milliseconds, microseconds, nanoseconds, all are ok
	std::cout << "current epoch time: " << now_timestamp << " ns" << std::endl;

	long long duration = std::chrono::steady_clock::now().time_since_epoch().count(); // steady_clock can get maching running to now duration, accuracy to 1 ns
	std::cout << "machine running to now duration: " << duration << " ns" << std::endl;

	uint64_t launch_timestamp = now_timestamp - duration;
	std::cout << "machine launch epoch time: " << launch_timestamp << " ns" << std::endl;

	return 0;
}

结果

=== linux only time analysis ===
current epoch time: 1587539276554509088 ns
machine running to now duration: 13213451782940677 ns
machine launch epoch time: 1574325824771568411 ns
=== cross platform time analysis ===
current epoch time: 1587539276554564904 ns
machine running to now duration: 13213451782993731 ns
machine launch epoch time: 1574325824771571173 ns

  • 有C++11的方法和linux only的方法
  • 比shell的命令精度高,可以到纳秒
  • 因为是epochtime,可以转换为自然时间格式
  • 计算误差在纳秒级,建议多跑几次求平均
 

标签:include,机器,启动,linux,C++,machine,epoch,time,ns
From: https://www.cnblogs.com/lidabo/p/17861332.html

相关文章

  • C/C++ Zlib实现文件压缩与解压
    在软件开发和数据处理中,对数据进行高效的压缩和解压缩是一项重要的任务。这不仅有助于减小数据在网络传输和存储中的占用空间,还能提高系统的性能和响应速度。本文将介绍如何使用zlib库进行数据的压缩和解压缩,以及如何保存和读取压缩后的文件。zlib是一个开源的数据压缩库,旨在提......
  • C++ 十进制与十六进制转换
    文章作者:里海十进制与十六进制转换#include<iostream>#include<string>usingnamespacestd;//十进制整数转十六进制字符串stringDecimalToHex(longlongdecimal){stringhex="";while(decimal>0){intremainder=decimal%16;......
  • C++ 查找文本文件中字符串是否存在
    简介查找文本文件中字符串是否存在代码#include<iostream>#include<fstream>#include<vector>#include<string>usingnamespacestd;boolSearchString(stringfilePath,stringstrF){vector<string>lines;stringline;ifst......
  • C\C++ 使用RapidJSON库,轻松解析和生成JSON
    简介  RapidJSON是一个高效的C++JSON解析器和生成器。它专注于性能和易用性,使得处理JSON数据变得简单和快速。RapidJSON支持现代的JSON特性,如嵌套对象、数组、Unicode编码和注释。它的API简洁易用,可以轻松解析和生成JSON数据。无论你的项目需要处理大量的JSON数据,还是只需要解析......
  • C++ 字符串编码转换封装函数,UTF-8编码与本地编码互转
    简介字符串编码转换封装函数,UTF-8编码与本地编码互转。中文乱码的解决方法有时候我们会遇到乱码的字符串,比如:古文码可能是用GBK方式读取UTF-8编码的中文导致的,用下面的Utf8ToLocal(stringstr)函数转换一下就可以了。口字码可能是因为以UTF-8的方式读取GBK编码的中文导致的,用下面......
  • C++ 01.学习C++的意义-狄泰软件学院
    一些历史UNIX操作系统诞生之初是用汇编语言编写的随着UNIX系统的发展,汇编语言的开发效率成为瓶颈,所以需要一个新的语言替代汇编语言1971年通过对B语言改良,使其能直接产生机器代码,C语言诞生UNIX使用C语言重写,同时C语言在实践中不断升级完善。C语言的特点没有深思熟虑的设计过程残留......
  • C++ 修改文件创建时间、修改时间属性
    简介        修改文件创建时间、修改时间、大小等属性。        博客《C++获取文件创建时间、修改时间、大小等属性》分享后,好兄弟“古月”发来一段代码,说可以修改文件的创建时间等。测试了一下真可以,下面是运行效果和代码:代码#include<windows.h>#include<f......
  • C++ 使用Windows的API CreateDirectory 创建多层级文件夹
    简介使用Windows的API创建多层级文件夹效果代码#include<windows.h>#include<direct.h>#include<iostream>#include<string>#include<sstream>#include<vector>//创建多层级文件夹boolCreateDir(conststd::string&path){ std::......
  • C++ 获取文件创建时间、修改时间、大小等属性
    简介获取文件创建时间、修改时间、大小等属性代码#include<iostream>#include<string.h>#include<time.h>voidmain(){std::stringfilename="E:\\LiHai123.txt";struct_statstat_buffer;intresult=_stat(filename.c_str(),&stat_b......
  • C++ 获取网卡名称和IP地址
    描述这是获取网卡名称和IP地址的代码示例,参考自。原文描述得比较详细,感谢博主分享。原文代码中没有输出网卡的物理地址,下面的代码进行了补充,并在win10上运行正常。代码//#include<WinSock2.h>#include<Iphlpapi.h>#pragmacomment(lib,"Iphlpapi.lib")//需要添加Iphlpapi.lib......