获取linux时间 并计算时间偏移
void getSystemTimer(void)
{
#if 0
char *wdate[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"} ;
time_t timep;
struct tm *p;
time(&timep);
p=gmtime(&timep);
printf("
************************* %d:%d:%d **************************
", p->tm_hour, p->tm_min, p->tm_sec);
#endif
static char flag=0;
static long lastsecond = 0 ; //old
long timeoffset = 0 ; //now
struct timeval tv;
struct timezone tz;
gettimeofday(&tv,&tz);
if(flag==0){
lastsecond = tv.tv_sec ;
flag = 1 ;
}
timeoffset = tv.tv_sec - lastsecond ; //get offsettime
printf("
************************* %d:%d:%d **************************
",(timeoffset/60)/60,(timeoffset/60)%60,timeoffset%60) ;
}
方法二:
void getSystemTimer(void)
{
time_t rawtime;
struct tm* timeinfo;
char timE[80];
time(&rawtime);
timeinfo=localtime(&rawtime);
strftime(timE,80,"Date:
%Y-%m-%d %I:%M:%S
",timeinfo);
printf("%s",timE);
}
按照格式输出:
参考百度百科:https://baike.baidu.com/item/strftime/9569073?fr=aladdin