增加 Linux 下获取当前时间(精确到毫秒/微妙)例子.
Signed-off-by: ithink.chan <chenyang@autoai.com>
This commit is contained in:
parent
2e4d588d92
commit
27fabf3542
|
@ -0,0 +1,48 @@
|
|||
/**
|
||||
* @file GetTime.c
|
||||
* @author Rick Chan (cy187lion@sina.com)
|
||||
* @brief Linux 下获取当前时间(精确到毫秒/微妙)
|
||||
* 头文件:
|
||||
* #include <sys/time.h>
|
||||
* 函数原型:
|
||||
* int gettimeofday (struct timeval * tv, struct timezone * tz);
|
||||
* 说明:
|
||||
* 获取当前的时间的秒数和微秒数
|
||||
* 参数:
|
||||
* struct timeval{
|
||||
* long tv_sec; //秒
|
||||
* long tv_usec; //微秒
|
||||
* };
|
||||
* struct timezone
|
||||
* {
|
||||
* int tz_minuteswest; //和Greenwich 时间差了多少分钟
|
||||
* int tz_dsttime; //日光节约时间的状态
|
||||
* };
|
||||
* 返回值:
|
||||
* @version 0.1
|
||||
* @date 2020-04-28
|
||||
*
|
||||
* @copyright Copyright (c) 2020
|
||||
*
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv,NULL);
|
||||
printf("second:%ld\n",tv.tv_sec); // 秒
|
||||
printf("millisecond:%ld\n",tv.tv_sec*1000 + tv.tv_usec/1000); // 毫秒
|
||||
printf("microsecond:%ld\n",tv.tv_sec*1000000 + tv.tv_usec); // 微秒
|
||||
|
||||
sleep(3); // 为方便观看,让程序睡三秒后对比
|
||||
printf("3s later:\n");
|
||||
|
||||
gettimeofday(&tv,NULL);
|
||||
printf("second:%ld\n",tv.tv_sec); // 秒
|
||||
printf("millisecond:%ld\n",tv.tv_sec*1000 + tv.tv_usec/1000); // 毫秒
|
||||
printf("microsecond:%ld\n",tv.tv_sec*1000000 + tv.tv_usec); // 微秒
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
.PHONY:all
|
||||
.PHONY:clean
|
||||
|
||||
target = GetTime
|
||||
objects = GetTime.o
|
||||
|
||||
all:$(objects)
|
||||
$(CC) $(objects) -o $(target)
|
||||
|
||||
clean:
|
||||
rm -rf $(target)
|
||||
rm -rf $(objects)
|
||||
|
||||
$(objects):%.o: %.c
|
||||
$(CC) -c $< -o $@
|
Loading…
Reference in New Issue