当前位置:网站首页>格式化输出当前时间
格式化输出当前时间
2022-08-10 14:25:00 【土豆西瓜大芝麻】
近期在测试跨进程数据共享时需要测试延时情况,在不适用日志系统的情况下,我们要对别多个进程在数据读写的时刻的差值,为此需要记录各操作对应的时刻,使用uint64_4打印timestamp是一种可行的方式,但是看着还是别扭,于是采用亲和人眼的年月日时分秒的方式。
这段代码比较简单,做成一个函数后面使用吧,也供有需要的人使用。编译g++ test.cpp -o test即可。
#include <sys/time.h>
#include <stdio.h>
#include <iostream>
using namespace std;
/**
* @brief 返回带有yyyy-mm-dd hh:MM:SS.MS格式的当前时刻对应的字符串,如果想使用us级别的,可以修改最后的tv.tv_usec部分
*
* @return std::string
*/
std::string formatCurrentTimeStr()
{
std::string sTimestamp;
char acTimestamp[256];
struct timeval tv;
struct tm *tm;
gettimeofday(&tv, NULL);
tm = localtime(&tv.tv_sec);
sprintf(acTimestamp, "%04d-%02d-%02d %02d:%02d:%02d.%03d\
",
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday,
tm->tm_hour,
tm->tm_min,
tm->tm_sec,
(int)(tv.tv_usec / 1000));
sTimestamp = acTimestamp;
return sTimestamp;
}
int main(void)
{
std::cout << formatCurrentTimeStr() << std::endl;
return 0;
}
边栏推荐
猜你喜欢
随机推荐
基于ArcGIS水文分析、HEC-RAS模拟技术在洪水危险性及风险评估
《论文阅读》PLATO: Pre-trained Dialogue Generation Model with Discrete Latent Variable
高薪程序员&面试题精讲系列135之你对分布式是怎么理解的?CAP理论你知道吗?
BCG库简介
MySQL - 数据库的存储引擎
SQL学习(基础)
2022-08-10 Daily: Swin Transformer author Cao Yue joins Zhiyuan to carry out research on basic vision models
awk的简单使用
Classifying irises using decision trees
数据产品经理那点事儿 二
等保2.0一个中心三重防护指的是什么?如何理解?
[219] The training course notes of the go engineer with more than 3,000 MOOCs 02 Programming ideas in the go language
mysql进阶(三十三)MySQL数据表添加字段
ICML 2022 | 基于随机注意力机制的可解释可泛化图学习
Do not access Object.prototype method ‘hasOwnProperty‘ from target object....
SecureCRTPortable – 破解
司空见惯 - 股市狠狠下跌后,何時能反弹?
Circle 创始人回应美财政部禁止 Tornado :隐私与安全之间关系紧张
一种能让大型数据聚类快2000倍的方法,真不戳
如何完成新媒体产品策划?