struct timeval{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
struct timezone{
int tz_minuteswest; /*和Greenwich 时间差了多少分钟*/
int tz_dsttime; /*日光节约时间的状态*/
};
int gettimeofday ( struct timeval * tv , struct timezone * tz )
//gettimeofday()会把目前的时间有tv所指的结构返回,当地时区的信息则放到tz所指的结构中。
#include<time.h> //亲自试了一下应该不是<sys/time.h>而是<time.h>
#include<unistd.h>
struct tm nowtime;
struct timeval tv;
unsigned char time_now[128];
gettimeofday(&tv, NULL);
localtime_r(&tv.tv_sec,&nowtime);
sprintf(time_now,"%d-%d-%d %d:%d:%d.%03d ",
nowtime.tm_year+1900,
nowtime.tm_mon+1,
nowtime.tm_mday,
nowtime.tm_hour,
nowtime.tm_min,
nowtime.tm_sec,
(int)(tv.tv_usec/1000)
);
printf("current time is %s\n",time_now);
打印结果如下:
current time is 2018-7-28 15:10:47.883