1,精确到毫秒
gettimeofday()//可精确到微秒
clock_gettime()函数,与gettimeofday类似,但精确度到更高,可以精确到纳秒
C语言获取Linux系统当前时间(精确到毫秒)_Linuxer_Martin-CSDN博客_linuxc获取系统时间毫秒
c语言中获取当前时间的函数_modi000的博客-CSDN博客_c语言获取当前时间的函数
2,Convert from epoch to human-readable date
Epoch Converter - Unix Timestamp Converter
使用time模块 获取时间戳,毫秒__Tsun 的博客-CSDN博客_strftime 毫秒
3,use_sim_time的使用
ros:time::now()详解 - 咸鱼翻身! - 博客园
4,格式化字符串并赋给变量
1、该函数包含在stdio.h的头文件中。
2、sprintf和平时我们常用的printf函数的功能很相似。sprintf函数打印到字符串中(要注意字符串的长度要足够容纳打印的内容,否则会出现内存溢出),而printf函数打印输出到屏幕上。sprintf函数在我们完成其他数据类型转换成字符串类型的操作中应用广泛。
C语言中sprintf()函数的用法_yishizuofei的博客-CSDN博客_sprintf函数的用法
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include<iostream>
using namespace std;
int main()
{
struct timeval tv;
struct timezone tz;
struct tm *p;
char buf[80];
string time_human;
string time_h;
gettimeofday(&tv, &tz);
p = localtime(&tv.tv_sec);
sprintf(buf,"%4d%2d%2d%2d%2d%2d.%ld", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec);
time_human = buf;
puts(buf);
printf("Hello World\n");
time_h=time_human;
cout<<time_h<<endl;
printf("%s\n",time_human.c_str());
time_t rawtime;
struct tm ;
char buf1[80];
char buf2[80];
time(&rawtime);
// Format time, "ddd yyyy-mm-dd hh:mm:ss zzz"
//ts = *localtime(&rawtime);
//strftime(buf1, sizeof(buf1), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
p = localtime(&tv.tv_sec);
strftime(buf1, sizeof(buf1), "%Y%m%d%H%M%S", p);
sprintf(buf2,"%s.%ld", buf1, tv.tv_usec);
printf("%s\n", buf1);
puts(buf2);
return 0;
}