linux下获取系统时间 和 时间偏移

获取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("\n************************* %d:%d:%d **************************\n", 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("\n************************* %d:%d:%d **************************\n",(timeoffset/60)/60,(timeoffset/60)%60,timeoffset%60) ;
}

猜你喜欢

转载自www.cnblogs.com/ChenChangXiong/p/10770649.html