有时,我们需要在程序中,获取当前的系统时间,我们可以用下面两个function来实现.
#include <stdio.h>
#include <time.h>
void get_time( char * STime)
{
time_t now;
struct tm * curtime;
time(&now);
curtime = (struct tm*)localtime(&now);
memset(STime, 0x00, sizeof(STime));
sprintf(STime, "%02d%02d%02d", curtime->tm_hour, cur_time->tm_min, curtime->tm_sec);
return;
}
这个function,主要使用了time(),他只能获得到秒(s)为单位的时间.
void get_sec_time(char * STime)
{
struct timeval sectime;
if(gettimeofday(§ime, NULL))
{
printf("fail to gettimeofday/n");
return;
}
sprintf(STime, "%d:%d", sectime.tv_sec, sectime.tv_usec);
return;
}
这个function,主要使用了gettimeofday(),他只能获得微秒(ms)级时钟.
tv_sec:当前距1970年1月1日0时0分0秒的相对秒数.