增加 Linux 下获取当前时间(精确到毫秒/微妙)例子.

Signed-off-by: ithink.chan <chenyang@autoai.com>
This commit is contained in:
ithink.chan 2020-04-28 14:05:21 +08:00
parent 2e4d588d92
commit 27fabf3542
2 changed files with 63 additions and 0 deletions

View File

@ -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;
}

View File

@ -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 $@