当前位置:网站首页>格式化输出当前时间
格式化输出当前时间
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;
}边栏推荐
- 舵机内部结及工作原理浅析[通俗易懂]
- Open source SPL wipes out tens of thousands of database intermediate tables
- 雨水中存在的PFAS化学物质对饮用水安全构成了威胁
- ICML 2022 | 基于随机注意力机制的可解释可泛化图学习
- 强意识 压责任 安全培训筑牢生产屏障
- How to code like a pro in 2022 and avoid If-Else
- NAACL 2022 | 简单且高效!随机中间层映射指导的知识蒸馏方法
- PyTorch 多机多卡训练:DDP 实战与技巧
- 这一次,话筒给你:向自由软件之父斯托曼 提问啦!
- 如何完成新媒体产品策划?
猜你喜欢

Alibaba的秒杀系统—千亿级并发设计手册上线了

日志@Slf4j介绍使用及配置等级

王学岗—————————哔哩哔哩直播-手写哔哩哔哩硬编码录屏推流(硬编)(26节课)

1W字详解线程本地存储 ThreadLocal
![[JS Advanced] Creating sub-objects and replacing this_10 in ES5 standard specification](/img/3e/14a1d7c2837c896eaa0ca625eaa040.png)
[JS Advanced] Creating sub-objects and replacing this_10 in ES5 standard specification

Unfinished mathematics test paper ----- test paper generator (Qt includes source code)

实现一个深克隆

符合信创要求的堡垒机有哪些?支持哪些系统?

字节终面:CPU 是如何读写内存的?

Cloud Migration Practice of Redis
随机推荐
Lack of comparators, op amps come to the rescue!(Op amp is recorded as a comparator circuit)
黑客入门,从HTB开始
file system design
力扣解法汇总640-求解方程
PEST 分析法
A method that can make large data clustering 2000 times faster
AWS 安全基础知识
《论文阅读》PLATO: Pre-trained Dialogue Generation Model with Discrete Latent Variable
【POI 2008, BLO】割点
从全球价值链视角看,京东云数智供应链对未来经济有何影响?
MySQL interview questions
1W word detailed thread local storage ThreadLocal
Data product manager thing 2
等保2.0一个中心三重防护指的是什么?如何理解?
Summary of tensorflow installation stepping on the pit
缺少比较器,运放来救场!(运放当做比较器电路记录)
Unfinished mathematics test paper ----- test paper generator (Qt includes source code)
BCG库简介
"Thesis Reading" PLATO: Pre-trained Dialogue Generation Model with Discrete Latent Variable
Steam教育在新时代中综合学习论