打赏

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

 

方法二:

 void getSystemTimer(void)

{
 time_t rawtime;
 struct tm* timeinfo;
 char timE[80];
 
 time(&rawtime);
 timeinfo=localtime(&rawtime);

 strftime(timE,80,"Date:\n%Y-%m-%d %I:%M:%S\n",timeinfo);
 
 printf("%s",timE);
 }

 

按照格式输出:

参考百度百科:https://baike.baidu.com/item/strftime/9569073?fr=aladdin

 

posted @ 2019-04-25 20:08  陈昌雄  阅读(614)  评论(0编辑  收藏  举报